src/array_list.c

changeset 628
1e2be40f0cb5
parent 627
cc8cbabd27cd
child 629
6c81ee4f11ad
     1.1 --- a/src/array_list.c	Sun Nov 20 17:48:42 2022 +0100
     1.2 +++ b/src/array_list.c	Sun Nov 20 21:08:36 2022 +0100
     1.3 @@ -31,7 +31,7 @@
     1.4  #include <string.h>
     1.5  #include <stdint.h>
     1.6  
     1.7 -/* LOW LEVEL ARRAY LIST FUNCTIONS */
     1.8 +// LOW LEVEL ARRAY LIST FUNCTIONS
     1.9  
    1.10  enum cx_array_copy_result cx_array_copy(
    1.11          void **target,
    1.12 @@ -43,37 +43,37 @@
    1.13          size_t elem_count,
    1.14          struct cx_array_reallocator_s *reallocator
    1.15  ) {
    1.16 -    /* assert pointers */
    1.17 +    // assert pointers
    1.18      assert(target != NULL);
    1.19      assert(size != NULL);
    1.20      assert(src != NULL);
    1.21  
    1.22 -    /* determine capacity */
    1.23 +    // determine capacity
    1.24      size_t cap = capacity == NULL ? *size : *capacity;
    1.25  
    1.26 -    /* check if resize is required */
    1.27 +    // check if resize is required
    1.28      size_t minsize = index + elem_count;
    1.29      size_t newsize = *size < minsize ? minsize : *size;
    1.30      bool needrealloc = newsize > cap;
    1.31  
    1.32 -    /* reallocate if possible */
    1.33 +    // reallocate if possible
    1.34      if (needrealloc) {
    1.35 -        /* a reallocator and a capacity variable must be available */
    1.36 +        // a reallocator and a capacity variable must be available
    1.37          if (reallocator == NULL || capacity == NULL) {
    1.38              return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
    1.39          }
    1.40  
    1.41 -        /* check, if we need to repair the src pointer */
    1.42 +        // check, if we need to repair the src pointer
    1.43          uintptr_t targetaddr = (uintptr_t) *target;
    1.44          uintptr_t srcaddr = (uintptr_t) src;
    1.45          bool repairsrc = targetaddr <= srcaddr
    1.46                           && srcaddr < targetaddr + cap * elem_size;
    1.47  
    1.48 -        /* calculate new capacity (next number divisible by 16) */
    1.49 +        // calculate new capacity (next number divisible by 16)
    1.50          cap = newsize - (newsize % 16) + 16;
    1.51          assert(cap > newsize);
    1.52  
    1.53 -        /* perform reallocation */
    1.54 +        // perform reallocation
    1.55          void *newmem = reallocator->realloc(
    1.56                  *target, cap, elem_size, reallocator
    1.57          );
    1.58 @@ -81,25 +81,25 @@
    1.59              return CX_ARRAY_COPY_REALLOC_FAILED;
    1.60          }
    1.61  
    1.62 -        /* repair src pointer, if necessary */
    1.63 +        // repair src pointer, if necessary
    1.64          if (repairsrc) {
    1.65              src = ((char *) newmem) + (srcaddr - targetaddr);
    1.66          }
    1.67  
    1.68 -        /* store new pointer and capacity */
    1.69 +        // store new pointer and capacity
    1.70          *target = newmem;
    1.71          *capacity = cap;
    1.72      }
    1.73  
    1.74 -    /* determine target pointer */
    1.75 +    // determine target pointer
    1.76      char *start = *target;
    1.77      start += index * elem_size;
    1.78  
    1.79 -    /* copy elements and set new size */
    1.80 +    // copy elements and set new size
    1.81      memmove(start, src, elem_count * elem_size);
    1.82      *size = newsize;
    1.83  
    1.84 -    /* return successfully */
    1.85 +    // return successfully
    1.86      return CX_ARRAY_COPY_SUCCESS;
    1.87  }
    1.88  
    1.89 @@ -111,38 +111,38 @@
    1.90          size_t idx1,
    1.91          size_t idx2
    1.92  ) {
    1.93 -    /* short circuit */
    1.94 +    // short circuit
    1.95      if (idx1 == idx2) return;
    1.96  
    1.97      char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
    1.98      void *tmp;
    1.99  
   1.100 -    /* decide if we can use the local buffer */
   1.101 +    // decide if we can use the local buffer
   1.102      if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
   1.103          tmp = malloc(elem_size);
   1.104 -        /* we don't want to enforce error handling */
   1.105 +        // we don't want to enforce error handling
   1.106          if (tmp == NULL) abort();
   1.107      } else {
   1.108          tmp = sbo_mem;
   1.109      }
   1.110  
   1.111 -    /* calculate memory locations */
   1.112 +    // calculate memory locations
   1.113      char *left = arr, *right = arr;
   1.114      left += idx1 * elem_size;
   1.115      right += idx2 * elem_size;
   1.116  
   1.117 -    /* three-way swap */
   1.118 +    // three-way swap
   1.119      memcpy(tmp, left, elem_size);
   1.120      memcpy(left, right, elem_size);
   1.121      memcpy(right, tmp, elem_size);
   1.122  
   1.123 -    /* free dynamic memory, if it was needed */
   1.124 +    // free dynamic memory, if it was needed
   1.125      if (tmp != sbo_mem) {
   1.126          free(tmp);
   1.127      }
   1.128  }
   1.129  
   1.130 -/* HIGH LEVEL ARRAY LIST FUNCTIONS */
   1.131 +// HIGH LEVEL ARRAY LIST FUNCTIONS
   1.132  
   1.133  typedef struct {
   1.134      struct cx_list_s base;
   1.135 @@ -156,10 +156,10 @@
   1.136          size_t elem_size,
   1.137          struct cx_array_reallocator_s *alloc
   1.138  ) {
   1.139 -    /* retrieve the pointer to the list allocator */
   1.140 +    // retrieve the pointer to the list allocator
   1.141      CxAllocator const *al = alloc->ptr1;
   1.142  
   1.143 -    /* use the list allocator to reallocate the memory */
   1.144 +    // use the list allocator to reallocate the memory
   1.145      return cxRealloc(al, array, capacity * elem_size);
   1.146  }
   1.147  
   1.148 @@ -197,7 +197,7 @@
   1.149      } else {
   1.150          cx_array_list *arl = (cx_array_list *) list;
   1.151  
   1.152 -        /* move elements starting at index to the right */
   1.153 +        // move elements starting at index to the right
   1.154          if (cx_array_copy(
   1.155                  &arl->data,
   1.156                  &list->size,
   1.157 @@ -211,7 +211,7 @@
   1.158              return 1;
   1.159          }
   1.160  
   1.161 -        /* place the element */
   1.162 +        // place the element
   1.163          memcpy(((char *) arl->data) + index * list->itemsize,
   1.164                 elem, list->itemsize);
   1.165  
   1.166 @@ -247,18 +247,18 @@
   1.167          struct cx_list_s *list,
   1.168          size_t index
   1.169  ) {
   1.170 -    /* out-of-bounds check */
   1.171 +    // out-of-bounds check
   1.172      if (index >= list->size) {
   1.173          return 1;
   1.174      }
   1.175  
   1.176 -    /* short-circuit removal of last element */
   1.177 +    // short-circuit removal of last element
   1.178      if (index == list->size - 1) {
   1.179          list->size--;
   1.180          return 0;
   1.181      }
   1.182  
   1.183 -    /* just move the elements starting at index to the left */
   1.184 +    // just move the elements starting at index to the left
   1.185      cx_array_list *arl = (cx_array_list *) list;
   1.186      int result = cx_array_copy(
   1.187              &arl->data,
   1.188 @@ -271,7 +271,7 @@
   1.189              &arl->reallocator
   1.190      );
   1.191      if (result == 0) {
   1.192 -        /* decrease the size */
   1.193 +        // decrease the size
   1.194          list->size--;
   1.195      }
   1.196      return result;
   1.197 @@ -417,7 +417,7 @@
   1.198      list->base.itemsize = item_size;
   1.199      list->base.capacity = initial_capacity;
   1.200  
   1.201 -    /* configure the reallocator */
   1.202 +    // configure the reallocator
   1.203      list->reallocator.realloc = cx_arl_realloc;
   1.204      list->reallocator.ptr1 = (void *) allocator;
   1.205  

mercurial