FFmpeg
Macros
dynarray.h File Reference
#include "log.h"
#include "mem.h"

Go to the source code of this file.

Macros

#define FF_DYNARRAY_ADD(av_size_max, av_elt_size, av_array, av_size, av_success, av_failure)
 Add an element to a dynamic array. More...
 

Macro Definition Documentation

◆ FF_DYNARRAY_ADD

#define FF_DYNARRAY_ADD (   av_size_max,
  av_elt_size,
  av_array,
  av_size,
  av_success,
  av_failure 
)
Value:
do { \
size_t av_size_new = (av_size); \
if (!((av_size) & ((av_size) - 1))) { \
av_size_new = (av_size) ? (av_size) << 1 : 1; \
if (av_size_new > (av_size_max) / (av_elt_size)) { \
av_size_new = 0; \
} else { \
void *av_array_new = \
av_realloc((av_array), av_size_new * (av_elt_size)); \
if (!av_array_new) \
av_size_new = 0; \
else \
(av_array) = av_array_new; \
} \
} \
if (av_size_new) { \
{ av_success } \
(av_size)++; \
} else { \
av_failure \
} \
} while (0)

Add an element 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 size is incremented.

Parameters
av_size_maxmaximum size of the array, usually the MAX macro of the type of the size
av_elt_sizesize of the elements in the array, in bytes
av_arraypointer to the array, must be a lvalue
av_sizesize of the array, must be an integer lvalue
av_successstatement to execute on success; at this point, the size variable is not yet incremented
av_failurestatement to execute on failure; if this happens, the array and size are not changed; the statement can end with a return or a goto

Definition at line 45 of file dynarray.h.