X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fdynamic-array.h;h=be27cc4958d51a2f2b42320044e371a0e664b492;hb=a0377dfefe40662ba7d68617bce6ff467114136c;hp=208dd1956b41263df7f8336605f22e082bff21ac;hpb=ab5be9fa2eb5ba9600a82cd18fd3cfcbac69169a;p=lttng-tools.git diff --git a/src/common/dynamic-array.h b/src/common/dynamic-array.h index 208dd1956..be27cc495 100644 --- a/src/common/dynamic-array.h +++ b/src/common/dynamic-array.h @@ -9,7 +9,6 @@ #define LTTNG_DYNAMIC_ARRAY_H #include -#include typedef void (*lttng_dynamic_array_element_destructor)(void *element); typedef void (*lttng_dynamic_pointer_array_destructor)(void *ptr); @@ -52,10 +51,29 @@ static inline void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array *array, size_t element_index) { - assert(element_index < array->size); + LTTNG_ASSERT(element_index < array->size); return array->buffer.data + (element_index * array->element_size); } +/* + * Set the array's element count to new_element_count. Any added element will + * be zeroed. + * + * Be careful to expand the array's element count _before_ calling out external + * APIs (e.g. read(3)) which may populate the buffer as setting the element + * count after will zero-out the result of the operation. + * + * Shrinking an array does not zero the old content. If the buffer may contain + * sensititve information, it must be cleared manually _before_ changing the + * size. + * + * NOTE: It is striclty _invalid_ to access memory after _size_, regardless + * of prior calls to set_capacity(). + */ +LTTNG_HIDDEN +int lttng_dynamic_array_set_count(struct lttng_dynamic_array *array, + size_t new_element_count); + /* * Add an element to the end of a dynamic array. The array's element count is * increased by one and its underlying capacity is adjusted automatically. @@ -121,6 +139,23 @@ void *lttng_dynamic_pointer_array_get_pointer( return *element; } +/* + * Returns the pointer at index `index`, sets the array slot to NULL. Does not + * run the destructor. + */ + +static inline +void *lttng_dynamic_pointer_array_steal_pointer( + struct lttng_dynamic_pointer_array *array, size_t index) +{ + void **p_element = lttng_dynamic_array_get_element(&array->array, index); + void *element = *p_element; + + *p_element = NULL; + + return element; +} + /* * Add a pointer to the end of a dynamic pointer array. The array's element * count is increased by one and its underlying capacity is adjusted