common dynamic-array: Introduce dynamic array clear
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 9 Dec 2019 14:50:30 +0000 (09:50 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 19 Dec 2019 22:23:57 +0000 (17:23 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I35d25b054a040c2d23b4b1c1a8137ef59ee3c7d1
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/dynamic-array.c
src/common/dynamic-array.h

index 094e269a4fe6e9d8245f517ab1d9e692a5ba8d94..bea903cafeae2a115b1fc1816345a0d50017dc8f 100644 (file)
@@ -87,6 +87,22 @@ void lttng_dynamic_array_reset(struct lttng_dynamic_array *array)
        array->size = 0;
 }
 
+LTTNG_HIDDEN
+void lttng_dynamic_array_clear(struct lttng_dynamic_array *array)
+{
+       if (array->destructor) {
+               size_t i;
+
+               for (i = 0; i < lttng_dynamic_array_get_count(array); i++) {
+                       array->destructor(lttng_dynamic_array_get_element(array,
+                                       i));
+               }
+       }
+
+       lttng_dynamic_buffer_set_size(&array->buffer, 0);
+       array->size = 0;
+}
+
 LTTNG_HIDDEN
 void lttng_dynamic_pointer_array_init(
                struct lttng_dynamic_pointer_array *array,
@@ -138,3 +154,28 @@ void lttng_dynamic_pointer_array_reset(
        }
        lttng_dynamic_array_reset(&array->array);
 }
+
+LTTNG_HIDDEN
+void lttng_dynamic_pointer_array_clear(
+               struct lttng_dynamic_pointer_array *array)
+{
+       const lttng_dynamic_array_element_destructor destructor =
+                       array->array.destructor;
+
+       /*
+        * Prevent the destructor from being used by the underlying
+        * dynamic array.
+        */
+       array->array.destructor = NULL;
+       if (destructor) {
+               size_t i, count = lttng_dynamic_pointer_array_get_count(array);
+
+               for (i = 0; i < count; i++) {
+                       void *ptr = lttng_dynamic_pointer_array_get_pointer(
+                                       array, i);
+                       destructor(ptr);
+               }
+       }
+       lttng_dynamic_array_clear(&array->array);
+       array->array.destructor = destructor;
+}
index 483db363122ebbc5a6339cd5ec144a2181922066..c279a921e3616b8b6016f4d93b71dd216ff69d54 100644 (file)
@@ -89,6 +89,9 @@ int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array,
 LTTNG_HIDDEN
 void lttng_dynamic_array_reset(struct lttng_dynamic_array *array);
 
+/* Remove all elements from the dynamic array. */
+LTTNG_HIDDEN
+void lttng_dynamic_array_clear(struct lttng_dynamic_array *array);
 
 /*
  * Specialization of lttng_dynamic_array for pointers. This utility
@@ -154,4 +157,9 @@ LTTNG_HIDDEN
 void lttng_dynamic_pointer_array_reset(
                struct lttng_dynamic_pointer_array *array);
 
+/* Remove all elements from the dynamic pointer array. */
+LTTNG_HIDDEN
+void lttng_dynamic_pointer_array_clear(
+               struct lttng_dynamic_pointer_array *array);
+
 #endif /* LTTNG_DYNAMIC_ARRAY_H */
This page took 0.03477 seconds and 4 git commands to generate.