diff -r cc8cbabd27cd -r 1e2be40f0cb5 src/array_list.c --- a/src/array_list.c Sun Nov 20 17:48:42 2022 +0100 +++ b/src/array_list.c Sun Nov 20 21:08:36 2022 +0100 @@ -31,7 +31,7 @@ #include #include -/* LOW LEVEL ARRAY LIST FUNCTIONS */ +// LOW LEVEL ARRAY LIST FUNCTIONS enum cx_array_copy_result cx_array_copy( void **target, @@ -43,37 +43,37 @@ size_t elem_count, struct cx_array_reallocator_s *reallocator ) { - /* assert pointers */ + // assert pointers assert(target != NULL); assert(size != NULL); assert(src != NULL); - /* determine capacity */ + // determine capacity size_t cap = capacity == NULL ? *size : *capacity; - /* check if resize is required */ + // check if resize is required size_t minsize = index + elem_count; size_t newsize = *size < minsize ? minsize : *size; bool needrealloc = newsize > cap; - /* reallocate if possible */ + // reallocate if possible if (needrealloc) { - /* a reallocator and a capacity variable must be available */ + // a reallocator and a capacity variable must be available if (reallocator == NULL || capacity == NULL) { return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; } - /* check, if we need to repair the src pointer */ + // check, if we need to repair the src pointer uintptr_t targetaddr = (uintptr_t) *target; uintptr_t srcaddr = (uintptr_t) src; bool repairsrc = targetaddr <= srcaddr && srcaddr < targetaddr + cap * elem_size; - /* calculate new capacity (next number divisible by 16) */ + // calculate new capacity (next number divisible by 16) cap = newsize - (newsize % 16) + 16; assert(cap > newsize); - /* perform reallocation */ + // perform reallocation void *newmem = reallocator->realloc( *target, cap, elem_size, reallocator ); @@ -81,25 +81,25 @@ return CX_ARRAY_COPY_REALLOC_FAILED; } - /* repair src pointer, if necessary */ + // repair src pointer, if necessary if (repairsrc) { src = ((char *) newmem) + (srcaddr - targetaddr); } - /* store new pointer and capacity */ + // store new pointer and capacity *target = newmem; *capacity = cap; } - /* determine target pointer */ + // determine target pointer char *start = *target; start += index * elem_size; - /* copy elements and set new size */ + // copy elements and set new size memmove(start, src, elem_count * elem_size); *size = newsize; - /* return successfully */ + // return successfully return CX_ARRAY_COPY_SUCCESS; } @@ -111,38 +111,38 @@ size_t idx1, size_t idx2 ) { - /* short circuit */ + // short circuit if (idx1 == idx2) return; char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; void *tmp; - /* decide if we can use the local buffer */ + // decide if we can use the local buffer if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { tmp = malloc(elem_size); - /* we don't want to enforce error handling */ + // we don't want to enforce error handling if (tmp == NULL) abort(); } else { tmp = sbo_mem; } - /* calculate memory locations */ + // calculate memory locations char *left = arr, *right = arr; left += idx1 * elem_size; right += idx2 * elem_size; - /* three-way swap */ + // three-way swap memcpy(tmp, left, elem_size); memcpy(left, right, elem_size); memcpy(right, tmp, elem_size); - /* free dynamic memory, if it was needed */ + // free dynamic memory, if it was needed if (tmp != sbo_mem) { free(tmp); } } -/* HIGH LEVEL ARRAY LIST FUNCTIONS */ +// HIGH LEVEL ARRAY LIST FUNCTIONS typedef struct { struct cx_list_s base; @@ -156,10 +156,10 @@ size_t elem_size, struct cx_array_reallocator_s *alloc ) { - /* retrieve the pointer to the list allocator */ + // retrieve the pointer to the list allocator CxAllocator const *al = alloc->ptr1; - /* use the list allocator to reallocate the memory */ + // use the list allocator to reallocate the memory return cxRealloc(al, array, capacity * elem_size); } @@ -197,7 +197,7 @@ } else { cx_array_list *arl = (cx_array_list *) list; - /* move elements starting at index to the right */ + // move elements starting at index to the right if (cx_array_copy( &arl->data, &list->size, @@ -211,7 +211,7 @@ return 1; } - /* place the element */ + // place the element memcpy(((char *) arl->data) + index * list->itemsize, elem, list->itemsize); @@ -247,18 +247,18 @@ struct cx_list_s *list, size_t index ) { - /* out-of-bounds check */ + // out-of-bounds check if (index >= list->size) { return 1; } - /* short-circuit removal of last element */ + // short-circuit removal of last element if (index == list->size - 1) { list->size--; return 0; } - /* just move the elements starting at index to the left */ + // just move the elements starting at index to the left cx_array_list *arl = (cx_array_list *) list; int result = cx_array_copy( &arl->data, @@ -271,7 +271,7 @@ &arl->reallocator ); if (result == 0) { - /* decrease the size */ + // decrease the size list->size--; } return result; @@ -417,7 +417,7 @@ list->base.itemsize = item_size; list->base.capacity = initial_capacity; - /* configure the reallocator */ + // configure the reallocator list->reallocator.realloc = cx_arl_realloc; list->reallocator.ptr1 = (void *) allocator;