From d9148030b0026c8f3973870d639ab8c35739bda9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 22 Apr 2025 17:23:34 +0900 Subject: [PATCH] Update chunked_array_pop to optionally not cleanup Another parameter was added to the chunked_array_pop function to be able to control whether the returned element has the cleanup function run on it before a newly allocated copy of it is returned. --- src/data_structures/chunked_array.c | 5 +++-- src/data_structures/chunked_array.h | 4 +++- src/data_structures/test.c | 13 ++++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/data_structures/chunked_array.c b/src/data_structures/chunked_array.c index c1bbb95..6748eb8 100644 --- a/src/data_structures/chunked_array.c +++ b/src/data_structures/chunked_array.c @@ -138,7 +138,8 @@ int simple_archiver_chunked_array_push(SDArchiverChunkedArr *chunked_array, return 0; } -void *simple_archiver_chunked_array_pop(SDArchiverChunkedArr *chunked_array) { +void *simple_archiver_chunked_array_pop(SDArchiverChunkedArr *chunked_array, + int no_cleanup) { if (chunked_array->chunk_count == 0 || !chunked_array->array) { return 0; } @@ -179,7 +180,7 @@ void *simple_archiver_chunked_array_pop(SDArchiverChunkedArr *chunked_array) { + inner_idx * chunked_array->elem_size, chunked_array->elem_size); - if (chunked_array->elem_cleanup_fn) { + if (no_cleanup == 0 && chunked_array->elem_cleanup_fn) { chunked_array->elem_cleanup_fn( (char*)chunked_array->array[chunked_array->chunk_count - 1] + chunked_array->last_size * chunked_array->elem_size); diff --git a/src/data_structures/chunked_array.h b/src/data_structures/chunked_array.h index c4e5e6c..e769162 100644 --- a/src/data_structures/chunked_array.h +++ b/src/data_structures/chunked_array.h @@ -49,7 +49,9 @@ int simple_archiver_chunked_array_push(SDArchiverChunkedArr *, void *to_copy); /// Returns non-null on success. /// Returned ptr is newly allocated and must be free'd. -void *simple_archiver_chunked_array_pop(SDArchiverChunkedArr *); +/// If "no_cleanup" is non-zero, then the cleanup function will not be run on +/// the element before a newly allocated copy of it is returned. +void *simple_archiver_chunked_array_pop(SDArchiverChunkedArr *, int no_cleanup); /// Returns non-zero if an element was removed. int simple_archiver_chunked_array_pop_no_ret(SDArchiverChunkedArr *); diff --git a/src/data_structures/test.c b/src/data_structures/test.c index b77ddda..e425f0e 100644 --- a/src/data_structures/test.c +++ b/src/data_structures/test.c @@ -438,10 +438,17 @@ int main(void) { if (idx > 50) { CHECK_TRUE( simple_archiver_chunked_array_pop_no_ret(&chunked_array) != 0); + } else if (idx > 70) { + t_ptr = simple_archiver_chunked_array_pop(&chunked_array, 0); + CHECK_TRUE(t_ptr); + if (t_ptr) { + free(t_ptr); + } } else { - t_ptr = simple_archiver_chunked_array_pop(&chunked_array); + t_ptr = simple_archiver_chunked_array_pop(&chunked_array, 1); CHECK_TRUE(t_ptr); if (t_ptr) { + cleanup_test_struct_fn(t_ptr); free(t_ptr); } } @@ -474,14 +481,14 @@ int main(void) { } for (int idx = 100; idx-- > 0;) { - int_ptr = simple_archiver_chunked_array_pop(&chunked_array); + int_ptr = simple_archiver_chunked_array_pop(&chunked_array, 0); CHECK_TRUE(int_ptr); CHECK_TRUE(*int_ptr == idx); free(int_ptr); } for (int idx = 0; idx < 10; ++idx) { - int_ptr = simple_archiver_chunked_array_pop(&chunked_array); + int_ptr = simple_archiver_chunked_array_pop(&chunked_array, 0); CHECK_FALSE(int_ptr); } -- 2.49.0