]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Add auxiliary method git__hexdump
[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 extern void *git__malloc(size_t);
7 extern void *git__calloc(size_t, size_t);
8 extern char *git__strdup(const char *);
9
10 #ifndef GIT__NO_HIDE_MALLOC
11 # define GIT__FORBID_MALLOC do_not_use_malloc_directly
12
13 # ifdef malloc
14 # undef malloc
15 # endif
16 # define malloc(a) GIT__FORBID_MALLOC
17
18 # ifdef calloc
19 # undef calloc
20 # endif
21 # define calloc(a,b) GIT__FORBID_MALLOC
22
23 # ifdef strdup
24 # undef strdup
25 # endif
26 # define strdup(a) GIT__FORBID_MALLOC
27 #endif
28
29 extern int git__fmt(char *, size_t, const char *, ...)
30 GIT_FORMAT_PRINTF(3, 4);
31 extern int git__prefixcmp(const char *str, const char *prefix);
32 extern int git__suffixcmp(const char *str, const char *suffix);
33
34 extern int git__dirname(char *dir, size_t n, char *path);
35 extern int git__basename(char *base, size_t n, char *path);
36
37 extern void git__hexdump(const char *buffer, size_t n);
38
39 /** @return true if p fits into the range of a size_t */
40 GIT_INLINE(int) git__is_sizet(off_t p)
41 {
42 size_t r = (size_t)p;
43 return p == (off_t)r;
44 }
45
46 /*
47 * Realloc the buffer pointed at by variable 'x' so that it can hold
48 * at least 'nr' entries; the number of entries currently allocated
49 * is 'alloc', using the standard growing factor alloc_nr() macro.
50 *
51 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
52 */
53 #define alloc_nr(x) (((x)+16)*3/2)
54 #define ALLOC_GROW(x, nr, alloc) \
55 do { \
56 if ((nr) > alloc) { \
57 if (alloc_nr(alloc) < (nr)) \
58 alloc = (nr); \
59 else \
60 alloc = alloc_nr(alloc); \
61 x = xrealloc((x), alloc * sizeof(*(x))); \
62 } \
63 } while (0)
64
65 #endif /* INCLUDE_util_h__ */