- shut up g++
[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 #include <stddef.h>
17 #include <string.h>
18
19 extern void *sat_malloc(size_t);
20 extern void *sat_malloc2(size_t, size_t);
21 extern void *sat_calloc(size_t, size_t);
22 extern void *sat_realloc(void *, size_t);
23 extern void *sat_realloc2(void *, size_t, size_t);
24 extern void *sat_free(void *);
25
26 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
27 {
28   if (nmemb == 1)
29     {
30       if ((len & block) == 0)
31         buf = sat_realloc2(buf, len + (1 + block), size);
32     }
33   else
34     {
35       if (((len - 1) | block) != ((len + nmemb - 1) | block))
36         buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
37     }
38   return buf;
39 }
40
41 static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
42 {
43   buf = sat_extend(buf, len, nmemb, size, block);
44   memset((char *)buf + len * size, 0, nmemb * size);
45   return buf;
46 }
47
48 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
49 {
50   if (len)
51     buf = sat_realloc2(buf, (len + block) & ~block, size);
52   return buf;
53 }
54
55 static inline void *sat_calloc_block(size_t len, size_t size, size_t block)
56 {
57   void *buf;
58   if (!len)
59     return 0;
60   buf = sat_malloc2((len + block) & ~block, size);
61   memset(buf, 0, ((len + block) & ~block) * size);
62   return buf;
63 }
64 #endif /* SATSOLVER_UTIL_H */