From: Mathieu Desnoyers Date: Mon, 9 Dec 2019 14:50:30 +0000 (-0500) Subject: common dynamic-array: Introduce dynamic array clear X-Git-Tag: v2.12.0-rc1~149 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=74465ffbb5ead0a3d7ba6342e7baf16f4fa662f9;p=lttng-tools.git common dynamic-array: Introduce dynamic array clear Signed-off-by: Mathieu Desnoyers Change-Id: I35d25b054a040c2d23b4b1c1a8137ef59ee3c7d1 Signed-off-by: Jérémie Galarneau --- diff --git a/src/common/dynamic-array.c b/src/common/dynamic-array.c index 094e269a4..bea903caf 100644 --- a/src/common/dynamic-array.c +++ b/src/common/dynamic-array.c @@ -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; +} diff --git a/src/common/dynamic-array.h b/src/common/dynamic-array.h index 483db3631..c279a921e 100644 --- a/src/common/dynamic-array.h +++ b/src/common/dynamic-array.h @@ -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 */