]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Add util.h - utility macros
[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
6 /*
7 * Realloc the buffer pointed at by variable 'x' so that it can hold
8 * at least 'nr' entries; the number of entries currently allocated
9 * is 'alloc', using the standard growing factor alloc_nr() macro.
10 *
11 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
12 */
13 #define alloc_nr(x) (((x)+16)*3/2)
14 #define ALLOC_GROW(x, nr, alloc) \
15 do { \
16 if ((nr) > alloc) { \
17 if (alloc_nr(alloc) < (nr)) \
18 alloc = (nr); \
19 else \
20 alloc = alloc_nr(alloc); \
21 x = xrealloc((x), alloc * sizeof(*(x))); \
22 } \
23 } while(0)
24
25 #endif /* INCLUDE_util_h__ */