2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
13 #ifndef LIBSOLV_UTIL_H
14 #define LIBSOLV_UTIL_H
21 * exits with error message on error
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 void solv_oom(size_t, size_t);
30 extern unsigned int solv_timems(unsigned int subtract);
31 extern void solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard);
32 extern char *solv_dupjoin(const char *str1, const char *str2, const char *str3);
33 extern char *solv_dupappend(const char *str1, const char *str2, const char *str3);
34 extern int solv_hex2bin(const char **strp, unsigned char *buf, int bufl);
35 extern char *solv_bin2hex(const unsigned char *buf, int l, char *str);
38 static inline void *solv_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
42 if ((len & block) == 0)
43 buf = solv_realloc2(buf, len + (1 + block), size);
47 if (((len - 1) | block) != ((len + nmemb - 1) | block))
48 buf = solv_realloc2(buf, (len + (nmemb + block)) & ~block, size);
54 * extend an array by reallocation and zero's the new section
57 * nmbemb number of elements to add
58 * size size of each element
59 * block block size used to allocate the elements
61 static inline void *solv_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
63 buf = solv_extend(buf, len, nmemb, size, block);
64 memset((char *)buf + len * size, 0, nmemb * size);
68 static inline void *solv_extend_resize(void *buf, size_t len, size_t size, size_t block)
71 buf = solv_realloc2(buf, (len + block) & ~block, size);
75 static inline void *solv_calloc_block(size_t len, size_t size, size_t block)
80 buf = solv_malloc2((len + block) & ~block, size);
81 memset(buf, 0, ((len + block) & ~block) * size);
84 #endif /* LIBSOLV_UTIL_H */