]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Merge pull request #1879 from libgit2/redir-refactor
[libgit2.git] / src / util.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_util_h__
8 #define INCLUDE_util_h__
9
10 #include "common.h"
11
12 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
13 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
14 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
15 #ifndef min
16 # define min(a,b) ((a) < (b) ? (a) : (b))
17 #endif
18 #ifndef max
19 # define max(a,b) ((a) > (b) ? (a) : (b))
20 #endif
21
22 /*
23 * Custom memory allocation wrappers
24 * that set error code and error message
25 * on allocation failure
26 */
27 GIT_INLINE(void *) git__malloc(size_t len)
28 {
29 void *ptr = malloc(len);
30 if (!ptr) giterr_set_oom();
31 return ptr;
32 }
33
34 GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
35 {
36 void *ptr = calloc(nelem, elsize);
37 if (!ptr) giterr_set_oom();
38 return ptr;
39 }
40
41 GIT_INLINE(char *) git__strdup(const char *str)
42 {
43 char *ptr = strdup(str);
44 if (!ptr) giterr_set_oom();
45 return ptr;
46 }
47
48 GIT_INLINE(char *) git__strndup(const char *str, size_t n)
49 {
50 size_t length = 0;
51 char *ptr;
52
53 while (length < n && str[length])
54 ++length;
55
56 ptr = (char*)git__malloc(length + 1);
57
58 if (!ptr)
59 return NULL;
60
61 if (length)
62 memcpy(ptr, str, length);
63
64 ptr[length] = '\0';
65
66 return ptr;
67 }
68
69 /* NOTE: This doesn't do null or '\0' checking. Watch those boundaries! */
70 GIT_INLINE(char *) git__substrdup(const char *start, size_t n)
71 {
72 char *ptr = (char*)git__malloc(n+1);
73 memcpy(ptr, start, n);
74 ptr[n] = '\0';
75 return ptr;
76 }
77
78 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
79 {
80 void *new_ptr = realloc(ptr, size);
81 if (!new_ptr) giterr_set_oom();
82 return new_ptr;
83 }
84
85 GIT_INLINE(void) git__free(void *ptr)
86 {
87 free(ptr);
88 }
89
90 #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
91 ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
92
93 #define CASESELECT(IGNORE_CASE, ICASE, CASE) \
94 ((IGNORE_CASE) ? (ICASE) : (CASE))
95
96 extern int git__prefixcmp(const char *str, const char *prefix);
97 extern int git__prefixcmp_icase(const char *str, const char *prefix);
98 extern int git__suffixcmp(const char *str, const char *suffix);
99
100 GIT_INLINE(int) git__signum(int val)
101 {
102 return ((val > 0) - (val < 0));
103 }
104
105 extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
106 extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
107
108 extern void git__hexdump(const char *buffer, size_t n);
109 extern uint32_t git__hash(const void *key, int len, uint32_t seed);
110
111 /** @return true if p fits into the range of a size_t */
112 GIT_INLINE(int) git__is_sizet(git_off_t p)
113 {
114 size_t r = (size_t)p;
115 return p == (git_off_t)r;
116 }
117
118 /** @return true if p fits into the range of a uint32_t */
119 GIT_INLINE(int) git__is_uint32(size_t p)
120 {
121 uint32_t r = (uint32_t)p;
122 return p == (size_t)r;
123 }
124
125 /* 32-bit cross-platform rotl */
126 #ifdef _MSC_VER /* use built-in method in MSVC */
127 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
128 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
129 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
130 #endif
131
132 extern char *git__strtok(char **end, const char *sep);
133 extern char *git__strsep(char **end, const char *sep);
134
135 extern void git__strntolower(char *str, size_t len);
136 extern void git__strtolower(char *str);
137
138 GIT_INLINE(const char *) git__next_line(const char *s)
139 {
140 while (*s && *s != '\n') s++;
141 while (*s == '\n' || *s == '\r') s++;
142 return s;
143 }
144
145 GIT_INLINE(const void *) git__memrchr(const void *s, int c, size_t n)
146 {
147 const unsigned char *cp;
148
149 if (n != 0) {
150 cp = (unsigned char *)s + n;
151 do {
152 if (*(--cp) == (unsigned char)c)
153 return cp;
154 } while (--n != 0);
155 }
156
157 return NULL;
158 }
159
160 typedef int (*git__tsort_cmp)(const void *a, const void *b);
161
162 extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp);
163
164 typedef int (*git__sort_r_cmp)(const void *a, const void *b, void *payload);
165
166 extern void git__tsort_r(
167 void **dst, size_t size, git__sort_r_cmp cmp, void *payload);
168
169 extern void git__qsort_r(
170 void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload);
171
172 extern void git__insertsort_r(
173 void *els, size_t nel, size_t elsize, void *swapel,
174 git__sort_r_cmp cmp, void *payload);
175
176 /**
177 * @param position If non-NULL, this will be set to the position where the
178 * element is or would be inserted if not found.
179 * @return 0 if found; GIT_ENOTFOUND if not found
180 */
181 extern int git__bsearch(
182 void **array,
183 size_t array_len,
184 const void *key,
185 int (*compare)(const void *key, const void *element),
186 size_t *position);
187
188 extern int git__bsearch_r(
189 void **array,
190 size_t array_len,
191 const void *key,
192 int (*compare_r)(const void *key, const void *element, void *payload),
193 void *payload,
194 size_t *position);
195
196 extern int git__strcmp_cb(const void *a, const void *b);
197
198 extern int git__strcmp(const char *a, const char *b);
199 extern int git__strcasecmp(const char *a, const char *b);
200 extern int git__strncmp(const char *a, const char *b, size_t sz);
201 extern int git__strncasecmp(const char *a, const char *b, size_t sz);
202
203 extern int git__strcasesort_cmp(const char *a, const char *b);
204
205 #include "thread-utils.h"
206
207 typedef struct {
208 git_atomic refcount;
209 void *owner;
210 } git_refcount;
211
212 typedef void (*git_refcount_freeptr)(void *r);
213
214 #define GIT_REFCOUNT_INC(r) { \
215 git_atomic_inc(&((git_refcount *)(r))->refcount); \
216 }
217
218 #define GIT_REFCOUNT_DEC(_r, do_free) { \
219 git_refcount *r = (git_refcount *)(_r); \
220 int val = git_atomic_dec(&r->refcount); \
221 if (val <= 0 && r->owner == NULL) { do_free(_r); } \
222 }
223
224 #define GIT_REFCOUNT_OWN(r, o) { \
225 ((git_refcount *)(r))->owner = o; \
226 }
227
228 #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
229
230 #define GIT_REFCOUNT_VAL(r) git_atomic_get(&((git_refcount *)(r))->refcount)
231
232
233 static signed char from_hex[] = {
234 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
235 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
236 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
237 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
238 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
239 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
240 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
241 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
242 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
243 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
244 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
245 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
249 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
250 };
251
252 GIT_INLINE(int) git__fromhex(char h)
253 {
254 return from_hex[(unsigned char) h];
255 }
256
257 GIT_INLINE(int) git__ishex(const char *str)
258 {
259 unsigned i;
260 for (i=0; i<strlen(str); i++)
261 if (git__fromhex(str[i]) < 0)
262 return 0;
263 return 1;
264 }
265
266 GIT_INLINE(size_t) git__size_t_bitmask(size_t v)
267 {
268 v--;
269 v |= v >> 1;
270 v |= v >> 2;
271 v |= v >> 4;
272 v |= v >> 8;
273 v |= v >> 16;
274
275 return v;
276 }
277
278 GIT_INLINE(size_t) git__size_t_powerof2(size_t v)
279 {
280 return git__size_t_bitmask(v) + 1;
281 }
282
283 GIT_INLINE(bool) git__isupper(int c)
284 {
285 return (c >= 'A' && c <= 'Z');
286 }
287
288 GIT_INLINE(bool) git__isalpha(int c)
289 {
290 return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
291 }
292
293 GIT_INLINE(bool) git__isdigit(int c)
294 {
295 return (c >= '0' && c <= '9');
296 }
297
298 GIT_INLINE(bool) git__isspace(int c)
299 {
300 return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */);
301 }
302
303 GIT_INLINE(bool) git__isspace_nonlf(int c)
304 {
305 return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */);
306 }
307
308 GIT_INLINE(bool) git__iswildcard(int c)
309 {
310 return (c == '*' || c == '?' || c == '[');
311 }
312
313 /*
314 * Parse a string value as a boolean, just like Core Git does.
315 *
316 * Valid values for true are: 'true', 'yes', 'on'
317 * Valid values for false are: 'false', 'no', 'off'
318 */
319 extern int git__parse_bool(int *out, const char *value);
320
321 /*
322 * Parse a string into a value as a git_time_t.
323 *
324 * Sample valid input:
325 * - "yesterday"
326 * - "July 17, 2003"
327 * - "2003-7-17 08:23"
328 */
329 extern int git__date_parse(git_time_t *out, const char *date);
330
331 /*
332 * Unescapes a string in-place.
333 *
334 * Edge cases behavior:
335 * - "jackie\" -> "jacky\"
336 * - "chan\\" -> "chan\"
337 */
338 extern size_t git__unescape(char *str);
339
340 /*
341 * Safely zero-out memory, making sure that the compiler
342 * doesn't optimize away the operation.
343 */
344 GIT_INLINE(void) git__memzero(void *data, size_t size)
345 {
346 #ifdef _MSC_VER
347 SecureZeroMemory((PVOID)data, size);
348 #else
349 volatile uint8_t *scan = (volatile uint8_t *)data;
350
351 while (size--)
352 *scan++ = 0x0;
353 #endif
354 }
355
356 #ifdef GIT_WIN32
357
358 GIT_INLINE(double) git__timer(void)
359 {
360 /* We need the initial tick count to detect if the tick
361 * count has rolled over. */
362 static DWORD initial_tick_count = 0;
363
364 /* GetTickCount returns the number of milliseconds that have
365 * elapsed since the system was started. */
366 DWORD count = GetTickCount();
367
368 if(initial_tick_count == 0) {
369 initial_tick_count = count;
370 } else if (count < initial_tick_count) {
371 /* The tick count has rolled over - adjust for it. */
372 count = (0xFFFFFFFF - initial_tick_count) + count;
373 }
374
375 return (double) count / (double) 1000;
376 }
377
378 #elif __APPLE__
379
380 #include <mach/mach_time.h>
381
382 double git__timer(void)
383 {
384 uint64_t time = mach_absolute_time();
385 static double scaling_factor = 0;
386
387 if (scaling_factor == 0) {
388 mach_timebase_info_data_t info;
389 (void)mach_timebase_info(&info);
390 scaling_factor = (double)info.numer / (double)info.denom;
391 }
392
393 return (double)time * scaling_factor / 1.0E-9;
394 }
395
396 #else
397
398 #include <sys/time.h>
399
400 GIT_INLINE(double) git__timer(void)
401 {
402 struct timespec tp;
403
404 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
405 return (double) tp.tv_sec + (double) tp.tv_nsec / 1E-9;
406 } else {
407 /* Fall back to using gettimeofday */
408 struct timeval tv;
409 struct timezone tz;
410 gettimeofday(&tv, &tz);
411 return (double)tv.tv_sec + (double)tv.tv_usec / 1E-6;
412 }
413 }
414
415 #endif
416
417 #endif /* INCLUDE_util_h__ */