]>
git.proxmox.com Git - libgit2.git/blob - src/util.h
2 * Copyright (C) 2009-2011 the libgit2 contributors
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.
7 #ifndef INCLUDE_util_h__
8 #define INCLUDE_util_h__
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))))
14 # define min(a,b) ((a) < (b) ? (a) : (b))
18 * Custom memory allocation wrappers
19 * that set error code and error message
20 * on allocation failure
22 GIT_INLINE(void *) git__malloc(size_t len
)
24 void *ptr
= malloc(len
);
26 git__throw(GIT_ENOMEM
, "Out of memory. Failed to allocate %d bytes.", (int)len
);
30 GIT_INLINE(void *) git__calloc(size_t nelem
, size_t elsize
)
32 void *ptr
= calloc(nelem
, elsize
);
34 git__throw(GIT_ENOMEM
, "Out of memory. Failed to allocate %d bytes.", (int)elsize
);
38 GIT_INLINE(char *) git__strdup(const char *str
)
40 char *ptr
= strdup(str
);
42 git__throw(GIT_ENOMEM
, "Out of memory. Failed to duplicate string");
46 GIT_INLINE(char *) git__strndup(const char *str
, size_t n
)
55 ptr
= (char*)malloc(length
+ 1);
57 git__throw(GIT_ENOMEM
, "Out of memory. Failed to duplicate string");
61 memcpy(ptr
, str
, length
);
67 GIT_INLINE(void *) git__realloc(void *ptr
, size_t size
)
69 void *new_ptr
= realloc(ptr
, size
);
71 git__throw(GIT_ENOMEM
, "Out of memory. Failed to allocate %d bytes.", (int)size
);
75 extern int git__prefixcmp(const char *str
, const char *prefix
);
76 extern int git__suffixcmp(const char *str
, const char *suffix
);
78 extern int git__strtol32(long *n
, const char *buff
, const char **end_buf
, int base
);
80 extern void git__hexdump(const char *buffer
, size_t n
);
81 extern uint32_t git__hash(const void *key
, int len
, uint32_t seed
);
83 /** @return true if p fits into the range of a size_t */
84 GIT_INLINE(int) git__is_sizet(git_off_t p
)
87 return p
== (git_off_t
)r
;
90 /* 32-bit cross-platform rotl */
91 #ifdef _MSC_VER /* use built-in method in MSVC */
92 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
93 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
94 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
97 extern char *git__strtok(char **end
, const char *sep
);
99 extern void git__strntolower(char *str
, size_t len
);
100 extern void git__strtolower(char *str
);
102 extern int git__fnmatch(const char *pattern
, const char *name
, int flags
);
105 * Realloc the buffer pointed at by variable 'x' so that it can hold
106 * at least 'nr' entries; the number of entries currently allocated
107 * is 'alloc', using the standard growing factor alloc_nr() macro.
109 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
111 #define alloc_nr(x) (((x)+16)*3/2)
112 #define ALLOC_GROW(x, nr, alloc) \
114 if ((nr) > alloc) { \
115 if (alloc_nr(alloc) < (nr)) \
118 alloc = alloc_nr(alloc); \
119 x = xrealloc((x), alloc * sizeof(*(x))); \
123 extern void git__tsort(void **dst
, size_t size
, int (*cmp
)(const void *, const void *));
124 extern void **git__bsearch(const void *key
, void **base
, size_t nmemb
,
125 int (*compar
)(const void *, const void *));
127 extern int git__strcmp_cb(const void *a
, const void *b
);
129 #endif /* INCLUDE_util_h__ */