FFmpeg 5.1.6
|
Utilities to make an array grow when needed. More...
Functions | |
void | av_dynarray_add (void *tab_ptr, int *nb_ptr, void *elem) |
Add the pointer to an element to a dynamic array. More... | |
av_warn_unused_result int | av_dynarray_add_nofree (void *tab_ptr, int *nb_ptr, void *elem) |
Add an element to a dynamic array. More... | |
void * | av_dynarray2_add (void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data) |
Add an element of size elem_size to a dynamic array. More... | |
Utilities to make an array grow when needed.
Sometimes, the programmer would want to have an array that can grow when needed. The libavutil dynamic array utilities fill that need.
libavutil supports two systems of appending elements onto a dynamically allocated array, the first one storing the pointer to the value in the array, and the second storing the value directly. In both systems, the caller is responsible for maintaining a variable containing the length of the array, as well as freeing of the array after use.
The first system stores pointers to values in a block of dynamically allocated memory. Since only pointers are stored, the function does not need to know the size of the type. Both av_dynarray_add() and av_dynarray_add_nofree() implement this system.
The second system stores the value directly in a block of memory. As a result, the function has to know the size of the type. av_dynarray2_add() implements this mechanism.
void av_dynarray_add | ( | void * | tab_ptr, |
int * | nb_ptr, | ||
void * | elem | ||
) |
Add the pointer to an element to a dynamic array.
The array to grow is supposed to be an array of pointers to structures, and the element to add must be a pointer to an already allocated structure.
The array is reallocated when its size reaches powers of 2. Therefore, the amortized cost of adding an element is constant.
In case of success, the pointer to the array is updated in order to point to the new grown array, and the number pointed to by nb_ptr
is incremented. In case of failure, the array is freed, *tab_ptr
is set to NULL
and *nb_ptr
is set to 0.
[in,out] | tab_ptr | Pointer to the array to grow |
[in,out] | nb_ptr | Pointer to the number of elements in the array |
[in] | elem | Element to add |
av_warn_unused_result int av_dynarray_add_nofree | ( | void * | tab_ptr, |
int * | nb_ptr, | ||
void * | elem | ||
) |
Add an element to a dynamic array.
Function has the same functionality as av_dynarray_add(), but it doesn't free memory on fails. It returns error code instead and leave current buffer untouched.
void * av_dynarray2_add | ( | void ** | tab_ptr, |
int * | nb_ptr, | ||
size_t | elem_size, | ||
const uint8_t * | elem_data | ||
) |
Add an element of size elem_size
to a dynamic array.
The array is reallocated when its number of elements reaches powers of 2. Therefore, the amortized cost of adding an element is constant.
In case of success, the pointer to the array is updated in order to point to the new grown array, and the number pointed to by nb_ptr
is incremented. In case of failure, the array is freed, *tab_ptr
is set to NULL
and *nb_ptr
is set to 0.
[in,out] | tab_ptr | Pointer to the array to grow |
[in,out] | nb_ptr | Pointer to the number of elements in the array |
[in] | elem_size | Size in bytes of an element in the array |
[in] | elem_data | Pointer to the data of the element to add. If NULL , the space of the newly added element is allocated but left uninitialized. |