Sat, 04 Jan 2025 12:31:28 +0100
change cx_array_reserve() and cx_array_copy() to accept width in bytes instead of bits
src/array_list.c | file | annotate | diff | comparison | revisions | |
src/cx/array_list.h | file | annotate | diff | comparison | revisions |
--- a/src/array_list.c Sat Jan 04 11:55:31 2025 +0100 +++ b/src/array_list.c Sat Jan 04 12:31:28 2025 +0100 @@ -132,21 +132,21 @@ size_t oldcap; size_t oldsize; size_t max_size; - if (width == 0 || width == 8*sizeof(size_t)) { + if (width == 0 || width == sizeof(size_t)) { oldcap = *(size_t*) capacity; oldsize = *(size_t*) size; max_size = SIZE_MAX; - } else if (width == 16) { + } else if (width == sizeof(uint16_t)) { oldcap = *(uint16_t*) capacity; oldsize = *(uint16_t*) size; max_size = UINT16_MAX; - } else if (width == 8) { + } else if (width == sizeof(uint8_t)) { oldcap = *(uint8_t*) capacity; oldsize = *(uint8_t*) size; max_size = UINT8_MAX; } #if CX_WORDSIZE == 64 - else if (width == 32) { + else if (width == sizeof(uint32_t)) { oldcap = *(uint32_t*) capacity; oldsize = *(uint32_t*) size; max_size = UINT32_MAX; @@ -186,15 +186,15 @@ *array = newmem; // store new capacity - if (width == 0 || width == 8*sizeof(size_t)) { + if (width == 0 || width == sizeof(size_t)) { *(size_t*) capacity = newcap; - } else if (width == 16) { + } else if (width == sizeof(uint16_t)) { *(uint16_t*) capacity = (uint16_t) newcap; - } else if (width == 8) { + } else if (width == sizeof(uint8_t)) { *(uint8_t*) capacity = (uint8_t) newcap; } #if CX_WORDSIZE == 64 - else if (width == 32) { + else if (width == sizeof(uint32_t)) { *(uint32_t*) capacity = (uint32_t) newcap; } #endif @@ -225,21 +225,21 @@ size_t oldcap; size_t oldsize; size_t max_size; - if (width == 0 || width == 8*sizeof(size_t)) { + if (width == 0 || width == sizeof(size_t)) { oldcap = *(size_t*) capacity; oldsize = *(size_t*) size; max_size = SIZE_MAX; - } else if (width == 16) { + } else if (width == sizeof(uint16_t)) { oldcap = *(uint16_t*) capacity; oldsize = *(uint16_t*) size; max_size = UINT16_MAX; - } else if (width == 8) { + } else if (width == sizeof(uint8_t)) { oldcap = *(uint8_t*) capacity; oldsize = *(uint8_t*) size; max_size = UINT8_MAX; } #if CX_WORDSIZE == 64 - else if (width == 32) { + else if (width == sizeof(uint32_t)) { oldcap = *(uint32_t*) capacity; oldsize = *(uint32_t*) size; max_size = UINT32_MAX; @@ -303,18 +303,18 @@ // if any of size or capacity changed, store them back if (newsize != oldsize || newcap != oldcap) { - if (width == 0 || width == 8*sizeof(size_t)) { + if (width == 0 || width == sizeof(size_t)) { *(size_t*) capacity = newcap; *(size_t*) size = newsize; - } else if (width == 16) { + } else if (width == sizeof(uint16_t)) { *(uint16_t*) capacity = (uint16_t) newcap; *(uint16_t*) size = (uint16_t) newsize; - } else if (width == 8) { + } else if (width == sizeof(uint8_t)) { *(uint8_t*) capacity = (uint8_t) newcap; *(uint8_t*) size = (uint8_t) newsize; } #if CX_WORDSIZE == 64 - else if (width == 32) { + else if (width == sizeof(uint32_t)) { *(uint32_t*) capacity = (uint32_t) newcap; *(uint32_t*) size = (uint32_t) newsize; }
--- a/src/cx/array_list.h Sat Jan 04 11:55:31 2025 +0100 +++ b/src/cx/array_list.h Sat Jan 04 12:31:28 2025 +0100 @@ -192,13 +192,16 @@ * This function checks if the \p capacity of the array is sufficient to hold * at least \p size plus \p elem_count elements. If not, a reallocation is * performed with the specified \p reallocator. + * You can create your own reallocator by hand or use the convenience function + * cx_array_reallocator(). * * This function can be useful to replace subsequent calls to cx_array_copy() * with one single cx_array_reserve() and then - after guaranteeing a * sufficient capacity - use simple memmove() or memcpy(). * - * The \p width refers to the size and capacity. Both must have the same width. - * Supported are 0, 8, 16, and 32, as well as 64 if running on a 64 bit + * The \p width in bytes refers to the size and capacity. + * Both must have the same width. + * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64 bit * architecture. If set to zero, the native word width is used. * * @param array a pointer to the target array @@ -209,6 +212,7 @@ * @param elem_count the number of expected additional elements * @param reallocator the array reallocator to use * @return zero on success, non-zero on failure + * @see cx_array_reallocator() */ cx_attr_nonnull int cx_array_reserve( @@ -231,9 +235,12 @@ * * If the \p capacity is also insufficient to hold the new data, a reallocation * attempt is made with the specified \p reallocator. + * You can create your own reallocator by hand or use the convenience function + * cx_array_reallocator(). * - * The \p width refers to the size and capacity. Both must have the same width. - * Supported are 0, 8, 16, and 32, as well as 64 if running on a 64 bit + * The \p width in bytes refers to the size and capacity. + * Both must have the same width. + * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64 bit * architecture. If set to zero, the native word width is used. * * @param target a pointer to the target array @@ -246,6 +253,7 @@ * @param elem_count the number of elements to copy * @param reallocator the array reallocator to use * @return zero on success, non-zero on failure + * @see cx_array_reallocator() */ cx_attr_nonnull int cx_array_copy( @@ -275,7 +283,7 @@ */ #define cx_array_simple_copy_a(reallocator, array, index, src, count) \ cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ - 8*sizeof(array##_size), index, src, sizeof((array)[0]), count, \ + sizeof(array##_size), index, src, sizeof((array)[0]), count, \ reallocator) /** @@ -307,7 +315,7 @@ */ #define cx_array_simple_reserve_a(reallocator, array, count) \ cx_array_reserve((void**)&(array), &(array##_size), &(array##_capacity), \ - 8*sizeof(array##_size), sizeof((array)[0]), count, \ + sizeof(array##_size), sizeof((array)[0]), count, \ reallocator) /** @@ -343,7 +351,7 @@ * @return zero on success, non-zero on failure */ #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ - cx_array_copy((void**)(target), size, capacity, 8*sizeof(*(size)), \ + cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \ *(size), elem, elem_size, 1, reallocator) /**