2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
19 solv_oom(size_t num, size_t len)
22 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
24 fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
30 solv_malloc(size_t len)
32 void *r = malloc(len ? len : 1);
39 solv_malloc2(size_t num, size_t len)
41 if (len && (num * len) / len != num)
43 return solv_malloc(num * len);
47 solv_realloc(void *old, size_t len)
50 old = malloc(len ? len : 1);
52 old = realloc(old, len ? len : 1);
59 solv_realloc2(void *old, size_t num, size_t len)
61 if (len && (num * len) / len != num)
63 return solv_realloc(old, num * len);
67 solv_calloc(size_t num, size_t len)
70 if (num == 0 || len == 0)
88 solv_timems(unsigned int subtract)
93 if (gettimeofday(&tv, 0))
95 r = (((unsigned int)tv.tv_sec >> 16) * 1000) << 16;
96 r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
97 r += (unsigned int)tv.tv_usec / 1000;
101 /* bsd's qsort_r has different arguments, so we define our
102 own version in case we need to do some clever mapping
104 see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
106 #if defined(__GLIBC__)
108 # if HAVE_QSORT_R || HAVE___QSORT_R
110 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
113 qsort_r(base, nmemb, size, compar, compard);
115 /* backported for SLE10-SP2 */
116 __qsort_r(base, nmemb, size, compar, compard);
120 #else /* qsort_r or __qsort_r on glibc */
121 /* use own version of qsort if none available */
125 #else /* not glibc */
127 struct solv_sort_data {
128 int (*compar)(const void *, const void *, void *);
133 solv_sort_helper(void *compard, const void *a, const void *b)
135 struct solv_sort_data *d = compard;
136 return (*d->compar)(a, b, d->compard);
140 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
142 struct solv_sort_data d;
145 qsort_r(base, nmemb, size, &d, solv_sort_helper);
151 solv_dupjoin(const char *str1, const char *str2, const char *str3)
155 l1 = str1 ? strlen(str1) : 0;
156 l2 = str2 ? strlen(str2) : 0;
157 l3 = str3 ? strlen(str3) : 0;
158 s = str = solv_malloc(l1 + l2 + l3 + 1);
179 solv_dupappend(const char *str1, const char *str2, const char *str3)
181 char *str = solv_dupjoin(str1, str2, str3);
182 solv_free((void *)str1);
187 solv_hex2bin(const char **strp, unsigned char *buf, int bufl)
189 const char *str = *strp;
192 for (i = 0; i < bufl; i++)
196 if (c >= '0' && c <= '9')
198 else if (c >= 'a' && c <= 'f')
200 else if (c >= 'A' && c <= 'F')
206 if (c >= '0' && c <= '9')
208 else if (c >= 'a' && c <= 'f')
210 else if (c >= 'A' && c <= 'F')
222 solv_bin2hex(const unsigned char *buf, int l, char *str)
225 for (i = 0; i < l; i++, buf++)
228 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
230 *str++ = c < 10 ? c + '0' : c + ('a' - 10);