# HG changeset patch # User David Demelier # Date 1348520630 -7200 # Node ID 970e491d93cb44ccf8d58f0f7e11d6e59cb13af4 # Parent 654f32079cdc70303da2543be1a75ca77cc34421 Add _trim function to realloc to the needed size diff -r 654f32079cdc -r 970e491d93cb array.c --- 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. diff -r 654f32079cdc -r 970e491d93cb array.h --- 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)); \ diff -r 654f32079cdc -r 970e491d93cb parray.c --- 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. diff -r 654f32079cdc -r 970e491d93cb parray.h --- a/parray.h Wed Sep 05 07:44:07 2012 +0200 +++ b/parray.h Mon Sep 24 23:03:50 2012 +0200 @@ -106,6 +106,9 @@ void parray_free(struct parray *); +void * +parray_trim(struct parray *); + #define PARRAY_FOREACH(a, o, i) \ for (i = 0, o = (a)->data[i]; \ i < (a)->length; \