FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dynarray.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #ifndef AVUTIL_DYNARRAY_H
20 #define AVUTIL_DYNARRAY_H
21 
22 #include "log.h"
23 #include "mem.h"
24 
25 /**
26  * Add an element of to a dynamic array.
27  *
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.
30  *
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.
33  *
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
44  */
45 #define AV_DYNARRAY_ADD(av_size_max, av_elt_size, av_array, av_size, \
46  av_success, av_failure) \
47  do { \
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)) { \
52  av_size_new = 0; \
53  } else { \
54  void *av_array_new = \
55  av_realloc((av_array), av_size_new * (av_elt_size)); \
56  if (!av_array_new) \
57  av_size_new = 0; \
58  else \
59  (av_array) = av_array_new; \
60  } \
61  } \
62  if (av_size_new) { \
63  { av_success } \
64  (av_size)++; \
65  } else { \
66  av_failure \
67  } \
68  } while (0)
69 
70 #endif /* AVUTIL_DYNARRAY_H */
memory handling functions