Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef3374a

Browse files
committed
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).
1 parent f9c824c commit ef3374a

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/array.h

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,53 @@
99

1010
#include "util.h"
1111

12-
#define git_array_t(type) struct { type *ptr; size_t size, asize; }
12+
/*
13+
* Use this to declare a typesafe resizable array of items, a la:
14+
*
15+
* git_array_t(int) my_ints = GIT_ARRAY_INIT;
16+
* ...
17+
* int *i = git_array_alloc(my_ints);
18+
* GITERR_CHECK_ALLOC(i);
19+
* ...
20+
* git_array_clear(my_ints);
21+
*
22+
* You may also want to do things like:
23+
*
24+
* typedef git_array_t(my_struct) my_struct_array_t;
25+
*/
26+
#define git_array_t(type) struct { type *ptr; uint32_t size, asize; }
27+
28+
#define GIT_ARRAY_INIT { NULL, 0, 0 }
1329

1430
#define git_array_init(a) \
1531
do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
1632

1733
#define git_array_clear(a) \
1834
do { git__free((a).ptr); git_array_init(a); } while (0)
1935

20-
#define git_array_grow(a) do { \
21-
void *new_array; size_t new_size = \
22-
((a).asize >= 256) ? (a).asize + 256 : ((a).asize >= 8) ? (a).asize * 2 : 8; \
23-
new_array = git__realloc((a).ptr, new_size * sizeof(*(a).ptr)); \
24-
if (!new_array) { git_array_clear(a); } \
25-
else { (a).ptr = new_array; (a).asize = new_size; } \
26-
} while (0)
27-
2836
#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr)
2937

30-
#define git_array_alloc(a, el) do { \
31-
if ((a).size >= (a).asize) git_array_grow(a); \
32-
(el) = (a).ptr ? &(a).ptr[(a).size++] : NULL; \
33-
} while (0)
38+
39+
typedef git_array_t(void) git_array_generic_t;
40+
41+
/* use a generic array for growth so this can return the new item */
42+
GIT_INLINE(void *) git_array_grow(git_array_generic_t *a, size_t item_size)
43+
{
44+
uint32_t new_size = (a->size < 8) ? 8 : a->asize * 3 / 2;
45+
void *new_array = git__realloc(a->ptr, new_size * item_size);
46+
if (!new_array) {
47+
git_array_clear(*a);
48+
return NULL;
49+
} else {
50+
a->ptr = new_array; a->asize = new_size; a->size++;
51+
return (((char *)a->ptr) + (a->size - 1) * item_size);
52+
}
53+
}
54+
55+
#define git_array_alloc(a) \
56+
((a).size >= (a).asize) ? \
57+
git_array_grow((git_array_generic_t *)&(a), sizeof(*(a).ptr)) : \
58+
(a).ptr ? &(a).ptr[(a).size++] : NULL
3459

3560
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL)
3661

src/diff_driver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int diff_driver_add_funcname(
9393
return error;
9494
}
9595

96-
git_array_alloc(drv->fn_patterns, re_ptr);
96+
re_ptr = git_array_alloc(drv->fn_patterns);
9797
GITERR_CHECK_ALLOC(re_ptr);
9898

9999
memcpy(re_ptr, &re, sizeof(re));

src/diff_patch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static int diff_patch_hunk_cb(
712712

713713
GIT_UNUSED(delta);
714714

715-
git_array_alloc(patch->hunks, hunk);
715+
hunk = git_array_alloc(patch->hunks);
716716
GITERR_CHECK_ALLOC(hunk);
717717

718718
memcpy(&hunk->range, range, sizeof(hunk->range));
@@ -749,7 +749,7 @@ static int diff_patch_line_cb(
749749
hunk = git_array_last(patch->hunks);
750750
GITERR_CHECK_ALLOC(hunk);
751751

752-
git_array_alloc(patch->lines, line);
752+
line = git_array_alloc(patch->lines);
753753
GITERR_CHECK_ALLOC(line);
754754

755755
line->ptr = content;

0 commit comments

Comments
 (0)