Commit d02066c9 authored by Chris Müller's avatar Chris Müller

lib: add increase- and reduce allocation methods for arrays.

parent cf8cc7b6
......@@ -31,7 +31,7 @@ struct CryArray
static void resize_memory(struct CryArray* array, size_t size)
size_t cry_array_increase_alloc(struct CryArray* array, size_t size)
{
assert(array != 0);
......@@ -39,6 +39,21 @@ static void resize_memory(struct CryArray* array, size_t size)
array->alloc += array->extend;
array->data = cry_realloc(uint8_t, array->data, array->extend);
return array->alloc;
}
size_t cry_array_reduce_alloc(struct CryArray* array)
{
assert(array != 0);
while(array->alloc - array->length <= array->extend)
array->alloc -= array->extend;
array->data = cry_realloc(uint8_t, array->data, array->extend);
return array->alloc;
}
......@@ -91,26 +106,13 @@ cry_array_alloc(struct CryArray* array)
void
cry_array_set_alloc(struct CryArray* array, size_t space)
{
assert(array != 0);
if(space > array->alloc) {
array->data = cry_realloc(uint8_t, array->data, space);
array->alloc = space;
}
}
void
cry_array_append(struct CryArray* array, pointer data, size_t size)
{
assert(array != 0);
if(array->alloc < array->length + size)
resize_memory(array, array->length + size);
cry_array_increase_alloc(array, array->length + size);
memcpy(array->data + array->length, data, size);
......@@ -124,7 +126,7 @@ cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size
assert(array != 0);
if(array->alloc < array->length + size)
resize_memory(array, array->length + size);
cry_array_increase_alloc(array, array->length + size);
memmove(array->data + index, array->data + index + size, size);
memcpy(array->data + index, data, size);
......
......@@ -27,11 +27,11 @@ struct CryArray;
struct CryArray* cry_array_new(size_t initialsize, size_t extend);
void cry_array_free(struct CryArray* array);
size_t cry_array_size(struct CryArray* array);
size_t cry_array_alloc(struct CryArray* array);
void cry_array_set_alloc(struct CryArray* array, size_t space);
size_t cry_array_increase_alloc(struct CryArray* array, size_t size);
size_t cry_array_reduce_alloc(struct CryArray* array);
void cry_array_append(struct CryArray* array, pointer data, size_t size);
void cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size);
......
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