]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Update Copyright header
[libgit2.git] / src / util.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
11 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
12 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
13 #ifndef min
14 # define min(a,b) ((a) < (b) ? (a) : (b))
15 #endif
16
17 /*
18 * Custom memory allocation wrappers
19 * that set error code and error message
20 * on allocation failure
21 */
22 GIT_INLINE(void *) git__malloc(size_t len)
23 {
24 void *ptr = malloc(len);
25 if (!ptr)
26 git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)len);
27 return ptr;
28 }
29
30 GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
31 {
32 void *ptr = calloc(nelem, elsize);
33 if (!ptr)
34 git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)elsize);
35 return ptr;
36 }
37
38 GIT_INLINE(char *) git__strdup(const char *str)
39 {
40 char *ptr = strdup(str);
41 if (!ptr)
42 git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
43 return ptr;
44 }
45
46 GIT_INLINE(char *) git__strndup(const char *str, size_t n)
47 {
48 size_t length;
49 char *ptr;
50
51 length = strlen(str);
52 if (n < length)
53 length = n;
54
55 ptr = (char*)malloc(length + 1);
56 if (!ptr) {
57 git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
58 return NULL;
59 }
60
61 memcpy(ptr, str, length);
62 ptr[length] = '\0';
63
64 return ptr;
65 }
66
67 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
68 {
69 void *new_ptr = realloc(ptr, size);
70 if (!new_ptr)
71 git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)size);
72 return new_ptr;
73 }
74
75 #define git__free(ptr) free(ptr)
76
77 extern int git__prefixcmp(const char *str, const char *prefix);
78 extern int git__suffixcmp(const char *str, const char *suffix);
79
80 extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
81 extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
82
83 extern void git__hexdump(const char *buffer, size_t n);
84 extern uint32_t git__hash(const void *key, int len, uint32_t seed);
85
86 /** @return true if p fits into the range of a size_t */
87 GIT_INLINE(int) git__is_sizet(git_off_t p)
88 {
89 size_t r = (size_t)p;
90 return p == (git_off_t)r;
91 }
92
93 /* 32-bit cross-platform rotl */
94 #ifdef _MSC_VER /* use built-in method in MSVC */
95 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
96 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
97 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
98 #endif
99
100 extern char *git__strtok(char **end, const char *sep);
101
102 extern void git__strntolower(char *str, size_t len);
103 extern void git__strtolower(char *str);
104
105 GIT_INLINE(const char *) git__next_line(const char *s)
106 {
107 while (*s && *s != '\n') s++;
108 while (*s == '\n' || *s == '\r') s++;
109 return s;
110 }
111
112 extern int git__fnmatch(const char *pattern, const char *name, int flags);
113
114 extern void git__tsort(void **dst, size_t size, int (*cmp)(const void *, const void *));
115
116 extern int git__bsearch(
117 void **array,
118 size_t array_len,
119 const void *key,
120 int (*compare)(const void *, const void *),
121 size_t *position);
122
123 extern int git__strcmp_cb(const void *a, const void *b);
124
125 typedef struct {
126 short refcount;
127 void *owner;
128 } git_refcount;
129
130 typedef void (*git_refcount_freeptr)(void *r);
131
132 #define GIT_REFCOUNT_INC(r) { \
133 ((git_refcount *)(r))->refcount++; \
134 }
135
136 #define GIT_REFCOUNT_DEC(_r, do_free) { \
137 git_refcount *r = (git_refcount *)(_r); \
138 r->refcount--; \
139 if (r->refcount <= 0 && r->owner == NULL) { do_free(_r); } \
140 }
141
142 #define GIT_REFCOUNT_OWN(r, o) { \
143 ((git_refcount *)(r))->owner = o; \
144 }
145
146 #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
147
148 static signed char from_hex[] = {
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
150 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
151 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
152 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
153 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
154 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
155 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
156 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
157 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
158 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
159 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
160 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
161 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
162 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
163 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
164 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
165 };
166
167 GIT_INLINE(int) git__fromhex(char h)
168 {
169 return from_hex[(unsigned char) h];
170 }
171
172 #endif /* INCLUDE_util_h__ */