2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef AVUTIL_DYNARRAY_H
20 #define AVUTIL_DYNARRAY_H
26 * Add an element to a dynamic array.
28 * The array is reallocated when its number of elements reaches powers of 2.
29 * Therefore, the amortized cost of adding an element is constant.
31 * In case of success, the pointer to the array is updated in order to
32 * point to the new grown array, and the size is incremented.
34 * @param av_size_max maximum size of the array, usually the MAX macro of
35 * the type of the size
36 * @param av_elt_size size of the elements in the array, in bytes
37 * @param av_array pointer to the array, must be a lvalue
38 * @param av_size size of the array, must be an integer lvalue
39 * @param av_success statement to execute on success; at this point, the
40 * size variable is not yet incremented
41 * @param av_failure statement to execute on failure; if this happens, the
42 * array and size are not changed; the statement can end
43 * with a return or a goto
45 #define FF_DYNARRAY_ADD(av_size_max, av_elt_size, av_array, av_size, \
46 av_success, av_failure) \
48 size_t av_size_new = (av_size); \
49 if (!((av_size) & ((av_size) - 1))) { \
50 av_size_new = (av_size) ? (av_size) << 1 : 1; \
51 if (av_size_new > (av_size_max) / (av_elt_size)) { \
54 void *av_array_new = \
55 av_realloc((av_array), av_size_new * (av_elt_size)); \
59 (av_array) = av_array_new; \
70 #endif /* AVUTIL_DYNARRAY_H */