2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_util_h__
8 #define INCLUDE_util_h__
13 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
14 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
15 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
17 # define min(a,b) ((a) < (b) ? (a) : (b))
20 # define max(a,b) ((a) > (b) ? (a) : (b))
23 #define GIT_DATE_RFC2822_SZ 32
26 * Return the length of a constant string.
27 * We are aware that `strlen` performs the same task and is usually
28 * optimized away by the compiler, whilst being safer because it returns
29 * valid values when passed a pointer instead of a constant string; however
30 * this macro will transparently work with wide-char and single-char strings.
32 #define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
35 * Custom memory allocation wrappers
36 * that set error code and error message
37 * on allocation failure
39 GIT_INLINE(void *) git__malloc(size_t len
)
41 void *ptr
= malloc(len
);
42 if (!ptr
) giterr_set_oom();
46 GIT_INLINE(void *) git__calloc(size_t nelem
, size_t elsize
)
48 void *ptr
= calloc(nelem
, elsize
);
49 if (!ptr
) giterr_set_oom();
53 GIT_INLINE(char *) git__strdup(const char *str
)
55 char *ptr
= strdup(str
);
56 if (!ptr
) giterr_set_oom();
60 GIT_INLINE(char *) git__strndup(const char *str
, size_t n
)
65 length
= p_strnlen(str
, n
);
67 ptr
= (char*)git__malloc(length
+ 1);
73 memcpy(ptr
, str
, length
);
80 /* NOTE: This doesn't do null or '\0' checking. Watch those boundaries! */
81 GIT_INLINE(char *) git__substrdup(const char *start
, size_t n
)
83 char *ptr
= (char*)git__malloc(n
+1);
84 memcpy(ptr
, start
, n
);
89 GIT_INLINE(void *) git__realloc(void *ptr
, size_t size
)
91 void *new_ptr
= realloc(ptr
, size
);
92 if (!new_ptr
) giterr_set_oom();
96 GIT_INLINE(void) git__free(void *ptr
)
101 #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
102 ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
104 #define CASESELECT(IGNORE_CASE, ICASE, CASE) \
105 ((IGNORE_CASE) ? (ICASE) : (CASE))
107 extern int git__prefixcmp(const char *str
, const char *prefix
);
108 extern int git__prefixcmp_icase(const char *str
, const char *prefix
);
109 extern int git__suffixcmp(const char *str
, const char *suffix
);
111 GIT_INLINE(int) git__signum(int val
)
113 return ((val
> 0) - (val
< 0));
116 extern int git__strtol32(int32_t *n
, const char *buff
, const char **end_buf
, int base
);
117 extern int git__strtol64(int64_t *n
, const char *buff
, const char **end_buf
, int base
);
119 extern void git__hexdump(const char *buffer
, size_t n
);
120 extern uint32_t git__hash(const void *key
, int len
, uint32_t seed
);
122 /** @return true if p fits into the range of a size_t */
123 GIT_INLINE(int) git__is_sizet(git_off_t p
)
125 size_t r
= (size_t)p
;
126 return p
== (git_off_t
)r
;
129 /** @return true if p fits into the range of a uint32_t */
130 GIT_INLINE(int) git__is_uint32(size_t p
)
132 uint32_t r
= (uint32_t)p
;
133 return p
== (size_t)r
;
136 /** @return true if p fits into the range of an unsigned long */
137 GIT_INLINE(int) git__is_ulong(git_off_t p
)
139 unsigned long r
= (unsigned long)p
;
140 return p
== (git_off_t
)r
;
143 /* 32-bit cross-platform rotl */
144 #ifdef _MSC_VER /* use built-in method in MSVC */
145 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
146 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
147 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
150 extern char *git__strtok(char **end
, const char *sep
);
151 extern char *git__strsep(char **end
, const char *sep
);
153 extern void git__strntolower(char *str
, size_t len
);
154 extern void git__strtolower(char *str
);
156 GIT_INLINE(const char *) git__next_line(const char *s
)
158 while (*s
&& *s
!= '\n') s
++;
159 while (*s
== '\n' || *s
== '\r') s
++;
163 GIT_INLINE(const void *) git__memrchr(const void *s
, int c
, size_t n
)
165 const unsigned char *cp
;
168 cp
= (unsigned char *)s
+ n
;
170 if (*(--cp
) == (unsigned char)c
)
178 typedef int (*git__tsort_cmp
)(const void *a
, const void *b
);
180 extern void git__tsort(void **dst
, size_t size
, git__tsort_cmp cmp
);
182 typedef int (*git__sort_r_cmp
)(const void *a
, const void *b
, void *payload
);
184 extern void git__tsort_r(
185 void **dst
, size_t size
, git__sort_r_cmp cmp
, void *payload
);
187 extern void git__qsort_r(
188 void *els
, size_t nel
, size_t elsize
, git__sort_r_cmp cmp
, void *payload
);
190 extern void git__insertsort_r(
191 void *els
, size_t nel
, size_t elsize
, void *swapel
,
192 git__sort_r_cmp cmp
, void *payload
);
195 * @param position If non-NULL, this will be set to the position where the
196 * element is or would be inserted if not found.
197 * @return 0 if found; GIT_ENOTFOUND if not found
199 extern int git__bsearch(
203 int (*compare
)(const void *key
, const void *element
),
206 extern int git__bsearch_r(
210 int (*compare_r
)(const void *key
, const void *element
, void *payload
),
214 extern int git__strcmp_cb(const void *a
, const void *b
);
215 extern int git__strcasecmp_cb(const void *a
, const void *b
);
217 extern int git__strcmp(const char *a
, const char *b
);
218 extern int git__strcasecmp(const char *a
, const char *b
);
219 extern int git__strncmp(const char *a
, const char *b
, size_t sz
);
220 extern int git__strncasecmp(const char *a
, const char *b
, size_t sz
);
222 extern int git__strcasesort_cmp(const char *a
, const char *b
);
224 #include "thread-utils.h"
231 typedef void (*git_refcount_freeptr
)(void *r
);
233 #define GIT_REFCOUNT_INC(r) { \
234 git_atomic_inc(&((git_refcount *)(r))->refcount); \
237 #define GIT_REFCOUNT_DEC(_r, do_free) { \
238 git_refcount *r = (git_refcount *)(_r); \
239 int val = git_atomic_dec(&r->refcount); \
240 if (val <= 0 && r->owner == NULL) { do_free(_r); } \
243 #define GIT_REFCOUNT_OWN(r, o) { \
244 ((git_refcount *)(r))->owner = o; \
247 #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
249 #define GIT_REFCOUNT_VAL(r) git_atomic_get(&((git_refcount *)(r))->refcount)
252 static signed char from_hex
[] = {
253 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
254 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
255 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
256 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
257 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
258 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
259 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
260 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
261 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
262 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
263 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
264 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
265 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
266 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
267 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
268 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
271 GIT_INLINE(int) git__fromhex(char h
)
273 return from_hex
[(unsigned char) h
];
276 GIT_INLINE(int) git__ishex(const char *str
)
279 for (i
=0; i
<strlen(str
); i
++)
280 if (git__fromhex(str
[i
]) < 0)
285 GIT_INLINE(size_t) git__size_t_bitmask(size_t v
)
297 GIT_INLINE(size_t) git__size_t_powerof2(size_t v
)
299 return git__size_t_bitmask(v
) + 1;
302 GIT_INLINE(bool) git__isupper(int c
)
304 return (c
>= 'A' && c
<= 'Z');
307 GIT_INLINE(bool) git__isalpha(int c
)
309 return ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z'));
312 GIT_INLINE(bool) git__isdigit(int c
)
314 return (c
>= '0' && c
<= '9');
317 GIT_INLINE(bool) git__isspace(int c
)
319 return (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\f' || c
== '\r' || c
== '\v' || c
== 0x85 /* Unicode CR+LF */);
322 GIT_INLINE(bool) git__isspace_nonlf(int c
)
324 return (c
== ' ' || c
== '\t' || c
== '\f' || c
== '\r' || c
== '\v' || c
== 0x85 /* Unicode CR+LF */);
327 GIT_INLINE(bool) git__iswildcard(int c
)
329 return (c
== '*' || c
== '?' || c
== '[');
333 * Parse a string value as a boolean, just like Core Git does.
335 * Valid values for true are: 'true', 'yes', 'on'
336 * Valid values for false are: 'false', 'no', 'off'
338 extern int git__parse_bool(int *out
, const char *value
);
341 * Parse a string into a value as a git_time_t.
343 * Sample valid input:
346 * - "2003-7-17 08:23"
348 extern int git__date_parse(git_time_t
*out
, const char *date
);
351 * Format a git_time as a RFC2822 string
353 * @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
354 * @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
355 * @param date the date to be formatted
356 * @return 0 if successful; -1 on error
358 extern int git__date_rfc2822_fmt(char *out
, size_t len
, const git_time
*date
);
361 * Unescapes a string in-place.
363 * Edge cases behavior:
364 * - "jackie\" -> "jacky\"
365 * - "chan\\" -> "chan\"
367 extern size_t git__unescape(char *str
);
370 * Safely zero-out memory, making sure that the compiler
371 * doesn't optimize away the operation.
373 GIT_INLINE(void) git__memzero(void *data
, size_t size
)
376 SecureZeroMemory((PVOID
)data
, size
);
378 volatile uint8_t *scan
= (volatile uint8_t *)data
;
387 GIT_INLINE(double) git__timer(void)
389 /* We need the initial tick count to detect if the tick
390 * count has rolled over. */
391 static DWORD initial_tick_count
= 0;
393 /* GetTickCount returns the number of milliseconds that have
394 * elapsed since the system was started. */
395 DWORD count
= GetTickCount();
397 if(initial_tick_count
== 0) {
398 initial_tick_count
= count
;
399 } else if (count
< initial_tick_count
) {
400 /* The tick count has rolled over - adjust for it. */
401 count
= (0xFFFFFFFF - initial_tick_count
) + count
;
404 return (double) count
/ (double) 1000;
409 #include <mach/mach_time.h>
411 GIT_INLINE(double) git__timer(void)
413 uint64_t time
= mach_absolute_time();
414 static double scaling_factor
= 0;
416 if (scaling_factor
== 0) {
417 mach_timebase_info_data_t info
;
418 (void)mach_timebase_info(&info
);
419 scaling_factor
= (double)info
.numer
/ (double)info
.denom
;
422 return (double)time
* scaling_factor
/ 1.0E9
;
427 #include <sys/time.h>
429 GIT_INLINE(double) git__timer(void)
433 if (clock_gettime(CLOCK_MONOTONIC
, &tp
) == 0) {
434 return (double) tp
.tv_sec
+ (double) tp
.tv_nsec
/ 1.0E9
;
436 /* Fall back to using gettimeofday */
439 gettimeofday(&tv
, &tz
);
440 return (double)tv
.tv_sec
+ (double)tv
.tv_usec
/ 1.0E6
;
446 #endif /* INCLUDE_util_h__ */