]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
5e0de328 | 2 | * Copyright (C) 2009-2012 the libgit2 contributors |
bb742ede VM |
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 | */ | |
3a2aabdc AE |
7 | #ifndef INCLUDE_util_h__ |
8 | #define INCLUDE_util_h__ | |
9 | ||
10 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) | |
87d9869f | 11 | #define bitsizeof(x) (CHAR_BIT * sizeof(x)) |
58d06cf1 | 12 | #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits)))) |
8b9e8de5 CMN |
13 | #ifndef min |
14 | # define min(a,b) ((a) < (b) ? (a) : (b)) | |
15 | #endif | |
3a2aabdc | 16 | |
932d1baf | 17 | /* |
3f53c971 VM |
18 | * Custom memory allocation wrappers |
19 | * that set error code and error message | |
20 | * on allocation failure | |
9f54fe48 | 21 | */ |
3f53c971 VM |
22 | GIT_INLINE(void *) git__malloc(size_t len) |
23 | { | |
24 | void *ptr = malloc(len); | |
dda708e7 | 25 | if (!ptr) giterr_set_oom(); |
3f53c971 VM |
26 | return ptr; |
27 | } | |
28 | ||
29 | GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize) | |
30 | { | |
31 | void *ptr = calloc(nelem, elsize); | |
dda708e7 | 32 | if (!ptr) giterr_set_oom(); |
3f53c971 VM |
33 | return ptr; |
34 | } | |
35 | ||
36 | GIT_INLINE(char *) git__strdup(const char *str) | |
37 | { | |
38 | char *ptr = strdup(str); | |
dda708e7 | 39 | if (!ptr) giterr_set_oom(); |
3f53c971 VM |
40 | return ptr; |
41 | } | |
42 | ||
6adcb5f3 VM |
43 | GIT_INLINE(char *) git__strndup(const char *str, size_t n) |
44 | { | |
45 | size_t length; | |
46 | char *ptr; | |
47 | ||
48 | length = strlen(str); | |
49 | if (n < length) | |
50 | length = n; | |
51 | ||
f0619886 | 52 | ptr = (char*)malloc(length + 1); |
31e59092 | 53 | if (!ptr) { |
dda708e7 | 54 | giterr_set_oom(); |
31e59092 MS |
55 | return NULL; |
56 | } | |
6adcb5f3 VM |
57 | |
58 | memcpy(ptr, str, length); | |
31e59092 | 59 | ptr[length] = '\0'; |
6adcb5f3 VM |
60 | |
61 | return ptr; | |
62 | } | |
63 | ||
3f53c971 VM |
64 | GIT_INLINE(void *) git__realloc(void *ptr, size_t size) |
65 | { | |
66 | void *new_ptr = realloc(ptr, size); | |
dda708e7 | 67 | if (!new_ptr) giterr_set_oom(); |
3f53c971 VM |
68 | return new_ptr; |
69 | } | |
64a47c01 | 70 | |
3286c408 VM |
71 | #define git__free(ptr) free(ptr) |
72 | ||
e035685f VM |
73 | extern int git__prefixcmp(const char *str, const char *prefix); |
74 | extern int git__suffixcmp(const char *str, const char *suffix); | |
9eb79764 | 75 | |
fafd4710 VM |
76 | extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base); |
77 | extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base); | |
c6e65aca | 78 | |
e035685f | 79 | extern void git__hexdump(const char *buffer, size_t n); |
e0646b38 VM |
80 | extern uint32_t git__hash(const void *key, int len, uint32_t seed); |
81 | ||
a7c60cfc | 82 | /** @return true if p fits into the range of a size_t */ |
f0bde7fa | 83 | GIT_INLINE(int) git__is_sizet(git_off_t p) |
a7c60cfc SP |
84 | { |
85 | size_t r = (size_t)p; | |
f0bde7fa | 86 | return p == (git_off_t)r; |
a7c60cfc SP |
87 | } |
88 | ||
e0646b38 VM |
89 | /* 32-bit cross-platform rotl */ |
90 | #ifdef _MSC_VER /* use built-in method in MSVC */ | |
91 | # define git__rotl(v, s) (uint32_t)_rotl(v, s) | |
92 | #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */ | |
93 | # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s)))) | |
94 | #endif | |
95 | ||
0291b5b7 | 96 | extern char *git__strtok(char **end, const char *sep); |
f725931b | 97 | |
26e74c6a | 98 | extern void git__strntolower(char *str, size_t len); |
0da2c700 VM |
99 | extern void git__strtolower(char *str); |
100 | ||
df743c7d RB |
101 | GIT_INLINE(const char *) git__next_line(const char *s) |
102 | { | |
103 | while (*s && *s != '\n') s++; | |
cfbc880d | 104 | while (*s == '\n' || *s == '\r') s++; |
df743c7d RB |
105 | return s; |
106 | } | |
107 | ||
63f91e1c CMN |
108 | extern int git__fnmatch(const char *pattern, const char *name, int flags); |
109 | ||
bdcc4611 | 110 | extern void git__tsort(void **dst, size_t size, int (*cmp)(const void *, const void *)); |
bd370b14 | 111 | |
ae9e29fd RB |
112 | /** |
113 | * @param position If non-NULL, this will be set to the position where the | |
114 | * element is or would be inserted if not found. | |
115 | * @return pos (>=0) if found or -1 if not found | |
116 | */ | |
bd370b14 RB |
117 | extern int git__bsearch( |
118 | void **array, | |
119 | size_t array_len, | |
120 | const void *key, | |
121 | int (*compare)(const void *, const void *), | |
122 | size_t *position); | |
c20ffa61 | 123 | |
d1f34693 | 124 | extern int git__strcmp_cb(const void *a, const void *b); |
125 | ||
9462c471 VM |
126 | typedef struct { |
127 | short refcount; | |
128 | void *owner; | |
129 | } git_refcount; | |
130 | ||
131 | typedef void (*git_refcount_freeptr)(void *r); | |
132 | ||
133 | #define GIT_REFCOUNT_INC(r) { \ | |
134 | ((git_refcount *)(r))->refcount++; \ | |
135 | } | |
136 | ||
137 | #define GIT_REFCOUNT_DEC(_r, do_free) { \ | |
138 | git_refcount *r = (git_refcount *)(_r); \ | |
139 | r->refcount--; \ | |
03da4480 | 140 | if (r->refcount <= 0 && r->owner == NULL) { do_free(_r); } \ |
9462c471 VM |
141 | } |
142 | ||
143 | #define GIT_REFCOUNT_OWN(r, o) { \ | |
144 | ((git_refcount *)(r))->owner = o; \ | |
145 | } | |
146 | ||
147 | #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner) | |
148 | ||
eb8de747 | 149 | static signed char from_hex[] = { |
150 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */ | |
151 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */ | |
152 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */ | |
153 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */ | |
154 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */ | |
155 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */ | |
156 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */ | |
157 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */ | |
158 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */ | |
159 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */ | |
160 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */ | |
161 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */ | |
162 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */ | |
163 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */ | |
164 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */ | |
165 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */ | |
166 | }; | |
167 | ||
168 | GIT_INLINE(int) git__fromhex(char h) | |
169 | { | |
170 | return from_hex[(unsigned char) h]; | |
171 | } | |
9462c471 | 172 | |
905919e6 MS |
173 | GIT_INLINE(int) git__ishex(const char *str) |
174 | { | |
175 | unsigned i; | |
176 | for (i=0; i<strlen(str); i++) | |
177 | if (git__fromhex(str[i]) < 0) | |
178 | return 0; | |
179 | return 1; | |
180 | } | |
181 | ||
3a2aabdc | 182 | #endif /* INCLUDE_util_h__ */ |