7c196e76e818b220e1b31a1e0fadbd84c6553cd9
[platform/upstream/libsolv.git] / src / util.h
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 extern void *sat_malloc(size_t);
17 extern void *sat_malloc2(size_t, size_t);
18 extern void *sat_calloc(size_t, size_t);
19 extern void *sat_realloc(void *, size_t);
20 extern void *sat_realloc2(void *, size_t, size_t);
21 extern void *sat_free(void *);
22
23 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
24 {
25   if (nmemb == 1)
26     {
27       if ((len & block) == 0)
28         buf = sat_realloc2(buf, len + (1 + block), size);
29     }
30   else
31     {
32       if (((len - 1) | block) != ((len + nmemb - 1) | block))
33         buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
34     }
35   return buf;
36 }
37
38 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
39 {
40   if (len)
41     buf = sat_realloc2(buf, (len + block) & ~block, size);
42   return buf;
43 }
44
45 static inline void *sat_extend_cleanup(void *buf, size_t len, size_t size)
46 {
47   if (len)
48     buf = sat_realloc2(buf, len, size);
49   return buf;
50 }
51
52 #endif /* SATSOLVER_UTIL_H */