2
* This file is part of mpv.
4
* mpv is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* mpv 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 General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with mpv; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
#include "compat/compiler.h"
25
#define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0]))
26
#define MP_GROW_ARRAY(p, nextidx) do { \
27
if ((nextidx) == MP_TALLOC_ELEMS(p)) \
28
(p) = talloc_realloc_size(NULL, p, talloc_get_size(p) * 2); } while (0)
29
#define MP_RESIZE_ARRAY(ctx, p, count) do { \
30
(p) = talloc_realloc_size((ctx), p, (count) * sizeof((p)[0])); } while (0)
32
#define MP_TARRAY_GROW(ctx, p, nextidx) \
34
size_t nextidx_ = (nextidx); \
35
size_t nelems_ = MP_TALLOC_ELEMS(p); \
36
if (nextidx_ >= nelems_) \
37
(p) = talloc_realloc_size(ctx, p, \
38
(nextidx_ + 1) * sizeof((p)[0]) * 2);\
41
#define MP_TARRAY_APPEND(ctx, p, idxvar, ...) \
43
MP_TARRAY_GROW(ctx, p, idxvar); \
44
(p)[(idxvar)] = (MP_EXPAND_ARGS(__VA_ARGS__));\
48
// Doesn't actually free any memory, or do any other talloc calls.
49
#define MP_TARRAY_REMOVE_AT(p, idxvar, at) \
52
assert(at_ <= (idxvar)); \
53
memmove((p) + at_, (p) + at_ + 1, \
54
((idxvar) - at_ - 1) * sizeof((p)[0])); \
58
#define talloc_struct(ctx, type, ...) \
59
talloc_memdup(ctx, &(type) MP_EXPAND_ARGS(__VA_ARGS__), sizeof(type))