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