diff options
author | Andreas Ericsson <ae@op5.se> | 2008-11-22 14:44:47 +0100 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2008-11-22 12:08:00 -0800 |
commit | 3a2aabdce127a367aa27a4c27025199230e26c15 (patch) | |
tree | cb8e90b049ec87e0a999952544839c3c17f88e37 | |
parent | 76a8c44727406a76d251102bdee607871887a119 (diff) | |
download | libgit2-3a2aabdce127a367aa27a4c27025199230e26c15.tar.gz |
Add util.h - utility macros
ARRAY_SIZE() et al go in util.h, included from common.h
Signed-off-by: Andreas Ericsson <ae@op5.se>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
-rw-r--r-- | src/common.h | 1 | ||||
-rw-r--r-- | src/util.h | 25 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h index 6624a3e24..5a6fa6e57 100644 --- a/src/common.h +++ b/src/common.h @@ -2,6 +2,7 @@ #define INCLUDE_common_h__ #include "cc-compat.h" +#include "util.h" #include <errno.h> #include <stdlib.h> #include <unistd.h> diff --git a/src/util.h b/src/util.h new file mode 100644 index 000000000..45504b7b9 --- /dev/null +++ b/src/util.h @@ -0,0 +1,25 @@ +#ifndef INCLUDE_util_h__ +#define INCLUDE_util_h__ + +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) + +/* + * Realloc the buffer pointed at by variable 'x' so that it can hold + * at least 'nr' entries; the number of entries currently allocated + * is 'alloc', using the standard growing factor alloc_nr() macro. + * + * DO NOT USE any expression with side-effect for 'x' or 'alloc'. + */ +#define alloc_nr(x) (((x)+16)*3/2) +#define ALLOC_GROW(x, nr, alloc) \ + do { \ + if ((nr) > alloc) { \ + if (alloc_nr(alloc) < (nr)) \ + alloc = (nr); \ + else \ + alloc = alloc_nr(alloc); \ + x = xrealloc((x), alloc * sizeof(*(x))); \ + } \ + } while(0) + +#endif /* INCLUDE_util_h__ */ |