diff --git a/src/ptk_list.c b/src/ptk_list.c index 0af1f5d..7a590c5 100644 --- a/src/ptk_list.c +++ b/src/ptk_list.c @@ -22,24 +22,17 @@ bool _grow_PtkList(void **data, uint32_t *allocated, size_t element_size) { } bool _add_PtkList(void **data, uint32_t *size, uint32_t *allocated, void *elements, size_t element_count, size_t element_size) { - size_t elements_added = 0; - - for (size_t i = 0; i < element_count; ++i) { - if (*size == *allocated) { - if (!_grow_PtkList(data, allocated, element_size)) { - break; - } + while (*allocated < *size + element_count) { + if (!_grow_PtkList(data, allocated, element_size)) { + return false; } - - uint8_t *element = ((uint8_t *)elements) + (i * element_size); - - uint8_t *target = ((uint8_t *)*data) + (*size * element_size); - memcpy(target, element, element_size); - elements_added += 1; - *size += 1; } - return elements_added == element_count; + uint8_t *target = ((uint8_t *)*data) + (*size * element_size); + memcpy(target, (uint8_t *)elements, element_size * element_count); + *size += element_count; + + return true; } bool _remove_PtkList(void *data, uint32_t *size, void *elements, size_t element_count, size_t element_size) {