]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Merge pull request #306 from carlosmn/development
[libgit2.git] / src / util.h
1 #ifndef INCLUDE_util_h__
2 #define INCLUDE_util_h__
3
4 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
5 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
6 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
7 #ifndef min
8 # define min(a,b) ((a) < (b) ? (a) : (b))
9 #endif
10
11 /*
12 * Custom memory allocation wrappers
13 * that set error code and error message
14 * on allocation failure
15 */
16 GIT_INLINE(void *) git__malloc(size_t len)
17 {
18 void *ptr = malloc(len);
19 if (!ptr)
20 git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)len);
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)
28 git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)elsize);
29 return ptr;
30 }
31
32 GIT_INLINE(char *) git__strdup(const char *str)
33 {
34 char *ptr = strdup(str);
35 if (!ptr)
36 git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
37 return ptr;
38 }
39
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
49 ptr = (char*)malloc(length + 1);
50 if (!ptr)
51 git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
52
53 memcpy(ptr, str, length);
54 ptr[length] = 0;
55
56 return ptr;
57 }
58
59 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
60 {
61 void *new_ptr = realloc(ptr, size);
62 if (!new_ptr)
63 git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)size);
64 return new_ptr;
65 }
66
67 extern int git__fmt(char *, size_t, const char *, ...)
68 GIT_FORMAT_PRINTF(3, 4);
69 extern int git__prefixcmp(const char *str, const char *prefix);
70 extern int git__suffixcmp(const char *str, const char *suffix);
71
72 extern int git__strtol32(long *n, const char *buff, const char **end_buf, int base);
73
74 extern void git__hexdump(const char *buffer, size_t n);
75 extern uint32_t git__hash(const void *key, int len, uint32_t seed);
76
77 /** @return true if p fits into the range of a size_t */
78 GIT_INLINE(int) git__is_sizet(git_off_t p)
79 {
80 size_t r = (size_t)p;
81 return p == (git_off_t)r;
82 }
83
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
91 extern char *git__strtok(char **end, const char *sep);
92
93 extern void git__strntolower(char *str, int len);
94 extern void git__strtolower(char *str);
95
96 #define STRLEN(str) (sizeof(str) - 1)
97
98 #define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1)
99
100 extern int git__fnmatch(const char *pattern, const char *name, int flags);
101
102 /*
103 * Realloc the buffer pointed at by variable 'x' so that it can hold
104 * at least 'nr' entries; the number of entries currently allocated
105 * is 'alloc', using the standard growing factor alloc_nr() macro.
106 *
107 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
108 */
109 #define alloc_nr(x) (((x)+16)*3/2)
110 #define ALLOC_GROW(x, nr, alloc) \
111 do { \
112 if ((nr) > alloc) { \
113 if (alloc_nr(alloc) < (nr)) \
114 alloc = (nr); \
115 else \
116 alloc = alloc_nr(alloc); \
117 x = xrealloc((x), alloc * sizeof(*(x))); \
118 } \
119 } while (0)
120
121 extern int git__msort(void *b, size_t n, size_t s,
122 int (*cmp)(const void *, const void *));
123
124 #endif /* INCLUDE_util_h__ */