summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-06-12 13:46:44 -0700
committerRussell Belfer <rb@github.com>2013-06-12 13:46:44 -0700
commitef3374a8a81786a7b544ed7eded53c95766eb02f (patch)
tree66a813ad935656e145c583dea8b23c0ccb31c6ef
parentf9c824c592d7a23f7cc385c25c95a5d0c5c8687e (diff)
downloadlibgit2-ef3374a8a81786a7b544ed7eded53c95766eb02f.tar.gz
Improvements to git_array
This changes the size data to uint32_t, fixes the array growth logic to use a simple 1.5x multiplier, and uses a generic inline function for growing the array to make the git_array_alloc API feel more natural (i.e. it returns a pointer to the new item).
-rw-r--r--src/array.h51
-rw-r--r--src/diff_driver.c2
-rw-r--r--src/diff_patch.c4
3 files changed, 41 insertions, 16 deletions
diff --git a/src/array.h b/src/array.h
index aadd021f1..2d77c71a0 100644
--- a/src/array.h
+++ b/src/array.h
@@ -9,7 +9,23 @@
#include "util.h"
-#define git_array_t(type) struct { type *ptr; size_t size, asize; }
+/*
+ * Use this to declare a typesafe resizable array of items, a la:
+ *
+ * git_array_t(int) my_ints = GIT_ARRAY_INIT;
+ * ...
+ * int *i = git_array_alloc(my_ints);
+ * GITERR_CHECK_ALLOC(i);
+ * ...
+ * git_array_clear(my_ints);
+ *
+ * You may also want to do things like:
+ *
+ * typedef git_array_t(my_struct) my_struct_array_t;
+ */
+#define git_array_t(type) struct { type *ptr; uint32_t size, asize; }
+
+#define GIT_ARRAY_INIT { NULL, 0, 0 }
#define git_array_init(a) \
do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
@@ -17,20 +33,29 @@
#define git_array_clear(a) \
do { git__free((a).ptr); git_array_init(a); } while (0)
-#define git_array_grow(a) do { \
- void *new_array; size_t new_size = \
- ((a).asize >= 256) ? (a).asize + 256 : ((a).asize >= 8) ? (a).asize * 2 : 8; \
- new_array = git__realloc((a).ptr, new_size * sizeof(*(a).ptr)); \
- if (!new_array) { git_array_clear(a); } \
- else { (a).ptr = new_array; (a).asize = new_size; } \
- } while (0)
-
#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr)
-#define git_array_alloc(a, el) do { \
- if ((a).size >= (a).asize) git_array_grow(a); \
- (el) = (a).ptr ? &(a).ptr[(a).size++] : NULL; \
- } while (0)
+
+typedef git_array_t(void) git_array_generic_t;
+
+/* use a generic array for growth so this can return the new item */
+GIT_INLINE(void *) git_array_grow(git_array_generic_t *a, size_t item_size)
+{
+ uint32_t new_size = (a->size < 8) ? 8 : a->asize * 3 / 2;
+ void *new_array = git__realloc(a->ptr, new_size * item_size);
+ if (!new_array) {
+ git_array_clear(*a);
+ return NULL;
+ } else {
+ a->ptr = new_array; a->asize = new_size; a->size++;
+ return (((char *)a->ptr) + (a->size - 1) * item_size);
+ }
+}
+
+#define git_array_alloc(a) \
+ ((a).size >= (a).asize) ? \
+ git_array_grow((git_array_generic_t *)&(a), sizeof(*(a).ptr)) : \
+ (a).ptr ? &(a).ptr[(a).size++] : NULL
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL)
diff --git a/src/diff_driver.c b/src/diff_driver.c
index 5d3274a11..ae2b7c319 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -93,7 +93,7 @@ static int diff_driver_add_funcname(
return error;
}
- git_array_alloc(drv->fn_patterns, re_ptr);
+ re_ptr = git_array_alloc(drv->fn_patterns);
GITERR_CHECK_ALLOC(re_ptr);
memcpy(re_ptr, &re, sizeof(re));
diff --git a/src/diff_patch.c b/src/diff_patch.c
index 4c0b9a70c..05dee5ef7 100644
--- a/src/diff_patch.c
+++ b/src/diff_patch.c
@@ -712,7 +712,7 @@ static int diff_patch_hunk_cb(
GIT_UNUSED(delta);
- git_array_alloc(patch->hunks, hunk);
+ hunk = git_array_alloc(patch->hunks);
GITERR_CHECK_ALLOC(hunk);
memcpy(&hunk->range, range, sizeof(hunk->range));
@@ -749,7 +749,7 @@ static int diff_patch_line_cb(
hunk = git_array_last(patch->hunks);
GITERR_CHECK_ALLOC(hunk);
- git_array_alloc(patch->lines, line);
+ line = git_array_alloc(patch->lines);
GITERR_CHECK_ALLOC(line);
line->ptr = content;