src/array_list.c

changeset 1606
f5883f6e42e7
parent 1605
55b13f583356
child 1607
0ecb13118cac
equal deleted inserted replaced
1605:55b13f583356 1606:f5883f6e42e7
214 } 214 }
215 #endif 215 #endif
216 } 216 }
217 217
218 return 0; 218 return 0;
219 }
220
221 int cx_array_init_(CxArray *array, const CxAllocator *allocator, size_t elem_size, size_t capacity) {
222 array->size = 0;
223 array->capacity = capacity;
224 array->data = cxCalloc(allocator, capacity, elem_size);
225 return array->data == NULL;
226 }
227
228 static size_t cx_array_increase_capacity(size_t old_capacity) {
229 // TODO: make this strategy better
230 if (old_capacity < 1024) {
231 return old_capacity + 64;
232 } else {
233 return old_capacity + 128;
234 }
235 }
236
237 int cx_array_add_(CxArray *array, const CxAllocator *allocator, size_t elem_size, void *element) {
238 if (array->size >= array->capacity) {
239 size_t newcap = cx_array_increase_capacity(array->capacity);
240 if (cxReallocateArray(allocator, &array->data, newcap, elem_size)) {
241 return -1;
242 }
243 array->capacity = newcap;
244 }
245 char *dst = array->data;
246 dst += elem_size * array->size;
247 memcpy(dst, element, elem_size);
248 array->size++;
249 return 0;
250 }
251
252 void cx_array_free_(const CxAllocator *allocator, CxArray *array) {
253 cxFree(allocator, array->data);
254 array->data = NULL;
255 array->size = array->capacity = 0;
219 } 256 }
220 257
221 int cx_array_copy( 258 int cx_array_copy(
222 void **target, 259 void **target,
223 void *size, 260 void *size,

mercurial