util.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Novell Inc.
00003  *
00004  * This program is licensed under the BSD license, read LICENSE.BSD
00005  * for further information
00006  */
00007 
00008 /*
00009  * util.h
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 
00034 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
00035 {
00036   if (nmemb == 1)
00037     {
00038       if ((len & block) == 0)
00039         buf = sat_realloc2(buf, len + (1 + block), size);
00040     }
00041   else
00042     {
00043       if (((len - 1) | block) != ((len + nmemb - 1) | block))
00044         buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
00045     }
00046   return buf;
00047 }
00048 
00057 static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
00058 {
00059   buf = sat_extend(buf, len, nmemb, size, block);
00060   memset((char *)buf + len * size, 0, nmemb * size);
00061   return buf;
00062 }
00063 
00064 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
00065 {
00066   if (len)
00067     buf = sat_realloc2(buf, (len + block) & ~block, size);
00068   return buf;
00069 }
00070 
00071 static inline void *sat_calloc_block(size_t len, size_t size, size_t block)
00072 {
00073   void *buf;
00074   if (!len)
00075     return 0;
00076   buf = sat_malloc2((len + block) & ~block, size);
00077   memset(buf, 0, ((len + block) & ~block) * size);
00078   return buf;
00079 }
00080 #endif /* SATSOLVER_UTIL_H */
Generated on Mon Dec 12 11:44:12 2011 for satsolver by  doxygen 1.6.3