| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
/* |
| 6 |
* Dynamic array support. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "dynarr.h" |
| 10 |
|
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
typedef dnaDCL(void, dnaGeneric); |
| 14 |
|
| 15 |
struct dnaCtx_ { /* Library context */ |
| 16 |
ctlMemoryCallbacks mem; |
| 17 |
}; |
| 18 |
|
| 19 |
/* Validate client and create context. */ |
| 20 |
dnaCtx dnaNew(ctlMemoryCallbacks *mem_cb, CTL_CHECK_ARGS_DCL) { |
| 21 |
dnaCtx h; |
| 22 |
|
| 23 |
/* Check client/library compatibility */ |
| 24 |
if (CTL_CHECK_ARGS_TEST(DNA_VERSION)) { |
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
/* Allocate context */ |
| 29 |
h = (dnaCtx)mem_cb->manage(mem_cb, NULL, sizeof(struct dnaCtx_)); |
| 30 |
if (h == NULL) { |
| 31 |
return NULL; |
| 32 |
} |
| 33 |
|
| 34 |
memset(h, 0, sizeof(*h)); |
| 35 |
|
| 36 |
/* Initialize context */ |
| 37 |
h->mem = *mem_cb; |
| 38 |
|
| 39 |
return h; |
| 40 |
} |
| 41 |
|
| 42 |
/* Free library context. */ |
| 43 |
void dnaFree(dnaCtx h) { |
| 44 |
if (h == NULL) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
h->mem.manage(&h->mem, h, 0); |
| 49 |
} |
| 50 |
|
| 51 |
/* Initialize dynamic array. */ |
| 52 |
void dnaInit(dnaCtx h, void *object, size_t init, size_t incr, int check) { |
| 53 |
dnaGeneric *da = (dnaGeneric *)object; |
| 54 |
|
| 55 |
if (check && da->size != 0) { |
| 56 |
return; /* Already initialized */ |
| 57 |
} |
| 58 |
da->ctx = h; |
| 59 |
da->array = (void *)init; /* Used once then overwritten by array ptr */ |
| 60 |
da->cnt = 0; |
| 61 |
da->size = 0; |
| 62 |
/* 64-bit warning fixed by cast here */ |
| 63 |
da->incr = (long)incr; |
| 64 |
da->func = NULL; |
| 65 |
} |
| 66 |
|
| 67 |
/* Grow dynamic array to accommodate index. Return -1 on allocation failure else |
| 68 |
0. If the client has arranged to trap allocation failures in the memory |
| 69 |
callback functions by using setjmp/longjmp, for example, the -1 return will |
| 70 |
never occur and the macro wrappers provided will be safe to use. */ |
| 71 |
long dnaGrow(void *object, size_t elemsize, long index) { |
| 72 |
void *new_ptr; |
| 73 |
size_t new_size; |
| 74 |
dnaGeneric *da = (dnaGeneric *)object; |
| 75 |
dnaCtx h = da->ctx; |
| 76 |
|
| 77 |
if (index < da->size || elemsize == 0) { |
| 78 |
return 0; /* Use existing allocation */ |
| 79 |
} else if (da->size == 0) { |
| 80 |
/* Initial allocation */ |
| 81 |
size_t init = (size_t)da->array; |
| 82 |
size_t new_mem_size; |
| 83 |
new_size = ((size_t)index < init) ? init : init + ((index - init) + da->incr) / da->incr * da->incr; |
| 84 |
new_mem_size = new_size * elemsize; |
| 85 |
if (new_mem_size / elemsize == new_size) /* check math overflow */ |
| 86 |
new_ptr = h->mem.manage(&h->mem, NULL, new_mem_size); |
| 87 |
else |
| 88 |
new_ptr = NULL; |
| 89 |
} else { |
| 90 |
size_t new_mem_size; |
| 91 |
/* Incremental allocation */ |
| 92 |
new_size = da->size + |
| 93 |
((index - da->size) + da->incr) / da->incr * da->incr; |
| 94 |
new_mem_size = new_size * elemsize; |
| 95 |
if (new_mem_size / elemsize == new_size) /* check math overflow */ |
| 96 |
new_ptr = h->mem.manage(&h->mem, da->array, new_mem_size); |
| 97 |
else |
| 98 |
new_ptr = NULL; |
| 99 |
} |
| 100 |
|
| 101 |
if (new_ptr == NULL) { |
| 102 |
return -1; /* Allocation failed */ |
| 103 |
} |
| 104 |
|
| 105 |
/* explicitly zero out the new memory before initialization */ |
| 106 |
memset((char *)new_ptr + (da->size * elemsize), 0, (new_size - da->size) * elemsize); |
| 107 |
|
| 108 |
if (da->func != NULL) { |
| 109 |
/* Initialize newly allocated elements */ |
| 110 |
/* 64-bit warning fixed by cast here */ |
| 111 |
da->func(h->mem.ctx, |
| 112 |
(long)(new_size - da->size), (char *)new_ptr + da->size * elemsize); |
| 113 |
} |
| 114 |
|
| 115 |
da->array = new_ptr; |
| 116 |
/* 64-bit warning fixed by cast here */ |
| 117 |
da->size = (long)new_size; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
/* Set da cnt. Return -1 on error else 0. */ |
| 122 |
long dnaSetCnt(void *object, size_t elemsize, long cnt) { |
| 123 |
dnaGeneric *da = (dnaGeneric *)object; |
| 124 |
if (cnt > da->size) { |
| 125 |
if (dnaGrow(object, elemsize, cnt - 1)) { |
| 126 |
return -1; |
| 127 |
} |
| 128 |
} |
| 129 |
da->cnt = cnt; |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
/* Index da. Return -1 on error else index. */ |
| 134 |
long dnaIndex(void *object, size_t elemsize, long index) { |
| 135 |
dnaGeneric *da = (dnaGeneric *)object; |
| 136 |
if (index >= da->size) { |
| 137 |
if (dnaGrow(object, elemsize, index)) { |
| 138 |
return -1; |
| 139 |
} |
| 140 |
} |
| 141 |
return index; |
| 142 |
} |
| 143 |
|
| 144 |
/* Index da and record max index+1 in da.cnt. Return -1 on error else index. */ |
| 145 |
long dnaMax(void *object, size_t elemsize, long index) { |
| 146 |
dnaGeneric *da = (dnaGeneric *)object; |
| 147 |
if (index >= da->size) { |
| 148 |
if (dnaGrow(object, elemsize, index)) { |
| 149 |
return -1; |
| 150 |
} |
| 151 |
} |
| 152 |
if (index + 1 > da->cnt) { |
| 153 |
da->cnt = index + 1; |
| 154 |
} |
| 155 |
return index; |
| 156 |
} |
| 157 |
|
| 158 |
/* Grow da by one element. Return -1 on error else da->cnt. */ |
| 159 |
long dnaNext(void *object, size_t elemsize) { |
| 160 |
dnaGeneric *da = (dnaGeneric *)object; |
| 161 |
if (da->cnt >= da->size) { |
| 162 |
if (dnaGrow(object, elemsize, da->cnt)) { |
| 163 |
return -1; |
| 164 |
} |
| 165 |
} |
| 166 |
return da->cnt++; |
| 167 |
} |
| 168 |
|
| 169 |
/* Extend da by length elements. Return -1 on error else first element of |
| 170 |
extension. */ |
| 171 |
long dnaExtend(void *object, size_t elemsize, long length) { |
| 172 |
dnaGeneric *da = (dnaGeneric *)object; |
| 173 |
long index = da->cnt + length - 1; |
| 174 |
long retval = da->cnt; |
| 175 |
if (index >= da->size) { |
| 176 |
if (dnaGrow(object, elemsize, index)) { |
| 177 |
return -1; |
| 178 |
} |
| 179 |
} |
| 180 |
da->cnt = index + 1; |
| 181 |
return retval; |
| 182 |
} |
| 183 |
|
| 184 |
/* Free dynamic array object. */ |
| 185 |
void dnaFreeObj(void *object) { |
| 186 |
dnaGeneric *da = (dnaGeneric *)object; |
| 187 |
if (da->size != 0) { |
| 188 |
dnaCtx h = da->ctx; |
| 189 |
h->mem.manage(&h->mem, da->array, 0); |
| 190 |
da->size = 0; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/* Get version numbers of libraries. */ |
| 195 |
void dnaGetVersion(ctlVersionCallbacks *cb) { |
| 196 |
if (cb->called & 1 << DNA_LIB_ID) { |
| 197 |
return; /* Already enumerated */ |
| 198 |
} |
| 199 |
cb->getversion(cb, DNA_VERSION, "dynarr"); |
| 200 |
|
| 201 |
/* Record this call */ |
| 202 |
cb->called |= 1 << DNA_LIB_ID; |
| 203 |
} |