Commit 7a9eab13 authored by Christoffer Ackelman's avatar Christoffer Ackelman

Fixed issue in co_array.c causing it to realloc when it doesn't need to.

parent 7607d228
...@@ -46,11 +46,14 @@ array_tCtx array_New(int elemsize, int alloc_incr) ...@@ -46,11 +46,14 @@ array_tCtx array_New(int elemsize, int alloc_incr)
if (!arr) if (!arr)
return NULL; return NULL;
arr->data = calloc(1, elemsize * alloc_incr); arr->data = calloc(1, elemsize * alloc_incr);
if (!arr->data) if (!arr->data) {
free(arr);
return NULL; return NULL;
}
arr->elemsize = elemsize; arr->elemsize = elemsize;
arr->alloc_incr = alloc_incr; arr->alloc_incr = alloc_incr;
arr->capacity = alloc_incr; arr->capacity = alloc_incr;
arr->size = 0;
return arr; return arr;
} }
...@@ -68,11 +71,12 @@ int array_Push(array_tCtx arr, void* elem) ...@@ -68,11 +71,12 @@ int array_Push(array_tCtx arr, void* elem)
int array_Concat(array_tCtx arr, void* elems, int number) int array_Concat(array_tCtx arr, void* elems, int number)
{ {
if (arr->capacity <= arr->size + number - 1) { if (arr->capacity <= arr->size + number - 1) {
void* a_tmp = realloc(arr->data, arr->elemsize * MAX(arr->capacity + arr->alloc_incr, arr->size + number)); int newCapacity = MAX(arr->capacity + arr->alloc_incr, arr->size + number);
void* a_tmp = realloc(arr->data, arr->elemsize * newCapacity);
if (!a_tmp) if (!a_tmp)
return 0; return 0;
arr->data = a_tmp; arr->data = a_tmp;
arr->capacity = number; arr->capacity = newCapacity;
} }
memcpy((char*)arr->data + arr->size * arr->elemsize, elems, memcpy((char*)arr->data + arr->size * arr->elemsize, elems,
arr->elemsize * number); arr->elemsize * number);
......
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