]> git.seodisparate.com - SimpleArchiver/commitdiff
Add single-ptr variants data structures' free fns
authorStephen Seo <seo.disparate@gmail.com>
Fri, 13 Sep 2024 03:51:09 +0000 (12:51 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 13 Sep 2024 03:51:09 +0000 (12:51 +0900)
src/data_structures/hash_map.c
src/data_structures/hash_map.h
src/data_structures/linked_list.c
src/data_structures/linked_list.h
src/data_structures/priority_heap.c
src/data_structures/priority_heap.h

index fb2fb136b572db0184e6659ebfd575c723b07897..4e2df2f203cd9843644414cb5a57ae0861975f9c 100644 (file)
@@ -159,16 +159,21 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
   return hash_map;
 }
 
-void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
-  if (hash_map && *hash_map) {
-    for (size_t idx = 0; idx < (*hash_map)->buckets_size; ++idx) {
-      SDArchiverLinkedList **linked_list = (*hash_map)->buckets + idx;
+void simple_archiver_hash_map_free_single_ptr(SDArchiverHashMap *hash_map) {
+  if (hash_map) {
+    for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) {
+      SDArchiverLinkedList **linked_list = hash_map->buckets + idx;
       simple_archiver_list_free(linked_list);
     }
 
-    free((*hash_map)->buckets);
-    free(*hash_map);
+    free(hash_map->buckets);
+    free(hash_map);
+  }
+}
 
+void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
+  if (hash_map && *hash_map) {
+    simple_archiver_hash_map_free_single_ptr(*hash_map);
     *hash_map = NULL;
   }
 }
index 41e0087404b071f495b09076a49a8da15117d7f1..cc6271c0a24bce351186cd1acba0b732e3f9de05 100644 (file)
@@ -32,6 +32,11 @@ typedef struct SDArchiverHashMap {
 } SDArchiverHashMap;
 
 SDArchiverHashMap *simple_archiver_hash_map_init(void);
+
+/// It is recommended to use the double-pointer version of hash-map free as
+/// that will ensure the variable holding the pointer will end up pointing to
+/// NULL after free.
+void simple_archiver_hash_map_free_single_ptr(SDArchiverHashMap *hash_map);
 void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map);
 
 /// Returns zero on success.
index 475ca2be28a584ea6a259d468567fd92435517f7..dd4717bc2b518641cf2a821296898bf646094e44 100644 (file)
@@ -41,9 +41,9 @@ SDArchiverLinkedList *simple_archiver_list_init(void) {
   return list;
 }
 
-void simple_archiver_list_free(SDArchiverLinkedList **list) {
-  if (list && *list) {
-    SDArchiverLLNode *node = (*list)->head;
+void simple_archiver_list_free_single_ptr(SDArchiverLinkedList *list) {
+  if (list) {
+    SDArchiverLLNode *node = list->head;
     SDArchiverLLNode *prev;
     while (node) {
       prev = node;
@@ -58,7 +58,13 @@ void simple_archiver_list_free(SDArchiverLinkedList **list) {
       }
     }
 
-    free(*list);
+    free(list);
+  }
+}
+
+void simple_archiver_list_free(SDArchiverLinkedList **list) {
+  if (list && *list) {
+    simple_archiver_list_free_single_ptr(*list);
     *list = NULL;
   }
 }
index 04c25dab62f0c57afbeeb198a01bf13d73861cf4..6c612d54e424cf95d2aa51afb2b9779a6d01caee 100644 (file)
@@ -35,6 +35,11 @@ typedef struct SDArchiverLinkedList {
 } SDArchiverLinkedList;
 
 SDArchiverLinkedList *simple_archiver_list_init(void);
+
+/// It is recommended to use the double-pointer version of list free as that
+/// will ensure the variable holding the pointer will end up pointing to NULL
+/// after free.
+void simple_archiver_list_free_single_ptr(SDArchiverLinkedList *list);
 void simple_archiver_list_free(SDArchiverLinkedList **list);
 
 /// Returns 0 on success. Puts data at the end of the list
index 437aea2406d09281ba0b845f5bbfcff52bacf091..67d438a31e0a585a83e1ffe2129bf8642a9b0bcb 100644 (file)
@@ -77,23 +77,29 @@ SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
   return priority_heap;
 }
 
-void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) {
-  if (priority_heap && *priority_heap) {
-    for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) {
-      if ((*priority_heap)->nodes[idx].is_valid != 0) {
-        if ((*priority_heap)->nodes[idx].data_cleanup_fn) {
-          (*priority_heap)
-              ->nodes[idx]
-              .data_cleanup_fn((*priority_heap)->nodes[idx].data);
+void simple_archiver_priority_heap_free_single_ptr(
+    SDArchiverPHeap *priority_heap) {
+  if (priority_heap) {
+    for (size_t idx = 1; idx < priority_heap->size + 1; ++idx) {
+      if (priority_heap->nodes[idx].is_valid != 0) {
+        if (priority_heap->nodes[idx].data_cleanup_fn) {
+          priority_heap->nodes[idx].data_cleanup_fn(
+              priority_heap->nodes[idx].data);
         } else {
-          free((*priority_heap)->nodes[idx].data);
+          free(priority_heap->nodes[idx].data);
         }
-        (*priority_heap)->nodes[idx].is_valid = 0;
+        priority_heap->nodes[idx].is_valid = 0;
       }
     }
 
-    free((*priority_heap)->nodes);
-    free(*priority_heap);
+    free(priority_heap->nodes);
+    free(priority_heap);
+  }
+}
+
+void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) {
+  if (priority_heap && *priority_heap) {
+    simple_archiver_priority_heap_free_single_ptr(*priority_heap);
     *priority_heap = NULL;
   }
 }
index 0ec1b02de06ffdd489b12d2c4c33337b2e8decfd..03b142751a3eedb24b39b337a2aba8e7a6700f81 100644 (file)
@@ -43,6 +43,12 @@ int simple_archiver_priority_heap_default_less(long long a, long long b);
 SDArchiverPHeap *simple_archiver_priority_heap_init(void);
 SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
     int (*less_fn)(long long, long long));
+
+/// It is recommended to use the double-pointer version of priority-heap free
+/// as that will ensure the variable holding the pointer will end up pointing
+/// to NULL after free.
+void simple_archiver_priority_heap_free_single_ptr(
+    SDArchiverPHeap *priority_heap);
 void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap);
 
 /// If data_cleanup_fn is NULL, then "free()" is used on data when freed.