Unverified Commit 45489a11 by Edward Thomson Committed by GitHub

Merge pull request #6008 from boretrk/array

git_array_alloc: return objects of correct type
parents 34e685c0 f062eb62
...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
typedef git_array_t(char) git_array_generic_t; typedef git_array_t(char) git_array_generic_t;
/* use a generic array for growth so this can return the new item */ /* use a generic array for growth, return 0 on success */
GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size) GIT_INLINE(int) git_array_grow(void *_a, size_t item_size)
{ {
volatile git_array_generic_t *a = _a; volatile git_array_generic_t *a = _a;
size_t new_size; size_t new_size;
...@@ -59,18 +59,18 @@ GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size) ...@@ -59,18 +59,18 @@ GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
if ((new_array = git__reallocarray(a->ptr, new_size, item_size)) == NULL) if ((new_array = git__reallocarray(a->ptr, new_size, item_size)) == NULL)
goto on_oom; goto on_oom;
a->ptr = new_array; a->asize = new_size; a->size++; a->ptr = new_array;
return a->ptr + (a->size - 1) * item_size; a->asize = new_size;
return 0;
on_oom: on_oom:
git_array_clear(*a); git_array_clear(*a);
return NULL; return -1;
} }
#define git_array_alloc(a) \ #define git_array_alloc(a) \
(((a).size >= (a).asize) ? \ (((a).size < (a).asize || git_array_grow(&(a), sizeof(*(a).ptr)) == 0) ? \
git_array_grow(&(a), sizeof(*(a).ptr)) : \ &(a).ptr[(a).size++] : (void *)NULL)
((a).ptr ? &(a).ptr[(a).size++] : (void *)NULL))
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : (void *)NULL) #define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : (void *)NULL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment