X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fdynamic-array.h;h=a370774049bd680df09ccc2716b0ba38e401be8b;hb=3afa94aeca5a0daae40fd7b6cc96b7e4c150c7d8;hp=e97b51d5d0d1d0ff5eebf9e7cddd1fedfa9112f2;hpb=93bed9fe8f48c11b7bb1224db36d82404cea080d;p=lttng-tools.git diff --git a/src/common/dynamic-array.h b/src/common/dynamic-array.h index e97b51d5d..a37077404 100644 --- a/src/common/dynamic-array.h +++ b/src/common/dynamic-array.h @@ -1,25 +1,18 @@ /* - * Copyright (C) 2019 - Jérémie Galarneau + * Copyright (C) 2019 Jérémie Galarneau * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License, version 2.1 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: LGPL-2.1-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef LTTNG_DYNAMIC_ARRAY_H #define LTTNG_DYNAMIC_ARRAY_H #include -#include + +#if defined(__cplusplus) +extern "C" { +#endif typedef void (*lttng_dynamic_array_element_destructor)(void *element); typedef void (*lttng_dynamic_pointer_array_destructor)(void *ptr); @@ -39,7 +32,6 @@ struct lttng_dynamic_pointer_array { * Initialize a resizable array of fixed-size elements. This performs no * allocation and can't fail. */ -LTTNG_HIDDEN void lttng_dynamic_array_init(struct lttng_dynamic_array *array, size_t element_size, lttng_dynamic_array_element_destructor destructor); @@ -62,17 +54,34 @@ 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(). + */ +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. * * element is a pointer to the element to add (copy) to the array. */ -LTTNG_HIDDEN int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array, const void *element); @@ -81,14 +90,14 @@ int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array, * decreased by one and the following elements are shifted to take its place * (when applicable). */ -LTTNG_HIDDEN int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array, size_t element_index); /* Release any memory used by the dynamic array. */ -LTTNG_HIDDEN void lttng_dynamic_array_reset(struct lttng_dynamic_array *array); +/* Remove all elements from the dynamic array. */ +void lttng_dynamic_array_clear(struct lttng_dynamic_array *array); /* * Specialization of lttng_dynamic_array for pointers. This utility @@ -101,7 +110,6 @@ void lttng_dynamic_array_reset(struct lttng_dynamic_array *array); * Initialize a resizable array of fixed-size elements. This performs no * allocation and can't fail. */ -LTTNG_HIDDEN void lttng_dynamic_pointer_array_init( struct lttng_dynamic_pointer_array *array, lttng_dynamic_pointer_array_destructor destructor); @@ -117,18 +125,34 @@ size_t lttng_dynamic_pointer_array_get_count( } /* - * Returns a pointer to the element. Mutating operations on the array invalidate - * the returned pointer. + * Returns the pointer at index `index`. */ static inline void *lttng_dynamic_pointer_array_get_pointer( const struct lttng_dynamic_pointer_array *array, size_t index) { - void **element = lttng_dynamic_array_get_element(&array->array, index); + void **element = (void **) lttng_dynamic_array_get_element(&array->array, index); 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 = (void **) 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 @@ -146,16 +170,19 @@ int lttng_dynamic_pointer_array_add_pointer( * count is decreased by one and the following pointers are shifted to * take the place of the removed pointer (if applicable). */ -static inline int lttng_dynamic_pointer_array_remove_pointer( - struct lttng_dynamic_pointer_array *array, size_t index) -{ - return lttng_dynamic_array_remove_element(&array->array, index); -} + struct lttng_dynamic_pointer_array *array, size_t index); /* Release any memory used by the dynamic array. */ -LTTNG_HIDDEN void lttng_dynamic_pointer_array_reset( struct lttng_dynamic_pointer_array *array); +/* Remove all elements from the dynamic pointer array. */ +void lttng_dynamic_pointer_array_clear( + struct lttng_dynamic_pointer_array *array); + +#if defined(__cplusplus) +} +#endif + #endif /* LTTNG_DYNAMIC_ARRAY_H */