]>
git.proxmox.com Git - libgit2.git/blob - src/util.h
1 #ifndef INCLUDE_util_h__
2 #define INCLUDE_util_h__
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))))
8 # define min(a,b) ((a) < (b) ? (a) : (b))
12 * Custom memory allocation wrappers
13 * that set error code and error message
14 * on allocation failure
16 GIT_INLINE(void *) git__malloc(size_t len
)
18 void *ptr
= malloc(len
);
20 git__throw(GIT_ENOMEM
, "Out of memory. Failed to allocate %d bytes.", (int)len
);
24 GIT_INLINE(void *) git__calloc(size_t nelem
, size_t elsize
)
26 void *ptr
= calloc(nelem
, elsize
);
28 git__throw(GIT_ENOMEM
, "Out of memory. Failed to allocate %d bytes.", (int)elsize
);
32 GIT_INLINE(char *) git__strdup(const char *str
)
34 char *ptr
= strdup(str
);
36 git__throw(GIT_ENOMEM
, "Out of memory. Failed to duplicate string");
40 GIT_INLINE(char *) git__strndup(const char *str
, size_t n
)
49 ptr
= (char*)malloc(length
+ 1);
51 git__throw(GIT_ENOMEM
, "Out of memory. Failed to duplicate string");
53 memcpy(ptr
, str
, length
);
59 GIT_INLINE(void *) git__realloc(void *ptr
, size_t size
)
61 void *new_ptr
= realloc(ptr
, size
);
63 git__throw(GIT_ENOMEM
, "Out of memory. Failed to allocate %d bytes.", (int)size
);
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
);
72 extern int git__strtol32(long *n
, const char *buff
, const char **end_buf
, int base
);
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
);
77 /** @return true if p fits into the range of a size_t */
78 GIT_INLINE(int) git__is_sizet(git_off_t p
)
81 return p
== (git_off_t
)r
;
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))))
91 extern char *git__strtok(char **end
, const char *sep
);
93 extern void git__strntolower(char *str
, int len
);
94 extern void git__strtolower(char *str
);
96 #define STRLEN(str) (sizeof(str) - 1)
98 #define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1)
100 extern int git__fnmatch(const char *pattern
, const char *name
, int flags
);
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.
107 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
109 #define alloc_nr(x) (((x)+16)*3/2)
110 #define ALLOC_GROW(x, nr, alloc) \
112 if ((nr) > alloc) { \
113 if (alloc_nr(alloc) < (nr)) \
116 alloc = alloc_nr(alloc); \
117 x = xrealloc((x), alloc * sizeof(*(x))); \
121 extern int git__msort(void *b
, size_t n
, size_t s
,
122 int (*cmp
)(const void *, const void *));
124 #endif /* INCLUDE_util_h__ */