FFmpeg
Functions

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...
 
voidav_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...
 

Detailed Description

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.

type **array = NULL; //< an array of pointers to values
int nb = 0; //< a variable to keep track of the length of the array
type to_be_added = ...;
type to_be_added2 = ...;
av_dynarray_add(&array, &nb, &to_be_added);
if (nb == 0)
return AVERROR(ENOMEM);
av_dynarray_add(&array, &nb, &to_be_added2);
if (nb == 0)
return AVERROR(ENOMEM);
// Now:
// nb == 2
// &to_be_added == array[0]
// &to_be_added2 == array[1]
av_freep(&array);

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.

type *array = NULL; //< an array of values
int nb = 0; //< a variable to keep track of the length of the array
type to_be_added = ...;
type to_be_added2 = ...;
type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), NULL);
if (!addr)
return AVERROR(ENOMEM);
memcpy(addr, &to_be_added, sizeof(to_be_added));
// Shortcut of the above.
type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array),
(const void *)&to_be_added2);
if (!addr)
return AVERROR(ENOMEM);
// Now:
// nb == 2
// to_be_added == array[0]
// to_be_added2 == array[1]
av_freep(&array);

Function Documentation

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.

Parameters
[in,out]tab_ptrPointer to the array to grow
[in,out]nb_ptrPointer to the number of elements in the array
[in]elemElement to add
See also
av_dynarray_add_nofree(), av_dynarray2_add()

Definition at line 308 of file mem.c.

Referenced by decode_styl(), main(), mov_text_style_cb(), and mov_text_tx3g().

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.

Returns
>=0 on success, negative otherwise
See also
av_dynarray_add(), av_dynarray2_add()

Definition at line 294 of file mem.c.

Referenced by av_bsf_list_append(), ff_alsa_get_device_list(), ff_decklink_list_devices(), ff_fbdev_get_device_list(), ff_id3v2_parse_chapters(), ff_stream_add_bitstream_filter(), mpegts_add_service(), pulse_add_detected_device(), tee_write_header(), and v4l2_get_device_list().

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.

Parameters
[in,out]tab_ptrPointer to the array to grow
[in,out]nb_ptrPointer to the number of elements in the array
[in]elem_sizeSize in bytes of an element in the array
[in]elem_dataPointer to the data of the element to add. If NULL, the space of the newly added element is allocated but left uninitialized.
Returns
Pointer to the data of the element to copy in the newly allocated space
See also
av_dynarray_add(), av_dynarray_add_nofree()

Definition at line 322 of file mem.c.

Referenced by color_get(), color_inc(), and ip_parse_addr_list().