This is a container object. Declare a dynamic array of a specific type
as follows:
-typdef struct
+typedef struct
{
Dynarr_declare (mytype);
} mytype_dynarr;
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)
int Dynarr_min_size = 1;
+static void
+Dynarr_realloc (Dynarr *dy, int new_size)
+{
+ if (DUMPEDP (dy->base))
+ {
+ void *new_base = malloc (new_size);
+ memcpy (new_base, dy->base, dy->max > new_size ? new_size : dy->max);
+ dy->base = new_base;
+ }
+ else
+ dy->base = xrealloc (dy->base, new_size);
+}
+
void *
Dynarr_newf (int elsize)
{
/* 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;
}
}
{
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