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__
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))))
16 # define min(a,b) ((a) < (b) ? (a) : (b))
19 # define max(a,b) ((a) > (b) ? (a) : (b))
23 * Custom memory allocation wrappers
24 * that set error code and error message
25 * on allocation failure
27 GIT_INLINE(void *) git__malloc(size_t len
)
29 void *ptr
= malloc(len
);
30 if (!ptr
) giterr_set_oom();
34 GIT_INLINE(void *) git__calloc(size_t nelem
, size_t elsize
)
36 void *ptr
= calloc(nelem
, elsize
);
37 if (!ptr
) giterr_set_oom();
41 GIT_INLINE(char *) git__strdup(const char *str
)
43 char *ptr
= strdup(str
);
44 if (!ptr
) giterr_set_oom();
48 GIT_INLINE(char *) git__strndup(const char *str
, size_t n
)
53 while (length
< n
&& str
[length
])
56 ptr
= (char*)git__malloc(length
+ 1);
62 memcpy(ptr
, str
, length
);
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
)
72 char *ptr
= (char*)git__malloc(n
+1);
73 memcpy(ptr
, start
, n
);
78 GIT_INLINE(void *) git__realloc(void *ptr
, size_t size
)
80 void *new_ptr
= realloc(ptr
, size
);
81 if (!new_ptr
) giterr_set_oom();
85 GIT_INLINE(void) git__free(void *ptr
)
90 #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
91 ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
93 #define CASESELECT(IGNORE_CASE, ICASE, CASE) \
94 ((IGNORE_CASE) ? (ICASE) : (CASE))
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
);
100 GIT_INLINE(int) git__signum(int val
)
102 return ((val
> 0) - (val
< 0));
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
);
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
);
111 /** @return true if p fits into the range of a size_t */
112 GIT_INLINE(int) git__is_sizet(git_off_t p
)
114 size_t r
= (size_t)p
;
115 return p
== (git_off_t
)r
;
118 /** @return true if p fits into the range of a uint32_t */
119 GIT_INLINE(int) git__is_uint32(size_t p
)
121 uint32_t r
= (uint32_t)p
;
122 return p
== (size_t)r
;
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))))
132 extern char *git__strtok(char **end
, const char *sep
);
133 extern char *git__strsep(char **end
, const char *sep
);
135 extern void git__strntolower(char *str
, size_t len
);
136 extern void git__strtolower(char *str
);
138 GIT_INLINE(const char *) git__next_line(const char *s
)
140 while (*s
&& *s
!= '\n') s
++;
141 while (*s
== '\n' || *s
== '\r') s
++;
145 GIT_INLINE(const void *) git__memrchr(const void *s
, int c
, size_t n
)
147 const unsigned char *cp
;
150 cp
= (unsigned char *)s
+ n
;
152 if (*(--cp
) == (unsigned char)c
)
160 typedef int (*git__tsort_cmp
)(const void *a
, const void *b
);
162 extern void git__tsort(void **dst
, size_t size
, git__tsort_cmp cmp
);
164 typedef int (*git__sort_r_cmp
)(const void *a
, const void *b
, void *payload
);
166 extern void git__tsort_r(
167 void **dst
, size_t size
, git__sort_r_cmp cmp
, void *payload
);
169 extern void git__qsort_r(
170 void *els
, size_t nel
, size_t elsize
, git__sort_r_cmp cmp
, void *payload
);
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
);
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
181 extern int git__bsearch(
185 int (*compare
)(const void *key
, const void *element
),
188 extern int git__bsearch_r(
192 int (*compare_r
)(const void *key
, const void *element
, void *payload
),
196 extern int git__strcmp_cb(const void *a
, const void *b
);
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
);
203 extern int git__strcasesort_cmp(const char *a
, const char *b
);
205 #include "thread-utils.h"
212 typedef void (*git_refcount_freeptr
)(void *r
);
214 #define GIT_REFCOUNT_INC(r) { \
215 git_atomic_inc(&((git_refcount *)(r))->refcount); \
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); } \
224 #define GIT_REFCOUNT_OWN(r, o) { \
225 ((git_refcount *)(r))->owner = o; \
228 #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
230 #define GIT_REFCOUNT_VAL(r) git_atomic_get(&((git_refcount *)(r))->refcount)
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 */
252 GIT_INLINE(int) git__fromhex(char h
)
254 return from_hex
[(unsigned char) h
];
257 GIT_INLINE(int) git__ishex(const char *str
)
260 for (i
=0; i
<strlen(str
); i
++)
261 if (git__fromhex(str
[i
]) < 0)
266 GIT_INLINE(size_t) git__size_t_bitmask(size_t v
)
278 GIT_INLINE(size_t) git__size_t_powerof2(size_t v
)
280 return git__size_t_bitmask(v
) + 1;
283 GIT_INLINE(bool) git__isupper(int c
)
285 return (c
>= 'A' && c
<= 'Z');
288 GIT_INLINE(bool) git__isalpha(int c
)
290 return ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z'));
293 GIT_INLINE(bool) git__isdigit(int c
)
295 return (c
>= '0' && c
<= '9');
298 GIT_INLINE(bool) git__isspace(int c
)
300 return (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\f' || c
== '\r' || c
== '\v' || c
== 0x85 /* Unicode CR+LF */);
303 GIT_INLINE(bool) git__isspace_nonlf(int c
)
305 return (c
== ' ' || c
== '\t' || c
== '\f' || c
== '\r' || c
== '\v' || c
== 0x85 /* Unicode CR+LF */);
308 GIT_INLINE(bool) git__iswildcard(int c
)
310 return (c
== '*' || c
== '?' || c
== '[');
314 * Parse a string value as a boolean, just like Core Git does.
316 * Valid values for true are: 'true', 'yes', 'on'
317 * Valid values for false are: 'false', 'no', 'off'
319 extern int git__parse_bool(int *out
, const char *value
);
322 * Parse a string into a value as a git_time_t.
324 * Sample valid input:
327 * - "2003-7-17 08:23"
329 extern int git__date_parse(git_time_t
*out
, const char *date
);
332 * Unescapes a string in-place.
334 * Edge cases behavior:
335 * - "jackie\" -> "jacky\"
336 * - "chan\\" -> "chan\"
338 extern size_t git__unescape(char *str
);
341 * Safely zero-out memory, making sure that the compiler
342 * doesn't optimize away the operation.
344 GIT_INLINE(void) git__memzero(void *data
, size_t size
)
347 SecureZeroMemory((PVOID
)data
, size
);
349 volatile uint8_t *scan
= (volatile uint8_t *)data
;
358 GIT_INLINE(double) git__timer(void)
360 /* We need the initial tick count to detect if the tick
361 * count has rolled over. */
362 static DWORD initial_tick_count
= 0;
364 /* GetTickCount returns the number of milliseconds that have
365 * elapsed since the system was started. */
366 DWORD count
= GetTickCount();
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
;
375 return (double) count
/ (double) 1000;
380 #include <mach/mach_time.h>
382 double git__timer(void)
384 uint64_t time
= mach_absolute_time();
385 static double scaling_factor
= 0;
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
;
393 return (double)time
* scaling_factor
/ 1.0E-9;
398 #include <sys/time.h>
400 GIT_INLINE(double) git__timer(void)
404 if (clock_gettime(CLOCK_MONOTONIC
, &tp
) == 0) {
405 return (double) tp
.tv_sec
+ (double) tp
.tv_nsec
/ 1E-9;
407 /* Fall back to using gettimeofday */
410 gettimeofday(&tv
, &tz
);
411 return (double)tv
.tv_sec
+ (double)tv
.tv_usec
/ 1E-6;
417 #endif /* INCLUDE_util_h__ */