5488965cd13bd986d2a99bc73b570a7cef944a17
[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 extern void sat_oom(size_t, size_t);
26
27 static inline void *sat_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
28 {
29   if (nmemb == 1)
30     {
31       if ((len & block) == 0)
32         buf = sat_realloc2(buf, len + (1 + block), size);
33     }
34   else
35     {
36       if (((len - 1) | block) != ((len + nmemb - 1) | block))
37         buf = sat_realloc2(buf, (len + (nmemb + block)) & ~block, size);
38     }
39   return buf;
40 }
41
42 static inline void *sat_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
43 {
44   buf = sat_extend(buf, len, nmemb, size, block);
45   memset((char *)buf + len * size, 0, nmemb * size);
46   return buf;
47 }
48
49 static inline void *sat_extend_resize(void *buf, size_t len, size_t size, size_t block)
50 {
51   if (len)
52     buf = sat_realloc2(buf, (len + block) & ~block, size);
53   return buf;
54 }
55
56 static inline void *sat_calloc_block(size_t len, size_t size, size_t block)
57 {
58   void *buf;
59   if (!len)
60     return 0;
61   buf = sat_malloc2((len + block) & ~block, size);
62   memset(buf, 0, ((len + block) & ~block) * size);
63   return buf;
64 }
65 #endif /* SATSOLVER_UTIL_H */