mirror of https://github.com/mpv-player/mpv
mp_talloc: simplify a bit
Implement MP_GROW_ARRAY using MP_TARRAY_GROW. MP_GROW_ARRAY is basically the earlier version of MP_TARRAY_GROW, and had different semantics. When I added MP_TARRAY_GROW, I didn't dare to change it, but I think all code that relied on the exact semantics of MP_GROW_ARRAY is gone now, or the difference doesn't matter anyway. The semantic change is that now (n+1)*2 elements are preallocated instead of n*2. Also, implement MP_TARRAY_GROW using MP_RESIZE_ARRAY, which saves 1 line of code. In future, these macros should be part of TA.
This commit is contained in:
parent
d7287d60dd
commit
d2d0bc31bf
|
@ -23,21 +23,19 @@
|
||||||
#include "compat/compiler.h"
|
#include "compat/compiler.h"
|
||||||
|
|
||||||
#define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0]))
|
#define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0]))
|
||||||
#define MP_GROW_ARRAY(p, nextidx) do { \
|
|
||||||
if ((nextidx) == MP_TALLOC_ELEMS(p)) \
|
|
||||||
(p) = talloc_realloc_size(NULL, p, talloc_get_size(p) * 2); } while (0)
|
|
||||||
#define MP_RESIZE_ARRAY(ctx, p, count) do { \
|
#define MP_RESIZE_ARRAY(ctx, p, count) do { \
|
||||||
(p) = talloc_realloc_size((ctx), p, (count) * sizeof((p)[0])); } while (0)
|
(p) = talloc_realloc_size((ctx), p, (count) * sizeof((p)[0])); } while (0)
|
||||||
|
|
||||||
#define MP_TARRAY_GROW(ctx, p, nextidx) \
|
#define MP_TARRAY_GROW(ctx, p, nextidx) \
|
||||||
do { \
|
do { \
|
||||||
size_t nextidx_ = (nextidx); \
|
size_t nextidx_ = (nextidx); \
|
||||||
size_t nelems_ = MP_TALLOC_ELEMS(p); \
|
if (nextidx_ >= MP_TALLOC_ELEMS(p)) \
|
||||||
if (nextidx_ >= nelems_) \
|
MP_RESIZE_ARRAY(ctx, p, (nextidx_ + 1) * 2);\
|
||||||
(p) = talloc_realloc_size(ctx, p, \
|
|
||||||
(nextidx_ + 1) * sizeof((p)[0]) * 2);\
|
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define MP_GROW_ARRAY(p, nextidx) MP_TARRAY_GROW(NULL, p, nextidx)
|
||||||
|
|
||||||
#define MP_TARRAY_APPEND(ctx, p, idxvar, ...) \
|
#define MP_TARRAY_APPEND(ctx, p, idxvar, ...) \
|
||||||
do { \
|
do { \
|
||||||
MP_TARRAY_GROW(ctx, p, idxvar); \
|
MP_TARRAY_GROW(ctx, p, idxvar); \
|
||||||
|
|
Loading…
Reference in New Issue