]>
Commit | Line | Data |
---|---|---|
3a2aabdc AE |
1 | #ifndef INCLUDE_util_h__ |
2 | #define INCLUDE_util_h__ | |
3 | ||
4 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) | |
58d06cf1 VM |
5 | #define bitsizeof(x) (CHAR_BIT * sizeof(x)) |
6 | #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits)))) | |
8b9e8de5 CMN |
7 | #ifndef min |
8 | # define min(a,b) ((a) < (b) ? (a) : (b)) | |
9 | #endif | |
3a2aabdc | 10 | |
932d1baf | 11 | /* |
3f53c971 VM |
12 | * Custom memory allocation wrappers |
13 | * that set error code and error message | |
14 | * on allocation failure | |
9f54fe48 | 15 | */ |
3f53c971 VM |
16 | GIT_INLINE(void *) git__malloc(size_t len) |
17 | { | |
18 | void *ptr = malloc(len); | |
19 | if (!ptr) | |
fa59f18d | 20 | git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)len); |
3f53c971 VM |
21 | return ptr; |
22 | } | |
23 | ||
24 | GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize) | |
25 | { | |
26 | void *ptr = calloc(nelem, elsize); | |
27 | if (!ptr) | |
fa59f18d | 28 | git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)elsize); |
3f53c971 VM |
29 | return ptr; |
30 | } | |
31 | ||
32 | GIT_INLINE(char *) git__strdup(const char *str) | |
33 | { | |
34 | char *ptr = strdup(str); | |
35 | if (!ptr) | |
fa59f18d | 36 | git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string"); |
3f53c971 VM |
37 | return ptr; |
38 | } | |
39 | ||
6adcb5f3 VM |
40 | GIT_INLINE(char *) git__strndup(const char *str, size_t n) |
41 | { | |
42 | size_t length; | |
43 | char *ptr; | |
44 | ||
45 | length = strlen(str); | |
46 | if (n < length) | |
47 | length = n; | |
48 | ||
f0619886 | 49 | ptr = (char*)malloc(length + 1); |
31e59092 | 50 | if (!ptr) { |
6adcb5f3 | 51 | git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string"); |
31e59092 MS |
52 | return NULL; |
53 | } | |
6adcb5f3 VM |
54 | |
55 | memcpy(ptr, str, length); | |
31e59092 | 56 | ptr[length] = '\0'; |
6adcb5f3 VM |
57 | |
58 | return ptr; | |
59 | } | |
60 | ||
3f53c971 VM |
61 | GIT_INLINE(void *) git__realloc(void *ptr, size_t size) |
62 | { | |
63 | void *new_ptr = realloc(ptr, size); | |
64 | if (!new_ptr) | |
fa59f18d | 65 | git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)size); |
3f53c971 VM |
66 | return new_ptr; |
67 | } | |
64a47c01 | 68 | |
e035685f VM |
69 | extern int git__prefixcmp(const char *str, const char *prefix); |
70 | extern int git__suffixcmp(const char *str, const char *suffix); | |
9eb79764 | 71 | |
c6e65aca VM |
72 | extern int git__strtol32(long *n, const char *buff, const char **end_buf, int base); |
73 | ||
e035685f | 74 | extern void git__hexdump(const char *buffer, size_t n); |
e0646b38 VM |
75 | extern uint32_t git__hash(const void *key, int len, uint32_t seed); |
76 | ||
a7c60cfc | 77 | /** @return true if p fits into the range of a size_t */ |
f0bde7fa | 78 | GIT_INLINE(int) git__is_sizet(git_off_t p) |
a7c60cfc SP |
79 | { |
80 | size_t r = (size_t)p; | |
f0bde7fa | 81 | return p == (git_off_t)r; |
a7c60cfc SP |
82 | } |
83 | ||
e0646b38 VM |
84 | /* 32-bit cross-platform rotl */ |
85 | #ifdef _MSC_VER /* use built-in method in MSVC */ | |
86 | # define git__rotl(v, s) (uint32_t)_rotl(v, s) | |
87 | #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */ | |
88 | # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s)))) | |
89 | #endif | |
90 | ||
0291b5b7 | 91 | extern char *git__strtok(char **end, const char *sep); |
f725931b | 92 | |
26e74c6a | 93 | extern void git__strntolower(char *str, size_t len); |
0da2c700 VM |
94 | extern void git__strtolower(char *str); |
95 | ||
63f91e1c CMN |
96 | extern int git__fnmatch(const char *pattern, const char *name, int flags); |
97 | ||
3a2aabdc AE |
98 | /* |
99 | * Realloc the buffer pointed at by variable 'x' so that it can hold | |
100 | * at least 'nr' entries; the number of entries currently allocated | |
101 | * is 'alloc', using the standard growing factor alloc_nr() macro. | |
102 | * | |
103 | * DO NOT USE any expression with side-effect for 'x' or 'alloc'. | |
104 | */ | |
105 | #define alloc_nr(x) (((x)+16)*3/2) | |
106 | #define ALLOC_GROW(x, nr, alloc) \ | |
107 | do { \ | |
108 | if ((nr) > alloc) { \ | |
109 | if (alloc_nr(alloc) < (nr)) \ | |
110 | alloc = (nr); \ | |
111 | else \ | |
112 | alloc = alloc_nr(alloc); \ | |
113 | x = xrealloc((x), alloc * sizeof(*(x))); \ | |
114 | } \ | |
1e5dd572 | 115 | } while (0) |
3a2aabdc | 116 | |
bdcc4611 | 117 | extern void git__tsort(void **dst, size_t size, int (*cmp)(const void *, const void *)); |
de18f276 VM |
118 | extern void **git__bsearch(const void *key, void **base, size_t nmemb, |
119 | int (*compar)(const void *, const void *)); | |
c20ffa61 | 120 | |
d1f34693 | 121 | extern int git__strcmp_cb(const void *a, const void *b); |
122 | ||
3a2aabdc | 123 | #endif /* INCLUDE_util_h__ */ |