1.1 --- a/src/array.c Sat Aug 10 11:12:49 2019 +0200 1.2 +++ b/src/array.c Tue Sep 24 20:16:00 2019 +0200 1.3 @@ -35,6 +35,7 @@ 1.4 1.5 #include <string.h> 1.6 #include <stdlib.h> 1.7 +#include <errno.h> 1.8 1.9 #ifndef UCX_ARRAY_DISABLE_QSORT 1.10 #ifdef __GLIBC__ 1.11 @@ -67,6 +68,51 @@ 1.12 } 1.13 } 1.14 1.15 +int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, 1.16 + size_t elmsize, size_t index, ...) { 1.17 + 1.18 + if(!alloc || !capacity || !array) { 1.19 + errno = EINVAL; 1.20 + return 1; 1.21 + } 1.22 + 1.23 + size_t newcapacity = *capacity; 1.24 + while(index >= newcapacity) { 1.25 + if(ucx_szmul(newcapacity, 2, &newcapacity)) { 1.26 + errno = EOVERFLOW; 1.27 + return 1; 1.28 + } 1.29 + } 1.30 + 1.31 + size_t memlen, offset; 1.32 + if(ucx_szmul(newcapacity, elmsize, &memlen)) { 1.33 + errno = EOVERFLOW; 1.34 + return 1; 1.35 + } 1.36 + /* we don't need to check index*elmsize - it is smaller than memlen */ 1.37 + 1.38 + 1.39 + void* newptr = alrealloc(alloc, *array, memlen); 1.40 + if(newptr == NULL) { 1.41 + errno = ENOMEM; /* we cannot assume that every allocator sets this */ 1.42 + return 1; 1.43 + } 1.44 + *array = newptr; 1.45 + *capacity = newcapacity; 1.46 + 1.47 + 1.48 + char* dest = *array; 1.49 + dest += elmsize*index; 1.50 + 1.51 + va_list ap; 1.52 + va_start(ap, index); 1.53 + int elem = va_arg(ap, int); 1.54 + memcpy(dest, &elem, elmsize); 1.55 + va_end(ap); 1.56 + 1.57 + return 0; 1.58 +} 1.59 + 1.60 UcxArray ucx_array_new(size_t capacity, size_t elemsize) { 1.61 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator()); 1.62 }