- bring back dataiterator_match for now (to be removed again)
[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 /**
20  * malloc
21  * exits with error message on error
22  */
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
31 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
32 {
33   if (nmemb == 1)
34     {
35       if ((len & block) == 0)
36         buf = sat_realloc2(buf, len + (1 + block), size);
37     }
38   else
39     {
40       if (((len - 1) | block) != ((len + nmemb - 1) | block))
41         buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
42     }
43   return buf;
44 }
45
46 /**
47  * extend an array by reallocation and zero's the new section
48  * buf old pointer
49  * len current size
50  * nmbemb number of elements to add
51  * size size of each element
52  * block block size used to allocate the elements
53  */
54 static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
55 {
56   buf = sat_extend(buf, len, nmemb, size, block);
57   memset((char *)buf + len * size, 0, nmemb * size);
58   return buf;
59 }
60
61 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
62 {
63   if (len)
64     buf = sat_realloc2(buf, (len + block) & ~block, size);
65   return buf;
66 }
67
68 static inline void *sat_calloc_block(size_t len, size_t size, size_t block)
69 {
70   void *buf;
71   if (!len)
72     return 0;
73   buf = sat_malloc2((len + block) & ~block, size);
74   memset(buf, 0, ((len + block) & ~block) * size);
75   return buf;
76 }
77 #endif /* SATSOLVER_UTIL_H */