Mercurial > code
changeset 165:970e491d93cb
Add _trim function to realloc to the needed size
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 24 Sep 2012 23:03:50 +0200 |
parents | 654f32079cdc |
children | 7f214f26a4c0 |
files | array.c array.h parray.c parray.h |
diffstat | 4 files changed, 32 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/array.c Wed Sep 05 07:44:07 2012 +0200 +++ b/array.c Mon Sep 24 23:03:50 2012 +0200 @@ -348,6 +348,15 @@ } /* + * Trim down the array to the correct size. + */ +void * +array_trim(struct array *arr) +{ + return arr->realloc(arr->data, arr->length * arr->unit); +} + +/* * Increate the array storage when it is full. If the buffer is fixed size * it returns -1 on full buffer otherwise 0 is returned if allocation * succeeded.
--- a/array.h Wed Sep 05 07:44:07 2012 +0200 +++ b/array.h Mon Sep 24 23:03:50 2012 +0200 @@ -27,9 +27,9 @@ enum array_flags { ARRAY_AUTO = 0, /* array grows automatically */ - ARRAY_FIXED = (1 << 0) /* fixed size length */ - ARRAY_FASTREMOVE = (1 << 1) /* use last object when removing */ - ARRAY_CLEARBITS = (1 << 2) /* clear data when inserting/removing */ + ARRAY_FIXED = (1 << 0), /* fixed size length */ + ARRAY_FASTREMOVE = (1 << 1), /* use last object when removing */ + ARRAY_CLEARBITS = (1 << 2), /* clear data when inserting/removing */ ARRAY_INSERTSAFE = (1 << 3) /* insertion must have valid indexes */ }; @@ -109,6 +109,9 @@ void array_free(struct array *); + +void * +array_trim(struct array *); #define ARRAY_FOREACH(a, var, i) \ for (i = 0, (var) = array_first((a)); \
--- a/parray.c Wed Sep 05 07:44:07 2012 +0200 +++ b/parray.c Mon Sep 24 23:03:50 2012 +0200 @@ -325,6 +325,20 @@ } /* + * Trim down the array to the correct size. + */ +void * +parray_trim(struct parray *arr) +{ + int count = arr->length; + + if (arr->flags & PARRAY_NULLEND) + ++ count; + + return arr->realloc(arr->data, count * sizeof (void *)); +} + +/* * Increate the array storage when it is full. If the buffer is fixed size * it returns -1 on full buffer otherwise 0 is returned if allocation * succeeded.