00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef SATSOLVER_UTIL_H
00014 #define SATSOLVER_UTIL_H
00015
00016 #include <stddef.h>
00017 #include <string.h>
00018
00023 extern void *sat_malloc(size_t);
00024 extern void *sat_malloc2(size_t, size_t);
00025 extern void *sat_calloc(size_t, size_t);
00026 extern void *sat_realloc(void *, size_t);
00027 extern void *sat_realloc2(void *, size_t, size_t);
00028 extern void *sat_free(void *);
00029 extern void sat_oom(size_t, size_t);
00030 extern unsigned int sat_timems(unsigned int subtract);
00031 extern void sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard);
00032 extern char *sat_dupjoin(const char *str1, const char *str2, const char *str3);
00033 extern char *sat_dupappend(const char *str1, const char *str2, const char *str3);
00034 extern int sat_hex2bin(const char **strp, unsigned char *buf, int bufl);
00035 extern char *sat_bin2hex(const unsigned char *buf, int l, char *str);
00036
00037
00038 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
00039 {
00040 if (nmemb == 1)
00041 {
00042 if ((len & block) == 0)
00043 buf = sat_realloc2(buf, len + (1 + block), size);
00044 }
00045 else
00046 {
00047 if (((len - 1) | block) != ((len + nmemb - 1) | block))
00048 buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
00049 }
00050 return buf;
00051 }
00052
00061 static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
00062 {
00063 buf = sat_extend(buf, len, nmemb, size, block);
00064 memset((char *)buf + len * size, 0, nmemb * size);
00065 return buf;
00066 }
00067
00068 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
00069 {
00070 if (len)
00071 buf = sat_realloc2(buf, (len + block) & ~block, size);
00072 return buf;
00073 }
00074
00075 static inline void *sat_calloc_block(size_t len, size_t size, size_t block)
00076 {
00077 void *buf;
00078 if (!len)
00079 return 0;
00080 buf = sat_malloc2((len + block) & ~block, size);
00081 memset(buf, 0, ((len + block) & ~block) * size);
00082 return buf;
00083 }
00084 #endif