X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=src%2Fdynarr.c;h=5dfc79d35e19f502463e40d4b96558a1cd8eeaf2;hb=767a4d3c6cc84c530d28f5fa02f51c23ccd29f4e;hp=74bd399b866e443f3e4700880bd0b433bca43252;hpb=6883ee56ec887c2c48abe5b06b5e66aa74031910;p=chise%2Fxemacs-chise.git.1 diff --git a/src/dynarr.c b/src/dynarr.c index 74bd399..5dfc79d 100644 --- a/src/dynarr.c +++ b/src/dynarr.c @@ -44,7 +44,7 @@ until the next operation that changes the length of the array. This is a container object. Declare a dynamic array of a specific type as follows: -typdef struct +typedef struct { Dynarr_declare (mytype); } mytype_dynarr; @@ -72,7 +72,7 @@ Use the following functions/macros: The elements should be contiguous in memory, starting at BASE. Dynarr_insert_many(d, base, len, start) - Insert LEN elements to the dynamic arrary starting at position + Insert LEN elements to the dynamic array starting at position START. The elements should be contiguous in memory, starting at BASE. int Dynarr_length(d) @@ -101,15 +101,28 @@ Use the following functions/macros: Use the following global variable: Dynarr_min_size - Minimum allowable size for a dynamic array when it is resized. The - default is 32 and does not normally need to be changed. + Minimum allowable size for a dynamic array when it is resized. */ #include #include "lisp.h" -int Dynarr_min_size = 1; +static int Dynarr_min_size = 8; + +static void +Dynarr_realloc (Dynarr *dy, int new_size) +{ + if (DUMPEDP (dy->base)) + { + void *new_base = malloc (new_size); + int max_bytes = dy->max * dy->elsize; + memcpy (new_base, dy->base, max_bytes > new_size ? new_size : max_bytes); + dy->base = new_base; + } + else + dy->base = xrealloc (dy->base, new_size); +} void * Dynarr_newf (int elsize) @@ -138,14 +151,14 @@ Dynarr_resize (void *d, int size) /* Don't do anything if the array is already big enough. */ if (newsize > dy->max) { - dy->base = xrealloc (dy->base, newsize*dy->elsize); + Dynarr_realloc (dy, newsize*dy->elsize); dy->max = newsize; } } /* Add a number of contiguous elements to the array starting at START. */ void -Dynarr_insert_many (void *d, CONST void *el, int len, int start) +Dynarr_insert_many (void *d, const void *el, int len, int start) { Dynarr *dy = (Dynarr *) d; @@ -186,9 +199,10 @@ Dynarr_free (void *d) { Dynarr *dy = (Dynarr *) d; - if (dy->base) + if (dy->base && !DUMPEDP (dy->base)) xfree (dy->base); - xfree (dy); + if(!DUMPEDP (dy)) + xfree (dy); } #ifdef MEMORY_USAGE_STATS