Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 249 lines (219 sloc) 4.62 kB
1312779 @mlschroe - add a license
mlschroe authored
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
8b2723e @mlschroe - add sat_sort() function
mlschroe authored
8 #define _GNU_SOURCE
9
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
edab283 @mlschroe - adapt to coding style
mlschroe authored
14 #include <sys/time.h>
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
15
16 #include "util.h"
17
b089419 @mlschroe - clean up a bit
mlschroe authored
18 void
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
19 solv_oom(size_t num, size_t len)
b089419 @mlschroe - clean up a bit
mlschroe authored
20 {
21 if (num)
22 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
23 else
65c03f6 @mlschroe - add pool_set_installed() function, remove no longer needed "installed"
mlschroe authored
24 fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
25 abort();
b089419 @mlschroe - clean up a bit
mlschroe authored
26 exit(1);
27 }
28
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
29 void *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
30 solv_malloc(size_t len)
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
31 {
32 void *r = malloc(len ? len : 1);
b089419 @mlschroe - clean up a bit
mlschroe authored
33 if (!r)
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
34 solv_oom(0, len);
b089419 @mlschroe - clean up a bit
mlschroe authored
35 return r;
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
36 }
37
38 void *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
39 solv_malloc2(size_t num, size_t len)
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
40 {
41 if (len && (num * len) / len != num)
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
42 solv_oom(num, len);
43 return solv_malloc(num * len);
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
44 }
45
46 void *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
47 solv_realloc(void *old, size_t len)
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
48 {
49 if (old == 0)
50 old = malloc(len ? len : 1);
51 else
52 old = realloc(old, len ? len : 1);
b089419 @mlschroe - clean up a bit
mlschroe authored
53 if (!old)
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
54 solv_oom(0, len);
b089419 @mlschroe - clean up a bit
mlschroe authored
55 return old;
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
56 }
57
58 void *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
59 solv_realloc2(void *old, size_t num, size_t len)
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
60 {
61 if (len && (num * len) / len != num)
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
62 solv_oom(num, len);
63 return solv_realloc(old, num * len);
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
64 }
65
66 void *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
67 solv_calloc(size_t num, size_t len)
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
68 {
69 void *r;
70 if (num == 0 || len == 0)
71 r = malloc(1);
72 else
73 r = calloc(num, len);
b089419 @mlschroe - clean up a bit
mlschroe authored
74 if (!r)
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
75 solv_oom(num, len);
b089419 @mlschroe - clean up a bit
mlschroe authored
76 return r;
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
77 }
78
79 void *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
80 solv_free(void *mem)
8f8a9ed @kkaempf current state of 'sat-solver'
kkaempf authored
81 {
82 if (mem)
83 free(mem);
84 return 0;
85 }
86
87c5e01 @mlschroe - add solv_strdup(), rename repo_freeallrepos() to pool_freeallrepos()
mlschroe authored
87 char *
88 solv_strdup(const char *s)
89 {
90 char *r;
91 if (!s)
92 return 0;
93 r = strdup(s);
94 if (!r)
95 solv_oom(0, strlen(s));
96 return r;
97 }
98
edab283 @mlschroe - adapt to coding style
mlschroe authored
99 unsigned int
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
100 solv_timems(unsigned int subtract)
edab283 @mlschroe - adapt to coding style
mlschroe authored
101 {
102 struct timeval tv;
103 unsigned int r;
104
105 if (gettimeofday(&tv, 0))
106 return 0;
107 r = (((unsigned int)tv.tv_sec >> 16) * 1000) << 16;
108 r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
109 r += (unsigned int)tv.tv_usec / 1000;
110 return r - subtract;
111 }
112
8b2723e @mlschroe - add sat_sort() function
mlschroe authored
113 /* bsd's qsort_r has different arguments, so we define our
19b96f7 @kkaempf Add link to qsort_r wierdness
kkaempf authored
114 own version in case we need to do some clever mapping
c9ff482 @dmacvicar decide which qsort_r to use based on what it is available
dmacvicar authored
115
19b96f7 @kkaempf Add link to qsort_r wierdness
kkaempf authored
116 see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
117 */
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
118 #if defined(__GLIBC__)
119
c9ff482 @dmacvicar decide which qsort_r to use based on what it is available
dmacvicar authored
120 # if HAVE_QSORT_R || HAVE___QSORT_R
8b2723e @mlschroe - add sat_sort() function
mlschroe authored
121 void
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
122 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
8b2723e @mlschroe - add sat_sort() function
mlschroe authored
123 {
c9ff482 @dmacvicar decide which qsort_r to use based on what it is available
dmacvicar authored
124 # if HAVE_QSORT_R
8b2723e @mlschroe - add sat_sort() function
mlschroe authored
125 qsort_r(base, nmemb, size, compar, compard);
adbc547 @mlschroe - use __qsort_r on old SLE10 versions
mlschroe authored
126 # else
127 /* backported for SLE10-SP2 */
128 __qsort_r(base, nmemb, size, compar, compard);
129 # endif
c9ff482 @dmacvicar decide which qsort_r to use based on what it is available
dmacvicar authored
130
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
131 }
c9ff482 @dmacvicar decide which qsort_r to use based on what it is available
dmacvicar authored
132 #else /* qsort_r or __qsort_r on glibc */
133 /* use own version of qsort if none available */
134 #include "qsort_r.c"
135 #endif
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
136
c9ff482 @dmacvicar decide which qsort_r to use based on what it is available
dmacvicar authored
137 #else /* not glibc */
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
138
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
139 struct solv_sort_data {
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
140 int (*compar)(const void *, const void *, void *);
141 void *compard;
142 };
143
144 static int
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
145 solv_sort_helper(void *compard, const void *a, const void *b)
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
146 {
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
147 struct solv_sort_data *d = compard;
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
148 return (*d->compar)(a, b, d->compard);
8b2723e @mlschroe - add sat_sort() function
mlschroe authored
149 }
a250cf2 @mlschroe - add pool_tmpjoin and sat_dupjoin helpers
mlschroe authored
150
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
151 void
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
152 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
153 {
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
154 struct solv_sort_data d;
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
155 d.compar = compar;
156 d.compard = compard;
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
157 qsort_r(base, nmemb, size, &d, solv_sort_helper);
9820b03 @mlschroe - rename to USE_OWN_QSORT, add bsd qsort_r helper
mlschroe authored
158 }
159
160 #endif
161
a250cf2 @mlschroe - add pool_tmpjoin and sat_dupjoin helpers
mlschroe authored
162 char *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
163 solv_dupjoin(const char *str1, const char *str2, const char *str3)
a250cf2 @mlschroe - add pool_tmpjoin and sat_dupjoin helpers
mlschroe authored
164 {
165 int l1, l2, l3;
166 char *s, *str;
167 l1 = str1 ? strlen(str1) : 0;
168 l2 = str2 ? strlen(str2) : 0;
169 l3 = str3 ? strlen(str3) : 0;
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
170 s = str = solv_malloc(l1 + l2 + l3 + 1);
a250cf2 @mlschroe - add pool_tmpjoin and sat_dupjoin helpers
mlschroe authored
171 if (l1)
172 {
173 strcpy(s, str1);
174 s += l1;
175 }
176 if (l2)
177 {
178 strcpy(s, str2);
179 s += l2;
180 }
181 if (l3)
182 {
183 strcpy(s, str3);
184 s += l3;
185 }
186 *s = 0;
187 return str;
188 }
f7dc6ef @mlschroe - add pool_freetmpspace, pool_tmpappend, pool_bin2hex, sat_dupappend,…
mlschroe authored
189
190 char *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
191 solv_dupappend(const char *str1, const char *str2, const char *str3)
f7dc6ef @mlschroe - add pool_freetmpspace, pool_tmpappend, pool_bin2hex, sat_dupappend,…
mlschroe authored
192 {
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
193 char *str = solv_dupjoin(str1, str2, str3);
194 solv_free((void *)str1);
f7dc6ef @mlschroe - add pool_freetmpspace, pool_tmpappend, pool_bin2hex, sat_dupappend,…
mlschroe authored
195 return str;
196 }
197
198 int
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
199 solv_hex2bin(const char **strp, unsigned char *buf, int bufl)
f7dc6ef @mlschroe - add pool_freetmpspace, pool_tmpappend, pool_bin2hex, sat_dupappend,…
mlschroe authored
200 {
201 const char *str = *strp;
202 int i;
203
204 for (i = 0; i < bufl; i++)
205 {
206 int c = *str;
207 int d;
208 if (c >= '0' && c <= '9')
209 d = c - '0';
210 else if (c >= 'a' && c <= 'f')
211 d = c - ('a' - 10);
212 else if (c >= 'A' && c <= 'F')
213 d = c - ('A' - 10);
214 else
215 break;
216 c = *++str;
217 d <<= 4;
218 if (c >= '0' && c <= '9')
219 d |= c - '0';
220 else if (c >= 'a' && c <= 'f')
221 d |= c - ('a' - 10);
222 else if (c >= 'A' && c <= 'F')
223 d |= c - ('A' - 10);
224 else
225 break;
226 buf[i] = d;
227 ++str;
228 }
229 *strp = str;
230 return i;
231 }
232
233 char *
09f29f7 @mlschroe - rename all sat_ to solv_
mlschroe authored
234 solv_bin2hex(const unsigned char *buf, int l, char *str)
f7dc6ef @mlschroe - add pool_freetmpspace, pool_tmpappend, pool_bin2hex, sat_dupappend,…
mlschroe authored
235 {
236 int i;
237 for (i = 0; i < l; i++, buf++)
238 {
239 int c = *buf >> 4;
240 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
241 c = *buf & 15;
242 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
243 }
244 *str = 0;
245 return str;
246 }
247
248
Something went wrong with that request. Please try again.