small optimization: make whatprovidesdata + 2 always contain the systemsolvable
[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 LIBSOLV_UTIL_H
14 #define LIBSOLV_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 *solv_malloc(size_t);
24 extern void *solv_malloc2(size_t, size_t);
25 extern void *solv_calloc(size_t, size_t);
26 extern void *solv_realloc(void *, size_t);
27 extern void *solv_realloc2(void *, size_t, size_t);
28 extern void *solv_free(void *);
29 extern char *solv_strdup(const char *);
30 extern void solv_oom(size_t, size_t);
31 extern unsigned int solv_timems(unsigned int subtract);
32 extern void solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard);
33 extern char *solv_dupjoin(const char *str1, const char *str2, const char *str3);
34 extern char *solv_dupappend(const char *str1, const char *str2, const char *str3);
35 extern int solv_hex2bin(const char **strp, unsigned char *buf, int bufl);
36 extern char *solv_bin2hex(const unsigned char *buf, int l, char *str);
37
38
39 static inline void *solv_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
40 {
41   if (nmemb == 1)
42     {
43       if ((len & block) == 0)
44         buf = solv_realloc2(buf, len + (1 + block), size);
45     }
46   else
47     {
48       if (((len - 1) | block) != ((len + nmemb - 1) | block))
49         buf = solv_realloc2(buf, (len + (nmemb + block)) & ~block, size);
50     }
51   return buf;
52 }
53
54 /**
55  * extend an array by reallocation and zero's the new section
56  * buf old pointer
57  * len current size
58  * nmbemb number of elements to add
59  * size size of each element
60  * block block size used to allocate the elements
61  */
62 static inline void *solv_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
63 {
64   buf = solv_extend(buf, len, nmemb, size, block);
65   memset((char *)buf + len * size, 0, nmemb * size);
66   return buf;
67 }
68
69 static inline void *solv_extend_resize(void *buf, size_t len, size_t size, size_t block)
70 {
71   if (len)
72     buf = solv_realloc2(buf, (len + block) & ~block, size);
73   return buf;
74 }
75
76 static inline void *solv_calloc_block(size_t len, size_t size, size_t block)
77 {
78   void *buf;
79   if (!len)
80     return 0;
81   buf = solv_malloc2((len + block) & ~block, size);
82   memset(buf, 0, ((len + block) & ~block) * size);
83   return buf;
84 }
85 #endif /* LIBSOLV_UTIL_H */