1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
This software is licensed as OpenSource, under the Apache License, Version 2.0.
This license is available at: http://opensource.org/licenses/Apache-2.0. */
/*
* Dynamic array support.
*/
#include <stdio.h>
#include "Dda.h"
/* Memory management functions */
static void *(*alloc)(size_t size);
static void *(*resize)(void *old, size_t size);
static void (*dealloc)(void *ptr);
/* Dynamic array object template */
typedef da_DCL(void, DA);
/* Initialize dynamic array */
void da_Init(void *object, unsigned long intl, unsigned long incr) {
DA *da = object;
da->array = (void *)intl;
da->cnt = 0;
da->size = 0;
da->incr = incr;
da->init = NULL;
}
/* Grow dynamic array to accommodate index */
void da_Grow(void *object, size_t element, unsigned long index) {
DA *da = object;
long newSize = (index + da->incr) / da->incr * da->incr;
if (da->size == 0) {
if (newSize < (long)da->array)
newSize = (long)da->array; /* Use initial allocation */
da->array = alloc(newSize * element);
} else
da->array = resize(da->array, newSize * element);
if (da->init != NULL && da->array != NULL) {
/* Initialize new elements */
char *p;
for (p = &((char *)da->array)[da->size * element];
p < &((char *)da->array)[newSize * element];
p += element)
if (da->init(p))
break; /* Client function wants to stop */
}
da->size = newSize;
}
/* Free dynamic array */
void da_Free(void *object) {
dealloc(((DA *)object)->array);
}
/* Initialize memory management functions */
void da_SetMemFuncs(void *(*Alloc)(size_t size),
void *(*Resize)(void *old, size_t size),
void (*Dealloc)(void *ptr)) {
alloc = Alloc;
resize = Resize;
dealloc = Dealloc;
}
|