satsolver  0.17.2
util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7 
8 /*
9  * util.h
10  *
11  */
12 
13 #ifndef SATSOLVER_UTIL_H
14 #define SATSOLVER_UTIL_H
15 
16 #include <stddef.h>
17 #include <string.h>
18 
23 extern void *sat_malloc(size_t);
24 extern void *sat_malloc2(size_t, size_t);
25 extern void *sat_calloc(size_t, size_t);
26 extern void *sat_realloc(void *, size_t);
27 extern void *sat_realloc2(void *, size_t, size_t);
28 extern void *sat_free(void *);
29 extern void sat_oom(size_t, size_t);
30 extern unsigned int sat_timems(unsigned int subtract);
31 extern void sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard);
32 extern char *sat_dupjoin(const char *str1, const char *str2, const char *str3);
33 extern char *sat_dupappend(const char *str1, const char *str2, const char *str3);
34 extern int sat_hex2bin(const char **strp, unsigned char *buf, int bufl);
35 extern char *sat_bin2hex(const unsigned char *buf, int l, char *str);
36 
37 
38 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
39 {
40  if (nmemb == 1)
41  {
42  if ((len & block) == 0)
43  buf = sat_realloc2(buf, len + (1 + block), size);
44  }
45  else
46  {
47  if (((len - 1) | block) != ((len + nmemb - 1) | block))
48  buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
49  }
50  return buf;
51 }
52 
61 static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
62 {
63  buf = sat_extend(buf, len, nmemb, size, block);
64  memset((char *)buf + len * size, 0, nmemb * size);
65  return buf;
66 }
67 
68 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
69 {
70  if (len)
71  buf = sat_realloc2(buf, (len + block) & ~block, size);
72  return buf;
73 }
74 
75 static inline void *sat_calloc_block(size_t len, size_t size, size_t block)
76 {
77  void *buf;
78  if (!len)
79  return 0;
80  buf = sat_malloc2((len + block) & ~block, size);
81  memset(buf, 0, ((len + block) & ~block) * size);
82  return buf;
83 }
84 #endif /* SATSOLVER_UTIL_H */