src/array_list.c

Mon, 15 Jan 2024 20:59:18 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 15 Jan 2024 20:59:18 +0100
changeset 807
c8d692131b1e
parent 804
5136f2fc32ec
child 817
949908c97474
permissions
-rw-r--r--

remove flags to disable SBO in tests - fix #343 fix #358

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/array_list.h"
    30 #include "cx/compare.h"
    31 #include <assert.h>
    32 #include <string.h>
    34 // LOW LEVEL ARRAY LIST FUNCTIONS
    36 enum cx_array_copy_result cx_array_copy(
    37         void **target,
    38         size_t *size,
    39         size_t *capacity,
    40         size_t index,
    41         void const *src,
    42         size_t elem_size,
    43         size_t elem_count,
    44         struct cx_array_reallocator_s *reallocator
    45 ) {
    46     // assert pointers
    47     assert(target != NULL);
    48     assert(size != NULL);
    49     assert(src != NULL);
    51     // determine capacity
    52     size_t cap = capacity == NULL ? *size : *capacity;
    54     // check if resize is required
    55     size_t minsize = index + elem_count;
    56     size_t newsize = *size < minsize ? minsize : *size;
    57     bool needrealloc = newsize > cap;
    59     // reallocate if possible
    60     if (needrealloc) {
    61         // a reallocator and a capacity variable must be available
    62         if (reallocator == NULL || capacity == NULL) {
    63             return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
    64         }
    66         // check, if we need to repair the src pointer
    67         uintptr_t targetaddr = (uintptr_t) *target;
    68         uintptr_t srcaddr = (uintptr_t) src;
    69         bool repairsrc = targetaddr <= srcaddr
    70                          && srcaddr < targetaddr + cap * elem_size;
    72         // calculate new capacity (next number divisible by 16)
    73         cap = newsize - (newsize % 16) + 16;
    74         assert(cap > newsize);
    76         // perform reallocation
    77         void *newmem = reallocator->realloc(
    78                 *target, cap, elem_size, reallocator
    79         );
    80         if (newmem == NULL) {
    81             return CX_ARRAY_COPY_REALLOC_FAILED;
    82         }
    84         // repair src pointer, if necessary
    85         if (repairsrc) {
    86             src = ((char *) newmem) + (srcaddr - targetaddr);
    87         }
    89         // store new pointer and capacity
    90         *target = newmem;
    91         *capacity = cap;
    92     }
    94     // determine target pointer
    95     char *start = *target;
    96     start += index * elem_size;
    98     // copy elements and set new size
    99     memmove(start, src, elem_count * elem_size);
   100     *size = newsize;
   102     // return successfully
   103     return CX_ARRAY_COPY_SUCCESS;
   104 }
   106 #ifndef CX_ARRAY_SWAP_SBO_SIZE
   107 #define CX_ARRAY_SWAP_SBO_SIZE 128
   108 #endif
   109 unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE;
   111 void cx_array_swap(
   112         void *arr,
   113         size_t elem_size,
   114         size_t idx1,
   115         size_t idx2
   116 ) {
   117     assert(arr != NULL);
   119     // short circuit
   120     if (idx1 == idx2) return;
   122     char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
   123     void *tmp;
   125     // decide if we can use the local buffer
   126     if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
   127         tmp = malloc(elem_size);
   128         // we don't want to enforce error handling
   129         if (tmp == NULL) abort();
   130     } else {
   131         tmp = sbo_mem;
   132     }
   134     // calculate memory locations
   135     char *left = arr, *right = arr;
   136     left += idx1 * elem_size;
   137     right += idx2 * elem_size;
   139     // three-way swap
   140     memcpy(tmp, left, elem_size);
   141     memcpy(left, right, elem_size);
   142     memcpy(right, tmp, elem_size);
   144     // free dynamic memory, if it was needed
   145     if (tmp != sbo_mem) {
   146         free(tmp);
   147     }
   148 }
   150 // HIGH LEVEL ARRAY LIST FUNCTIONS
   152 typedef struct {
   153     struct cx_list_s base;
   154     void *data;
   155     size_t capacity;
   156     struct cx_array_reallocator_s reallocator;
   157 } cx_array_list;
   159 static void *cx_arl_realloc(
   160         void *array,
   161         size_t capacity,
   162         size_t elem_size,
   163         struct cx_array_reallocator_s *alloc
   164 ) {
   165     // retrieve the pointer to the list allocator
   166     CxAllocator const *al = alloc->ptr1;
   168     // use the list allocator to reallocate the memory
   169     return cxRealloc(al, array, capacity * elem_size);
   170 }
   172 static void cx_arl_destructor(struct cx_list_s *list) {
   173     cx_array_list *arl = (cx_array_list *) list;
   175     char *ptr = arl->data;
   177     if (list->simple_destructor) {
   178         for (size_t i = 0; i < list->size; i++) {
   179             cx_invoke_simple_destructor(list, ptr);
   180             ptr += list->item_size;
   181         }
   182     }
   183     if (list->advanced_destructor) {
   184         for (size_t i = 0; i < list->size; i++) {
   185             cx_invoke_advanced_destructor(list, ptr);
   186             ptr += list->item_size;
   187         }
   188     }
   190     cxFree(list->allocator, arl->data);
   191     cxFree(list->allocator, list);
   192 }
   194 static size_t cx_arl_insert_array(
   195         struct cx_list_s *list,
   196         size_t index,
   197         void const *array,
   198         size_t n
   199 ) {
   200     // out of bounds and special case check
   201     if (index > list->size || n == 0) return 0;
   203     // get a correctly typed pointer to the list
   204     cx_array_list *arl = (cx_array_list *) list;
   206     // do we need to move some elements?
   207     if (index < list->size) {
   208         char const *first_to_move = (char const *) arl->data;
   209         first_to_move += index * list->item_size;
   210         size_t elems_to_move = list->size - index;
   211         size_t start_of_moved = index + n;
   213         if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
   214                 &arl->data,
   215                 &list->size,
   216                 &arl->capacity,
   217                 start_of_moved,
   218                 first_to_move,
   219                 list->item_size,
   220                 elems_to_move,
   221                 &arl->reallocator
   222         )) {
   223             // if moving existing elems is unsuccessful, abort
   224             return 0;
   225         }
   226     }
   228     // note that if we had to move the elements, the following operation
   229     // is guaranteed to succeed, because we have the memory already allocated
   230     // therefore, it is impossible to leave this function with an invalid array
   232     // place the new elements
   233     if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
   234             &arl->data,
   235             &list->size,
   236             &arl->capacity,
   237             index,
   238             array,
   239             list->item_size,
   240             n,
   241             &arl->reallocator
   242     )) {
   243         return n;
   244     } else {
   245         // array list implementation is "all or nothing"
   246         return 0;
   247     }
   248 }
   250 static int cx_arl_insert_element(
   251         struct cx_list_s *list,
   252         size_t index,
   253         void const *element
   254 ) {
   255     return 1 != cx_arl_insert_array(list, index, element, 1);
   256 }
   258 static int cx_arl_insert_iter(
   259         struct cx_mut_iterator_s *iter,
   260         void const *elem,
   261         int prepend
   262 ) {
   263     struct cx_list_s *list = iter->src_handle;
   264     if (iter->index < list->size) {
   265         int result = cx_arl_insert_element(
   266                 list,
   267                 iter->index + 1 - prepend,
   268                 elem
   269         );
   270         if (result == 0 && prepend != 0) {
   271             iter->index++;
   272             iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
   273         }
   274         return result;
   275     } else {
   276         int result = cx_arl_insert_element(list, list->size, elem);
   277         iter->index = list->size;
   278         return result;
   279     }
   280 }
   282 static int cx_arl_remove(
   283         struct cx_list_s *list,
   284         size_t index
   285 ) {
   286     cx_array_list *arl = (cx_array_list *) list;
   288     // out-of-bounds check
   289     if (index >= list->size) {
   290         return 1;
   291     }
   293     // content destruction
   294     cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size);
   296     // short-circuit removal of last element
   297     if (index == list->size - 1) {
   298         list->size--;
   299         return 0;
   300     }
   302     // just move the elements starting at index to the left
   303     int result = cx_array_copy(
   304             &arl->data,
   305             &list->size,
   306             &arl->capacity,
   307             index,
   308             ((char *) arl->data) + (index + 1) * list->item_size,
   309             list->item_size,
   310             list->size - index - 1,
   311             &arl->reallocator
   312     );
   313     if (result == 0) {
   314         // decrease the size
   315         list->size--;
   316     }
   317     return result;
   318 }
   320 static void cx_arl_clear(struct cx_list_s *list) {
   321     if (list->size == 0) return;
   323     cx_array_list *arl = (cx_array_list *) list;
   324     char *ptr = arl->data;
   326     if (list->simple_destructor) {
   327         for (size_t i = 0; i < list->size; i++) {
   328             cx_invoke_simple_destructor(list, ptr);
   329             ptr += list->item_size;
   330         }
   331     }
   332     if (list->advanced_destructor) {
   333         for (size_t i = 0; i < list->size; i++) {
   334             cx_invoke_advanced_destructor(list, ptr);
   335             ptr += list->item_size;
   336         }
   337     }
   339     memset(arl->data, 0, list->size * list->item_size);
   340     list->size = 0;
   341 }
   343 static int cx_arl_swap(
   344         struct cx_list_s *list,
   345         size_t i,
   346         size_t j
   347 ) {
   348     if (i >= list->size || j >= list->size) return 1;
   349     cx_array_list *arl = (cx_array_list *) list;
   350     cx_array_swap(arl->data, list->item_size, i, j);
   351     return 0;
   352 }
   354 static void *cx_arl_at(
   355         struct cx_list_s const *list,
   356         size_t index
   357 ) {
   358     if (index < list->size) {
   359         cx_array_list const *arl = (cx_array_list const *) list;
   360         char *space = arl->data;
   361         return space + index * list->item_size;
   362     } else {
   363         return NULL;
   364     }
   365 }
   367 static ssize_t cx_arl_find_remove(
   368         struct cx_list_s *list,
   369         void const *elem,
   370         bool remove
   371 ) {
   372     assert(list->cmpfunc != NULL);
   373     assert(list->size < SIZE_MAX / 2);
   374     char *cur = ((cx_array_list const *) list)->data;
   376     for (ssize_t i = 0; i < (ssize_t) list->size; i++) {
   377         if (0 == list->cmpfunc(elem, cur)) {
   378             if (remove) {
   379                 if (0 == cx_arl_remove(list, i)) {
   380                     return i;
   381                 } else {
   382                     return -1;
   383                 }
   384             } else {
   385                 return i;
   386             }
   387         }
   388         cur += list->item_size;
   389     }
   391     return -1;
   392 }
   394 static void cx_arl_sort(struct cx_list_s *list) {
   395     assert(list->cmpfunc != NULL);
   396     qsort(((cx_array_list *) list)->data,
   397           list->size,
   398           list->item_size,
   399           list->cmpfunc
   400     );
   401 }
   403 static int cx_arl_compare(
   404         struct cx_list_s const *list,
   405         struct cx_list_s const *other
   406 ) {
   407     assert(list->cmpfunc != NULL);
   408     if (list->size == other->size) {
   409         char const *left = ((cx_array_list const *) list)->data;
   410         char const *right = ((cx_array_list const *) other)->data;
   411         for (size_t i = 0; i < list->size; i++) {
   412             int d = list->cmpfunc(left, right);
   413             if (d != 0) {
   414                 return d;
   415             }
   416             left += list->item_size;
   417             right += other->item_size;
   418         }
   419         return 0;
   420     } else {
   421         return list->size < other->size ? -1 : 1;
   422     }
   423 }
   425 static void cx_arl_reverse(struct cx_list_s *list) {
   426     if (list->size < 2) return;
   427     void *data = ((cx_array_list const *) list)->data;
   428     size_t half = list->size / 2;
   429     for (size_t i = 0; i < half; i++) {
   430         cx_array_swap(data, list->item_size, i, list->size - 1 - i);
   431     }
   432 }
   434 static bool cx_arl_iter_valid(void const *it) {
   435     struct cx_iterator_s const *iter = it;
   436     struct cx_list_s const *list = iter->src_handle;
   437     return iter->index < list->size;
   438 }
   440 static void *cx_arl_iter_current(void const *it) {
   441     struct cx_iterator_s const *iter = it;
   442     return iter->elem_handle;
   443 }
   445 static void cx_arl_iter_next(void *it) {
   446     struct cx_iterator_base_s *itbase = it;
   447     if (itbase->remove) {
   448         struct cx_mut_iterator_s *iter = it;
   449         itbase->remove = false;
   450         cx_arl_remove(iter->src_handle, iter->index);
   451     } else {
   452         struct cx_iterator_s *iter = it;
   453         iter->index++;
   454         iter->elem_handle =
   455                 ((char *) iter->elem_handle)
   456                 + ((struct cx_list_s const *) iter->src_handle)->item_size;
   457     }
   458 }
   460 static void cx_arl_iter_prev(void *it) {
   461     struct cx_iterator_base_s *itbase = it;
   462     struct cx_mut_iterator_s *iter = it;
   463     cx_array_list *const list = iter->src_handle;
   464     if (itbase->remove) {
   465         itbase->remove = false;
   466         cx_arl_remove(iter->src_handle, iter->index);
   467     }
   468     iter->index--;
   469     if (iter->index < list->base.size) {
   470         iter->elem_handle = ((char *) list->data)
   471                             + iter->index * list->base.item_size;
   472     }
   473 }
   475 static bool cx_arl_iter_flag_rm(void *it) {
   476     struct cx_iterator_base_s *iter = it;
   477     if (iter->mutating) {
   478         iter->remove = true;
   479         return true;
   480     } else {
   481         return false;
   482     }
   483 }
   485 static struct cx_iterator_s cx_arl_iterator(
   486         struct cx_list_s const *list,
   487         size_t index,
   488         bool backwards
   489 ) {
   490     struct cx_iterator_s iter;
   492     iter.index = index;
   493     iter.src_handle = list;
   494     iter.elem_handle = cx_arl_at(list, index);
   495     iter.base.valid = cx_arl_iter_valid;
   496     iter.base.current = cx_arl_iter_current;
   497     iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
   498     iter.base.flag_removal = cx_arl_iter_flag_rm;
   499     iter.base.remove = false;
   500     iter.base.mutating = false;
   502     return iter;
   503 }
   505 static cx_list_class cx_array_list_class = {
   506         cx_arl_destructor,
   507         cx_arl_insert_element,
   508         cx_arl_insert_array,
   509         cx_arl_insert_iter,
   510         cx_arl_remove,
   511         cx_arl_clear,
   512         cx_arl_swap,
   513         cx_arl_at,
   514         cx_arl_find_remove,
   515         cx_arl_sort,
   516         cx_arl_compare,
   517         cx_arl_reverse,
   518         cx_arl_iterator,
   519 };
   521 CxList *cxArrayListCreate(
   522         CxAllocator const *allocator,
   523         cx_compare_func comparator,
   524         size_t item_size,
   525         size_t initial_capacity
   526 ) {
   527     if (allocator == NULL) {
   528         allocator = cxDefaultAllocator;
   529     }
   531     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   532     if (list == NULL) return NULL;
   534     list->base.cl = &cx_array_list_class;
   535     list->base.allocator = allocator;
   536     list->capacity = initial_capacity;
   538     if (item_size > 0) {
   539         list->base.item_size = item_size;
   540         list->base.cmpfunc = comparator;
   541     } else {
   542         item_size = sizeof(void *);
   543         list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
   544         cxListStorePointers((CxList *) list);
   545     }
   547     // allocate the array after the real item_size is known
   548     list->data = cxCalloc(allocator, initial_capacity, item_size);
   549     if (list->data == NULL) {
   550         cxFree(allocator, list);
   551         return NULL;
   552     }
   554     // configure the reallocator
   555     list->reallocator.realloc = cx_arl_realloc;
   556     list->reallocator.ptr1 = (void *) allocator;
   558     return (CxList *) list;
   559 }

mercurial