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