# HG changeset patch # User Mike Becker # Date 1716489097 -7200 # Node ID 35bcb3216c0d10d0eb1c08931122ff573a65ba4b # Parent fe0d69d72bcd62c730210397931cef7983e57153 fix inconsistent use of item_size and elem_size diff -r fe0d69d72bcd -r 35bcb3216c0d src/array_list.c --- a/src/array_list.c Thu May 23 20:29:28 2024 +0200 +++ b/src/array_list.c Thu May 23 20:31:37 2024 +0200 @@ -194,13 +194,13 @@ if (list->base.simple_destructor) { for (size_t i = 0; i < list->base.size; i++) { cx_invoke_simple_destructor(list, ptr); - ptr += list->base.item_size; + ptr += list->base.elem_size; } } if (list->base.advanced_destructor) { for (size_t i = 0; i < list->base.size; i++) { cx_invoke_advanced_destructor(list, ptr); - ptr += list->base.item_size; + ptr += list->base.elem_size; } } @@ -223,7 +223,7 @@ // do we need to move some elements? if (index < list->base.size) { char const *first_to_move = (char const *) arl->data; - first_to_move += index * list->base.item_size; + first_to_move += index * list->base.elem_size; size_t elems_to_move = list->base.size - index; size_t start_of_moved = index + n; @@ -233,7 +233,7 @@ &arl->capacity, start_of_moved, first_to_move, - list->base.item_size, + list->base.elem_size, elems_to_move, &arl->reallocator )) { @@ -253,7 +253,7 @@ &arl->capacity, index, array, - list->base.item_size, + list->base.elem_size, n, &arl->reallocator )) { @@ -286,7 +286,7 @@ ); if (result == 0 && prepend != 0) { iter->index++; - iter->elem_handle = ((char *) iter->elem_handle) + list->base.item_size; + iter->elem_handle = ((char *) iter->elem_handle) + list->base.elem_size; } return result; } else { @@ -308,7 +308,7 @@ } // content destruction - cx_invoke_destructor(list, ((char *) arl->data) + index * list->base.item_size); + cx_invoke_destructor(list, ((char *) arl->data) + index * list->base.elem_size); // short-circuit removal of last element if (index == list->base.size - 1) { @@ -322,8 +322,8 @@ &list->base.size, &arl->capacity, index, - ((char *) arl->data) + (index + 1) * list->base.item_size, - list->base.item_size, + ((char *) arl->data) + (index + 1) * list->base.elem_size, + list->base.elem_size, list->base.size - index - 1, &arl->reallocator ); @@ -346,17 +346,17 @@ if (list->base.simple_destructor) { for (size_t i = 0; i < list->base.size; i++) { cx_invoke_simple_destructor(list, ptr); - ptr += list->base.item_size; + ptr += list->base.elem_size; } } if (list->base.advanced_destructor) { for (size_t i = 0; i < list->base.size; i++) { cx_invoke_advanced_destructor(list, ptr); - ptr += list->base.item_size; + ptr += list->base.elem_size; } } - memset(arl->data, 0, list->base.size * list->base.item_size); + memset(arl->data, 0, list->base.size * list->base.elem_size); list->base.size = 0; } @@ -367,7 +367,7 @@ ) { if (i >= list->base.size || j >= list->base.size) return 1; cx_array_list *arl = (cx_array_list *) list; - cx_array_swap(arl->data, list->base.item_size, i, j); + cx_array_swap(arl->data, list->base.elem_size, i, j); return 0; } @@ -378,7 +378,7 @@ if (index < list->base.size) { cx_array_list const *arl = (cx_array_list const *) list; char *space = arl->data; - return space + index * list->base.item_size; + return space + index * list->base.elem_size; } else { return NULL; } @@ -405,7 +405,7 @@ return i; } } - cur += list->base.item_size; + cur += list->base.elem_size; } return -1; @@ -415,7 +415,7 @@ assert(list->base.cmpfunc != NULL); qsort(((cx_array_list *) list)->data, list->base.size, - list->base.item_size, + list->base.elem_size, list->base.cmpfunc ); } @@ -433,8 +433,8 @@ if (d != 0) { return d; } - left += list->base.item_size; - right += other->base.item_size; + left += list->base.elem_size; + right += other->base.elem_size; } return 0; } else { @@ -447,7 +447,7 @@ void *data = ((cx_array_list const *) list)->data; size_t half = list->base.size / 2; for (size_t i = 0; i < half; i++) { - cx_array_swap(data, list->base.item_size, i, list->base.size - 1 - i); + cx_array_swap(data, list->base.elem_size, i, list->base.size - 1 - i); } } @@ -471,7 +471,7 @@ iter->index++; iter->elem_handle = ((char *) iter->elem_handle) - + ((struct cx_list_s const *) iter->src_handle.c)->base.item_size; + + ((struct cx_list_s const *) iter->src_handle.c)->base.elem_size; } } @@ -485,7 +485,7 @@ iter->index--; if (iter->index < list->base.base.size) { iter->elem_handle = ((char *) list->data) - + iter->index * list->base.base.item_size; + + iter->index * list->base.base.elem_size; } } @@ -500,7 +500,7 @@ iter.index = index; iter.src_handle.c = list; iter.elem_handle = cx_arl_at(list, index); - iter.elem_size = list->base.item_size; + iter.elem_size = list->base.elem_size; iter.elem_count = list->base.size; iter.base.valid = cx_arl_iter_valid; iter.base.current = cx_arl_iter_current; @@ -530,7 +530,7 @@ CxList *cxArrayListCreate( CxAllocator const *allocator, cx_compare_func comparator, - size_t item_size, + size_t elem_size, size_t initial_capacity ) { if (allocator == NULL) { @@ -544,17 +544,17 @@ list->base.base.allocator = allocator; list->capacity = initial_capacity; - if (item_size > 0) { - list->base.base.item_size = item_size; + if (elem_size > 0) { + list->base.base.elem_size = elem_size; list->base.base.cmpfunc = comparator; } else { - item_size = sizeof(void *); + elem_size = sizeof(void *); list->base.base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; cxListStorePointers((CxList *) list); } - // allocate the array after the real item_size is known - list->data = cxCalloc(allocator, initial_capacity, item_size); + // allocate the array after the real elem_size is known + list->data = cxCalloc(allocator, initial_capacity, elem_size); if (list->data == NULL) { cxFree(allocator, list); return NULL; diff -r fe0d69d72bcd -r 35bcb3216c0d src/cx/array_list.h --- a/src/cx/array_list.h Thu May 23 20:29:28 2024 +0200 +++ b/src/cx/array_list.h Thu May 23 20:31:37 2024 +0200 @@ -226,9 +226,9 @@ ) __attribute__((__nonnull__)); /** - * Allocates an array list for storing elements with \p item_size bytes each. + * Allocates an array list for storing elements with \p elem_size bytes each. * - * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if * cxListStorePointers() was called immediately after creation and the compare * function will be automatically set to cx_cmp_ptr(), if none is given. * @@ -237,34 +237,34 @@ * @param comparator the comparator for the elements * (if \c NULL, and the list is not storing pointers, sort and find * functions will not work) - * @param item_size the size of each element in bytes + * @param elem_size the size of each element in bytes * @param initial_capacity the initial number of elements the array can store * @return the created list */ CxList *cxArrayListCreate( CxAllocator const *allocator, cx_compare_func comparator, - size_t item_size, + size_t elem_size, size_t initial_capacity ); /** - * Allocates an array list for storing elements with \p item_size bytes each. + * Allocates an array list for storing elements with \p elem_size bytes each. * * The list will use the cxDefaultAllocator and \em NO compare function. * If you want to call functions that need a compare function, you have to * set it immediately after creation or use cxArrayListCreate(). * - * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if * cxListStorePointers() was called immediately after creation and the compare * function will be automatically set to cx_cmp_ptr(). * - * @param item_size the size of each element in bytes + * @param elem_size the size of each element in bytes * @param initial_capacity the initial number of elements the array can store * @return the created list */ -#define cxArrayListCreateSimple(item_size, initial_capacity) \ - cxArrayListCreate(NULL, NULL, item_size, initial_capacity) +#define cxArrayListCreateSimple(elem_size, initial_capacity) \ + cxArrayListCreate(NULL, NULL, elem_size, initial_capacity) #ifdef __cplusplus } // extern "C" diff -r fe0d69d72bcd -r 35bcb3216c0d src/cx/collection.h --- a/src/cx/collection.h Thu May 23 20:29:28 2024 +0200 +++ b/src/cx/collection.h Thu May 23 20:31:37 2024 +0200 @@ -64,7 +64,7 @@ /** * The size of each element. */ - size_t item_size; + size_t elem_size; /** * The number of currently stored elements. */ diff -r fe0d69d72bcd -r 35bcb3216c0d src/cx/hash_map.h --- a/src/cx/hash_map.h Thu May 23 20:29:28 2024 +0200 +++ b/src/cx/hash_map.h Thu May 23 20:31:37 2024 +0200 @@ -69,7 +69,7 @@ * * If \p buckets is zero, an implementation defined default will be used. * - * If \p item_size is CX_STORE_POINTERS, the created map will be created as if + * If \p elem_size is CX_STORE_POINTERS, the created map will be created as if * cxMapStorePointers() was called immediately after creation. * * @note Iterators provided by this hash map implementation provide the remove operation. @@ -91,7 +91,7 @@ /** * Creates a new hash map with a default number of buckets. * - * If \p item_size is CX_STORE_POINTERS, the created map will be created as if + * If \p elem_size is CX_STORE_POINTERS, the created map will be created as if * cxMapStorePointers() was called immediately after creation. * * @note Iterators provided by this hash map implementation provide the remove operation. diff -r fe0d69d72bcd -r 35bcb3216c0d src/cx/linked_list.h --- a/src/cx/linked_list.h Thu May 23 20:29:28 2024 +0200 +++ b/src/cx/linked_list.h Thu May 23 20:31:37 2024 +0200 @@ -50,9 +50,9 @@ extern unsigned cx_linked_list_swap_sbo_size; /** - * Allocates a linked list for storing elements with \p item_size bytes each. + * Allocates a linked list for storing elements with \p elem_size bytes each. * - * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if * cxListStorePointers() was called immediately after creation and the compare * function will be automatically set to cx_cmp_ptr(), if none is given. * @@ -61,31 +61,31 @@ * @param comparator the comparator for the elements * (if \c NULL, and the list is not storing pointers, sort and find * functions will not work) - * @param item_size the size of each element in bytes + * @param elem_size the size of each element in bytes * @return the created list */ CxList *cxLinkedListCreate( CxAllocator const *allocator, cx_compare_func comparator, - size_t item_size + size_t elem_size ); /** - * Allocates a linked list for storing elements with \p item_size bytes each. + * Allocates a linked list for storing elements with \p elem_size bytes each. * * The list will use cxDefaultAllocator and no comparator function. If you want * to call functions that need a comparator, you must either set one immediately * after list creation or use cxLinkedListCreate(). * - * If \p item_size is CX_STORE_POINTERS, the created list will be created as if + * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if * cxListStorePointers() was called immediately after creation and the compare * function will be automatically set to cx_cmp_ptr(). * - * @param item_size the size of each element in bytes + * @param elem_size the size of each element in bytes * @return the created list */ -#define cxLinkedListCreateSimple(item_size) \ - cxLinkedListCreate(NULL, NULL, item_size) +#define cxLinkedListCreateSimple(elem_size) \ + cxLinkedListCreate(NULL, NULL, elem_size) /** * Finds the node at a certain index. diff -r fe0d69d72bcd -r 35bcb3216c0d src/cx/map.h --- a/src/cx/map.h Thu May 23 20:29:28 2024 +0200 +++ b/src/cx/map.h Thu May 23 20:31:37 2024 +0200 @@ -184,7 +184,7 @@ __attribute__((__nonnull__)) static inline void cxMapStorePointers(CxMap *map) { map->base.store_pointer = true; - map->base.item_size = sizeof(void *); + map->base.elem_size = sizeof(void *); } diff -r fe0d69d72bcd -r 35bcb3216c0d src/hash_map.c --- a/src/hash_map.c Thu May 23 20:29:28 2024 +0200 +++ b/src/hash_map.c Thu May 23 20:31:37 2024 +0200 @@ -107,13 +107,13 @@ if (map->base.store_pointer) { memcpy(elm->data, &value, sizeof(void *)); } else { - memcpy(elm->data, value, map->base.item_size); + memcpy(elm->data, value, map->base.elem_size); } } else { // allocate new element struct cx_hash_map_element_s *e = cxMalloc( allocator, - sizeof(struct cx_hash_map_element_s) + map->base.item_size + sizeof(struct cx_hash_map_element_s) + map->base.elem_size ); if (e == NULL) { return -1; @@ -123,7 +123,7 @@ if (map->base.store_pointer) { memcpy(e->data, &value, sizeof(void *)); } else { - memcpy(e->data, value, map->base.item_size); + memcpy(e->data, value, map->base.elem_size); } // copy the key @@ -342,7 +342,7 @@ iter.base.current = cx_hash_map_iter_current_key; break; case CX_MAP_ITERATOR_VALUES: - iter.elem_size = map->base.item_size; + iter.elem_size = map->base.elem_size; iter.base.current = cx_hash_map_iter_current_value; break; default: @@ -417,10 +417,10 @@ if (itemsize > 0) { map->base.base.store_pointer = false; - map->base.base.item_size = itemsize; + map->base.base.elem_size = itemsize; } else { map->base.base.store_pointer = true; - map->base.base.item_size = sizeof(void *); + map->base.base.elem_size = sizeof(void *); } return (CxMap *) map; diff -r fe0d69d72bcd -r 35bcb3216c0d src/linked_list.c --- a/src/linked_list.c Thu May 23 20:29:28 2024 +0200 +++ b/src/linked_list.c Thu May 23 20:31:37 2024 +0200 @@ -518,14 +518,14 @@ // create the new new_node cx_linked_list_node *new_node = cxMalloc(list->base.allocator, - sizeof(cx_linked_list_node) + list->base.item_size); + sizeof(cx_linked_list_node) + list->base.elem_size); // sortir if failed if (new_node == NULL) return 1; // initialize new new_node new_node->prev = new_node->next = NULL; - memcpy(new_node->payload, elem, list->base.item_size); + memcpy(new_node->payload, elem, list->base.elem_size); // insert cx_linked_list *ll = (cx_linked_list *) list; @@ -566,7 +566,7 @@ // we can add the remaining nodes and immedately advance to the inserted node char const *source = array; for (size_t i = 1; i < n; i++) { - source += list->base.item_size; + source += list->base.elem_size; if (0 != cx_ll_insert_at(list, node, source)) { return i; } @@ -707,7 +707,7 @@ } } - if (list->base.item_size > CX_LINKED_LIST_SWAP_SBO_SIZE) { + if (list->base.elem_size > CX_LINKED_LIST_SWAP_SBO_SIZE) { cx_linked_list_node *prev = nleft->prev; cx_linked_list_node *next = nright->next; cx_linked_list_node *midstart = nleft->next; @@ -739,9 +739,9 @@ } else { // swap payloads to avoid relinking char buf[CX_LINKED_LIST_SWAP_SBO_SIZE]; - memcpy(buf, nleft->payload, list->base.item_size); - memcpy(nleft->payload, nright->payload, list->base.item_size); - memcpy(nright->payload, buf, list->base.item_size); + memcpy(buf, nleft->payload, list->base.elem_size); + memcpy(nleft->payload, nright->payload, list->base.elem_size); + memcpy(nright->payload, buf, list->base.elem_size); } return 0; @@ -871,7 +871,7 @@ iter.index = index; iter.src_handle.c = list; iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index); - iter.elem_size = list->base.item_size; + iter.elem_size = list->base.elem_size; iter.elem_count = list->base.size; iter.base.valid = cx_ll_iter_valid; iter.base.current = cx_ll_iter_current; @@ -934,7 +934,7 @@ CxList *cxLinkedListCreate( CxAllocator const *allocator, cx_compare_func comparator, - size_t item_size + size_t elem_size ) { if (allocator == NULL) { allocator = cxDefaultAllocator; @@ -946,8 +946,8 @@ list->base.cl = &cx_linked_list_class; list->base.base.allocator = allocator; - if (item_size > 0) { - list->base.base.item_size = item_size; + if (elem_size > 0) { + list->base.base.elem_size = elem_size; list->base.base.cmpfunc = comparator; } else { list->base.base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; diff -r fe0d69d72bcd -r 35bcb3216c0d src/list.c --- a/src/list.c Thu May 23 20:29:28 2024 +0200 +++ b/src/list.c Thu May 23 20:31:37 2024 +0200 @@ -188,7 +188,7 @@ } void cxListStorePointers(CxList *list) { - list->base.item_size = sizeof(void *); + list->base.elem_size = sizeof(void *); list->base.store_pointer = true; list->climpl = list->cl; list->cl = &cx_pointer_list_class; diff -r fe0d69d72bcd -r 35bcb3216c0d tests/test_hash_map.c --- a/tests/test_hash_map.c Thu May 23 20:29:28 2024 +0200 +++ b/tests/test_hash_map.c Thu May 23 20:31:37 2024 +0200 @@ -42,7 +42,7 @@ for(size_t i = 0 ; i < hmap->bucket_count ; i++) { CX_TEST_ASSERT(hmap->buckets[i] == NULL); } - CX_TEST_ASSERT(map->base.item_size == 1); + CX_TEST_ASSERT(map->base.elem_size == 1); CX_TEST_ASSERT(map->base.size == 0); CX_TEST_ASSERT(map->base.allocator == allocator); CX_TEST_ASSERT(!map->base.store_pointer); @@ -52,7 +52,7 @@ CX_TEST_ASSERT(map->base.destructor_data == NULL); cxMapStorePointers(map); CX_TEST_ASSERT(map->base.store_pointer); - CX_TEST_ASSERT(map->base.item_size == sizeof(void *)); + CX_TEST_ASSERT(map->base.elem_size == sizeof(void *)); cxMapStoreObjects(map); CX_TEST_ASSERT(!map->base.store_pointer); @@ -76,7 +76,7 @@ CX_TEST_ASSERT(map->base.size == 0); CX_TEST_ASSERT(map->base.allocator == allocator); CX_TEST_ASSERT(map->base.store_pointer); - CX_TEST_ASSERT(map->base.item_size == sizeof(void *)); + CX_TEST_ASSERT(map->base.elem_size == sizeof(void *)); cxMapDestroy(map); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); @@ -566,7 +566,7 @@ // by using that the values in our test data are unique strings // we can re-use a similar approach as above CxIterator valiter = cxMapIteratorValues(map); - CX_TEST_ASSERT(valiter.elem_size == map->base.item_size); + CX_TEST_ASSERT(valiter.elem_size == map->base.elem_size); CX_TEST_ASSERT(valiter.elem_count == map->base.size); char const** values = calloc(map->base.size, sizeof(char const*)); cx_foreach(char const*, elem, valiter) { diff -r fe0d69d72bcd -r 35bcb3216c0d tests/test_list.c --- a/tests/test_list.c Thu May 23 20:29:28 2024 +0200 +++ b/tests/test_list.c Thu May 23 20:31:37 2024 +0200 @@ -706,7 +706,7 @@ CX_TEST_DO { CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); CX_TEST_ASSERT(list != NULL); - CX_TEST_ASSERT(list->base.item_size == sizeof(int)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(int)); CX_TEST_ASSERT(list->base.simple_destructor == NULL); CX_TEST_ASSERT(list->base.advanced_destructor == NULL); CX_TEST_ASSERT(list->base.destructor_data == NULL); @@ -724,7 +724,7 @@ CxList *list = cxLinkedListCreateSimple(sizeof(int)); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); - CX_TEST_ASSERT(list->base.item_size == sizeof(int)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(int)); CX_TEST_ASSERT(list->base.simple_destructor == NULL); CX_TEST_ASSERT(list->base.advanced_destructor == NULL); CX_TEST_ASSERT(list->base.destructor_data == NULL); @@ -741,7 +741,7 @@ CX_TEST_DO { CX_TEST_ASSERT(!cxListIsStoringPointers(list)); cxListStorePointers(list); - CX_TEST_ASSERT(list->base.item_size == sizeof(void *)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(void *)); CX_TEST_ASSERT(list->cl != NULL); CX_TEST_ASSERT(list->climpl != NULL); CX_TEST_ASSERT(cxListIsStoringPointers(list)); @@ -757,7 +757,7 @@ CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); - CX_TEST_ASSERT(list->base.item_size == sizeof(void*)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(void*)); CX_TEST_ASSERT(list->base.simple_destructor == NULL); CX_TEST_ASSERT(list->base.advanced_destructor == NULL); CX_TEST_ASSERT(list->base.destructor_data == NULL); @@ -776,7 +776,7 @@ CX_TEST_DO { CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); CX_TEST_ASSERT(list != NULL); - CX_TEST_ASSERT(list->base.item_size == sizeof(int)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(int)); CX_TEST_ASSERT(list->base.simple_destructor == NULL); CX_TEST_ASSERT(list->base.advanced_destructor == NULL); CX_TEST_ASSERT(list->base.destructor_data == NULL); @@ -794,7 +794,7 @@ CxList *list = cxArrayListCreateSimple(sizeof(int), 8); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); - CX_TEST_ASSERT(list->base.item_size == sizeof(int)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(int)); CX_TEST_ASSERT(list->base.simple_destructor == NULL); CX_TEST_ASSERT(list->base.advanced_destructor == NULL); CX_TEST_ASSERT(list->base.destructor_data == NULL); @@ -810,7 +810,7 @@ CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); - CX_TEST_ASSERT(list->base.item_size == sizeof(void*)); + CX_TEST_ASSERT(list->base.elem_size == sizeof(void*)); CX_TEST_ASSERT(list->base.simple_destructor == NULL); CX_TEST_ASSERT(list->base.advanced_destructor == NULL); CX_TEST_ASSERT(list->base.destructor_data == NULL); @@ -1207,7 +1207,7 @@ int *testdata = int_test_data_added_to_list(list, isptrlist, len); CxIterator iter = cxListIterator(list); - CX_TEST_ASSERT(iter.elem_size == list->base.item_size); + CX_TEST_ASSERT(iter.elem_size == list->base.elem_size); CX_TEST_ASSERT(iter.elem_count == list->base.size); size_t i = 0; cx_foreach(int*, x, iter) { @@ -1225,7 +1225,7 @@ CX_TEST_ASSERT(i == 0); i = len / 2; CxIterator mut_iter = cxListMutIteratorAt(list, i); - CX_TEST_ASSERT(mut_iter.elem_size == list->base.item_size); + CX_TEST_ASSERT(mut_iter.elem_size == list->base.elem_size); CX_TEST_ASSERT(mut_iter.elem_count == list->base.size); size_t j = 0; cx_foreach(int*, x, mut_iter) {