obey droporphaned map in keep_orhans mode
[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 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /**
24  * malloc
25  * exits with error message on error
26  */
27 extern void *solv_malloc(size_t);
28 extern void *solv_malloc2(size_t, size_t);
29 extern void *solv_calloc(size_t, size_t);
30 extern void *solv_realloc(void *, size_t);
31 extern void *solv_realloc2(void *, size_t, size_t);
32 extern void *solv_free(void *);
33 extern char *solv_strdup(const char *);
34 extern void solv_oom(size_t, size_t);
35 extern unsigned int solv_timems(unsigned int subtract);
36 extern void solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard);
37 extern char *solv_dupjoin(const char *str1, const char *str2, const char *str3);
38 extern char *solv_dupappend(const char *str1, const char *str2, const char *str3);
39 extern int solv_hex2bin(const char **strp, unsigned char *buf, int bufl);
40 extern char *solv_bin2hex(const unsigned char *buf, int l, char *str);
41 extern size_t solv_validutf8(const char *buf);
42 extern char *solv_latin1toutf8(const char *buf);
43 extern char *solv_replacebadutf8(const char *buf, int replchar);
44
45
46 static inline void *solv_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
47 {
48   if (nmemb == 1)
49     {
50       if ((len & block) == 0)
51         buf = solv_realloc2(buf, len + (1 + block), size);
52     }
53   else
54     {
55       if (((len - 1) | block) != ((len + nmemb - 1) | block))
56         buf = solv_realloc2(buf, (len + (nmemb + block)) & ~block, size);
57     }
58   return buf;
59 }
60
61 /**
62  * extend an array by reallocation and zero's the new section
63  * buf old pointer
64  * len current size
65  * nmbemb number of elements to add
66  * size size of each element
67  * block block size used to allocate the elements
68  */
69 static inline void *solv_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
70 {
71   buf = solv_extend(buf, len, nmemb, size, block);
72   memset((char *)buf + len * size, 0, nmemb * size);
73   return buf;
74 }
75
76 static inline void *solv_extend_resize(void *buf, size_t len, size_t size, size_t block)
77 {
78   if (len)
79     buf = solv_realloc2(buf, (len + block) & ~block, size);
80   return buf;
81 }
82
83 static inline void *solv_calloc_block(size_t len, size_t size, size_t block)
84 {
85   void *buf;
86   if (!len)
87     return 0;
88   buf = solv_malloc2((len + block) & ~block, size);
89   memset(buf, 0, ((len + block) & ~block) * size);
90   return buf;
91 }
92
93 static inline void *solv_memdup(void *buf, size_t len)
94 {
95   void *newbuf;
96   if (!buf)
97     return 0;
98   newbuf = solv_malloc(len);
99   if (len)
100     memcpy(newbuf, buf, len);
101   return newbuf;
102 }
103
104 static inline void *solv_memdup2(void *buf, size_t num, size_t len)
105 {
106   void *newbuf;
107   if (!buf)
108     return 0;
109   newbuf = solv_malloc2(num, len);
110   if (num)
111     memcpy(newbuf, buf, num * len);
112   return newbuf;
113 }
114
115 #ifdef __cplusplus
116 }
117 #endif
118
119 #endif /* LIBSOLV_UTIL_H */