2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
19 sat_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 sat_malloc(size_t len)
32 void *r = malloc(len ? len : 1);
39 sat_malloc2(size_t num, size_t len)
41 if (len && (num * len) / len != num)
43 return sat_malloc(num * len);
47 sat_realloc(void *old, size_t len)
50 old = malloc(len ? len : 1);
52 old = realloc(old, len ? len : 1);
59 sat_realloc2(void *old, size_t num, size_t len)
61 if (len && (num * len) / len != num)
63 return sat_realloc(old, num * len);
67 sat_calloc(size_t num, size_t len)
70 if (num == 0 || len == 0)
88 sat_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;
105 /* bsd's qsort_r has different arguments, so we define our
106 own version in case we need to do some clever mapping
108 see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
110 #if defined(__GLIBC__)
113 sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
115 # if __GLIBC_PREREQ(2, 8)
116 qsort_r(base, nmemb, size, compar, compard);
118 /* backported for SLE10-SP2 */
119 __qsort_r(base, nmemb, size, compar, compard);
125 struct sat_sort_data {
126 int (*compar)(const void *, const void *, void *);
131 sat_sort_helper(void *compard, const void *a, const void *b)
133 struct sat_sort_data *d = compard;
134 return (*d->compar)(a, b, d->compard);
138 sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
140 struct sat_sort_data d;
143 qsort_r(base, nmemb, size, &d, sat_sort_helper);
148 #endif /* USE_OWN_QSORT */
151 sat_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 = sat_malloc(l1 + l2 + l3 + 1);