]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Merge pull request #3071 from linquize/git_reflog_drop
[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 #if defined(GIT_MSVC_CRTDBG)
11 /* Enable MSVC CRTDBG memory leak reporting.
12 *
13 * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
14 * documentation because all allocs/frees in libgit2 already go through
15 * the "git__" routines defined in this file. Simply using the normal
16 * reporting mechanism causes all leaks to be attributed to a routine
17 * here in util.h (ie, the actual call to calloc()) rather than the
18 * caller of git__calloc().
19 *
20 * Therefore, we declare a set of "git__crtdbg__" routines to replace
21 * the corresponding "git__" routines and re-define the "git__" symbols
22 * as macros. This allows us to get and report the file:line info of
23 * the real caller.
24 *
25 * We DO NOT replace the "git__free" routine because it needs to remain
26 * a function pointer because it is used as a function argument when
27 * setting up various structure "destructors".
28 *
29 * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
30 * "free" to be remapped to "_free_dbg" and this causes problems for
31 * structures which define a field named "free".
32 *
33 * Finally, CRTDBG must be explicitly enabled and configured at program
34 * startup. See tests/main.c for an example.
35 */
36 #include <stdlib.h>
37 #include <crtdbg.h>
38 #endif
39
40 #include "common.h"
41 #include "strnlen.h"
42
43 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
44 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
45 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
46 #ifndef min
47 # define min(a,b) ((a) < (b) ? (a) : (b))
48 #endif
49 #ifndef max
50 # define max(a,b) ((a) > (b) ? (a) : (b))
51 #endif
52
53 #define GIT_DATE_RFC2822_SZ 32
54
55 /**
56 * Return the length of a constant string.
57 * We are aware that `strlen` performs the same task and is usually
58 * optimized away by the compiler, whilst being safer because it returns
59 * valid values when passed a pointer instead of a constant string; however
60 * this macro will transparently work with wide-char and single-char strings.
61 */
62 #define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
63
64 #if defined(GIT_MSVC_CRTDBG)
65 GIT_INLINE(void *) git__crtdbg__malloc(size_t len, const char *file, int line)
66 {
67 void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, file, line);
68 if (!ptr) giterr_set_oom();
69 return ptr;
70 }
71
72 GIT_INLINE(void *) git__crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
73 {
74 void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, file, line);
75 if (!ptr) giterr_set_oom();
76 return ptr;
77 }
78
79 GIT_INLINE(char *) git__crtdbg__strdup(const char *str, const char *file, int line)
80 {
81 char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, file, line);
82 if (!ptr) giterr_set_oom();
83 return ptr;
84 }
85
86 GIT_INLINE(char *) git__crtdbg__strndup(const char *str, size_t n, const char *file, int line)
87 {
88 size_t length = 0, alloclength;
89 char *ptr;
90
91 length = p_strnlen(str, n);
92
93 if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
94 !(ptr = git__crtdbg__malloc(alloclength, file, line)))
95 return NULL;
96
97 if (length)
98 memcpy(ptr, str, length);
99
100 ptr[length] = '\0';
101
102 return ptr;
103 }
104
105 GIT_INLINE(char *) git__crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
106 {
107 char *ptr;
108 size_t alloclen;
109
110 if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
111 !(ptr = git__crtdbg__malloc(alloclen, file, line)))
112 return NULL;
113
114 memcpy(ptr, start, n);
115 ptr[n] = '\0';
116 return ptr;
117 }
118
119 GIT_INLINE(void *) git__crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
120 {
121 void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, file, line);
122 if (!new_ptr) giterr_set_oom();
123 return new_ptr;
124 }
125
126 GIT_INLINE(void *) git__crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
127 {
128 size_t newsize;
129 return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
130 NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, file, line);
131 }
132
133 GIT_INLINE(void *) git__crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
134 {
135 return git__crtdbg__reallocarray(NULL, nelem, elsize, file, line);
136 }
137
138 #define git__malloc(len) git__crtdbg__malloc(len, __FILE__, __LINE__)
139 #define git__calloc(nelem, elsize) git__crtdbg__calloc(nelem, elsize, __FILE__, __LINE__)
140 #define git__strdup(str) git__crtdbg__strdup(str, __FILE__, __LINE__)
141 #define git__strndup(str, n) git__crtdbg__strndup(str, n, __FILE__, __LINE__)
142 #define git__substrdup(str, n) git__crtdbg__substrdup(str, n, __FILE__, __LINE__)
143 #define git__realloc(ptr, size) git__crtdbg__realloc(ptr, size, __FILE__, __LINE__)
144 #define git__reallocarray(ptr, nelem, elsize) git__crtdbg__reallocarray(ptr, nelem, elsize, __FILE__, __LINE__)
145 #define git__mallocarray(nelem, elsize) git__crtdbg__mallocarray(nelem, elsize, __FILE__, __LINE__)
146
147 #else
148
149 /*
150 * Custom memory allocation wrappers
151 * that set error code and error message
152 * on allocation failure
153 */
154 GIT_INLINE(void *) git__malloc(size_t len)
155 {
156 void *ptr = malloc(len);
157 if (!ptr) giterr_set_oom();
158 return ptr;
159 }
160
161 GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
162 {
163 void *ptr = calloc(nelem, elsize);
164 if (!ptr) giterr_set_oom();
165 return ptr;
166 }
167
168 GIT_INLINE(char *) git__strdup(const char *str)
169 {
170 char *ptr = strdup(str);
171 if (!ptr) giterr_set_oom();
172 return ptr;
173 }
174
175 GIT_INLINE(char *) git__strndup(const char *str, size_t n)
176 {
177 size_t length = 0, alloclength;
178 char *ptr;
179
180 length = p_strnlen(str, n);
181
182 if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
183 !(ptr = git__malloc(alloclength)))
184 return NULL;
185
186 if (length)
187 memcpy(ptr, str, length);
188
189 ptr[length] = '\0';
190
191 return ptr;
192 }
193
194 /* NOTE: This doesn't do null or '\0' checking. Watch those boundaries! */
195 GIT_INLINE(char *) git__substrdup(const char *start, size_t n)
196 {
197 char *ptr;
198 size_t alloclen;
199
200 if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
201 !(ptr = git__malloc(alloclen)))
202 return NULL;
203
204 memcpy(ptr, start, n);
205 ptr[n] = '\0';
206 return ptr;
207 }
208
209 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
210 {
211 void *new_ptr = realloc(ptr, size);
212 if (!new_ptr) giterr_set_oom();
213 return new_ptr;
214 }
215
216 /**
217 * Similar to `git__realloc`, except that it is suitable for reallocing an
218 * array to a new number of elements of `nelem`, each of size `elsize`.
219 * The total size calculation is checked for overflow.
220 */
221 GIT_INLINE(void *) git__reallocarray(void *ptr, size_t nelem, size_t elsize)
222 {
223 size_t newsize;
224 return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
225 NULL : realloc(ptr, newsize);
226 }
227
228 /**
229 * Similar to `git__calloc`, except that it does not zero memory.
230 */
231 GIT_INLINE(void *) git__mallocarray(size_t nelem, size_t elsize)
232 {
233 return git__reallocarray(NULL, nelem, elsize);
234 }
235
236 #endif /* !MSVC_CTRDBG */
237
238 GIT_INLINE(void) git__free(void *ptr)
239 {
240 free(ptr);
241 }
242
243 #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
244 ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
245
246 #define CASESELECT(IGNORE_CASE, ICASE, CASE) \
247 ((IGNORE_CASE) ? (ICASE) : (CASE))
248
249 extern int git__prefixcmp(const char *str, const char *prefix);
250 extern int git__prefixcmp_icase(const char *str, const char *prefix);
251 extern int git__prefixncmp_icase(const char *str, size_t str_n, const char *prefix);
252 extern int git__suffixcmp(const char *str, const char *suffix);
253
254 GIT_INLINE(int) git__signum(int val)
255 {
256 return ((val > 0) - (val < 0));
257 }
258
259 extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
260 extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
261
262 extern void git__hexdump(const char *buffer, size_t n);
263 extern uint32_t git__hash(const void *key, int len, uint32_t seed);
264
265 /* 32-bit cross-platform rotl */
266 #ifdef _MSC_VER /* use built-in method in MSVC */
267 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
268 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
269 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
270 #endif
271
272 extern char *git__strtok(char **end, const char *sep);
273 extern char *git__strsep(char **end, const char *sep);
274
275 extern void git__strntolower(char *str, size_t len);
276 extern void git__strtolower(char *str);
277
278 GIT_INLINE(const char *) git__next_line(const char *s)
279 {
280 while (*s && *s != '\n') s++;
281 while (*s == '\n' || *s == '\r') s++;
282 return s;
283 }
284
285 GIT_INLINE(const void *) git__memrchr(const void *s, int c, size_t n)
286 {
287 const unsigned char *cp;
288
289 if (n != 0) {
290 cp = (unsigned char *)s + n;
291 do {
292 if (*(--cp) == (unsigned char)c)
293 return cp;
294 } while (--n != 0);
295 }
296
297 return NULL;
298 }
299
300 typedef int (*git__tsort_cmp)(const void *a, const void *b);
301
302 extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp);
303
304 typedef int (*git__sort_r_cmp)(const void *a, const void *b, void *payload);
305
306 extern void git__tsort_r(
307 void **dst, size_t size, git__sort_r_cmp cmp, void *payload);
308
309 extern void git__qsort_r(
310 void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload);
311
312 extern void git__insertsort_r(
313 void *els, size_t nel, size_t elsize, void *swapel,
314 git__sort_r_cmp cmp, void *payload);
315
316 /**
317 * @param position If non-NULL, this will be set to the position where the
318 * element is or would be inserted if not found.
319 * @return 0 if found; GIT_ENOTFOUND if not found
320 */
321 extern int git__bsearch(
322 void **array,
323 size_t array_len,
324 const void *key,
325 int (*compare)(const void *key, const void *element),
326 size_t *position);
327
328 extern int git__bsearch_r(
329 void **array,
330 size_t array_len,
331 const void *key,
332 int (*compare_r)(const void *key, const void *element, void *payload),
333 void *payload,
334 size_t *position);
335
336 extern int git__strcmp_cb(const void *a, const void *b);
337 extern int git__strcasecmp_cb(const void *a, const void *b);
338
339 extern int git__strcmp(const char *a, const char *b);
340 extern int git__strcasecmp(const char *a, const char *b);
341 extern int git__strncmp(const char *a, const char *b, size_t sz);
342 extern int git__strncasecmp(const char *a, const char *b, size_t sz);
343
344 extern int git__strcasesort_cmp(const char *a, const char *b);
345
346 #include "thread-utils.h"
347
348 typedef struct {
349 git_atomic refcount;
350 void *owner;
351 } git_refcount;
352
353 typedef void (*git_refcount_freeptr)(void *r);
354
355 #define GIT_REFCOUNT_INC(r) { \
356 git_atomic_inc(&((git_refcount *)(r))->refcount); \
357 }
358
359 #define GIT_REFCOUNT_DEC(_r, do_free) { \
360 git_refcount *r = (git_refcount *)(_r); \
361 int val = git_atomic_dec(&r->refcount); \
362 if (val <= 0 && r->owner == NULL) { do_free(_r); } \
363 }
364
365 #define GIT_REFCOUNT_OWN(r, o) { \
366 ((git_refcount *)(r))->owner = o; \
367 }
368
369 #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
370
371 #define GIT_REFCOUNT_VAL(r) git_atomic_get(&((git_refcount *)(r))->refcount)
372
373
374 static signed char from_hex[] = {
375 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
376 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
377 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
378 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
379 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
380 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
381 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
382 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
383 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
384 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
385 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
386 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
387 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
388 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
389 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
390 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
391 };
392
393 GIT_INLINE(int) git__fromhex(char h)
394 {
395 return from_hex[(unsigned char) h];
396 }
397
398 GIT_INLINE(int) git__ishex(const char *str)
399 {
400 unsigned i;
401 for (i=0; str[i] != '\0'; i++)
402 if (git__fromhex(str[i]) < 0)
403 return 0;
404 return 1;
405 }
406
407 GIT_INLINE(size_t) git__size_t_bitmask(size_t v)
408 {
409 v--;
410 v |= v >> 1;
411 v |= v >> 2;
412 v |= v >> 4;
413 v |= v >> 8;
414 v |= v >> 16;
415
416 return v;
417 }
418
419 GIT_INLINE(size_t) git__size_t_powerof2(size_t v)
420 {
421 return git__size_t_bitmask(v) + 1;
422 }
423
424 GIT_INLINE(bool) git__isupper(int c)
425 {
426 return (c >= 'A' && c <= 'Z');
427 }
428
429 GIT_INLINE(bool) git__isalpha(int c)
430 {
431 return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
432 }
433
434 GIT_INLINE(bool) git__isdigit(int c)
435 {
436 return (c >= '0' && c <= '9');
437 }
438
439 GIT_INLINE(bool) git__isspace(int c)
440 {
441 return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v');
442 }
443
444 GIT_INLINE(bool) git__isspace_nonlf(int c)
445 {
446 return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v');
447 }
448
449 GIT_INLINE(bool) git__iswildcard(int c)
450 {
451 return (c == '*' || c == '?' || c == '[');
452 }
453
454 /*
455 * Parse a string value as a boolean, just like Core Git does.
456 *
457 * Valid values for true are: 'true', 'yes', 'on'
458 * Valid values for false are: 'false', 'no', 'off'
459 */
460 extern int git__parse_bool(int *out, const char *value);
461
462 /*
463 * Parse a string into a value as a git_time_t.
464 *
465 * Sample valid input:
466 * - "yesterday"
467 * - "July 17, 2003"
468 * - "2003-7-17 08:23"
469 */
470 extern int git__date_parse(git_time_t *out, const char *date);
471
472 /*
473 * Format a git_time as a RFC2822 string
474 *
475 * @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
476 * @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
477 * @param date the date to be formatted
478 * @return 0 if successful; -1 on error
479 */
480 extern int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date);
481
482 /*
483 * Unescapes a string in-place.
484 *
485 * Edge cases behavior:
486 * - "jackie\" -> "jacky\"
487 * - "chan\\" -> "chan\"
488 */
489 extern size_t git__unescape(char *str);
490
491 /*
492 * Iterate through an UTF-8 string, yielding one
493 * codepoint at a time.
494 *
495 * @param str current position in the string
496 * @param str_len size left in the string; -1 if the string is NULL-terminated
497 * @param dst pointer where to store the current codepoint
498 * @return length in bytes of the read codepoint; -1 if the codepoint was invalid
499 */
500 extern int git__utf8_iterate(const uint8_t *str, int str_len, int32_t *dst);
501
502 /*
503 * Safely zero-out memory, making sure that the compiler
504 * doesn't optimize away the operation.
505 */
506 GIT_INLINE(void) git__memzero(void *data, size_t size)
507 {
508 #ifdef _MSC_VER
509 SecureZeroMemory((PVOID)data, size);
510 #else
511 volatile uint8_t *scan = (volatile uint8_t *)data;
512
513 while (size--)
514 *scan++ = 0x0;
515 #endif
516 }
517
518 #ifdef GIT_WIN32
519
520 GIT_INLINE(double) git__timer(void)
521 {
522 /* We need the initial tick count to detect if the tick
523 * count has rolled over. */
524 static DWORD initial_tick_count = 0;
525
526 /* GetTickCount returns the number of milliseconds that have
527 * elapsed since the system was started. */
528 DWORD count = GetTickCount();
529
530 if(initial_tick_count == 0) {
531 initial_tick_count = count;
532 } else if (count < initial_tick_count) {
533 /* The tick count has rolled over - adjust for it. */
534 count = (0xFFFFFFFF - initial_tick_count) + count;
535 }
536
537 return (double) count / (double) 1000;
538 }
539
540 #elif __APPLE__
541
542 #include <mach/mach_time.h>
543
544 GIT_INLINE(double) git__timer(void)
545 {
546 uint64_t time = mach_absolute_time();
547 static double scaling_factor = 0;
548
549 if (scaling_factor == 0) {
550 mach_timebase_info_data_t info;
551 (void)mach_timebase_info(&info);
552 scaling_factor = (double)info.numer / (double)info.denom;
553 }
554
555 return (double)time * scaling_factor / 1.0E9;
556 }
557
558 #elif defined(AMIGA)
559
560 #include <proto/timer.h>
561
562 GIT_INLINE(double) git__timer(void)
563 {
564 struct TimeVal tv;
565 ITimer->GetUpTime(&tv);
566 return (double)tv.Seconds + (double)tv.Microseconds / 1.0E6;
567 }
568
569 #else
570
571 #include <sys/time.h>
572
573 GIT_INLINE(double) git__timer(void)
574 {
575 struct timespec tp;
576
577 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
578 return (double) tp.tv_sec + (double) tp.tv_nsec / 1.0E9;
579 } else {
580 /* Fall back to using gettimeofday */
581 struct timeval tv;
582 struct timezone tz;
583 gettimeofday(&tv, &tz);
584 return (double)tv.tv_sec + (double)tv.tv_usec / 1.0E6;
585 }
586 }
587
588 #endif
589
590 #endif /* INCLUDE_util_h__ */