]> git.sesse.net Git - ffmpeg/blob - libavutil/mem_internal.h
lavu: move LOCAL_ALIGNED from internal.h to mem_internal.h
[ffmpeg] / libavutil / mem_internal.h
1 /*
2  * Copyright (c) 2002 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #ifndef AVUTIL_MEM_INTERNAL_H
22 #define AVUTIL_MEM_INTERNAL_H
23
24 #include "config.h"
25
26 #include <stdint.h>
27
28 #include "avassert.h"
29 #include "mem.h"
30
31 // Some broken preprocessors need a second expansion
32 // to be forced to tokenize __VA_ARGS__
33 #define E1(x) x
34
35 #define LOCAL_ALIGNED_A(a, t, v, s, o, ...)             \
36     uint8_t la_##v[sizeof(t s o) + (a)];                \
37     t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
38
39 #define LOCAL_ALIGNED_D(a, t, v, s, o, ...)             \
40     DECLARE_ALIGNED(a, t, la_##v) s o;                  \
41     t (*v) o = la_##v
42
43 #define LOCAL_ALIGNED(a, t, v, ...) LOCAL_ALIGNED_##a(t, v, __VA_ARGS__)
44
45 #if HAVE_LOCAL_ALIGNED
46 #   define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_D(4, t, v, __VA_ARGS__,,))
47 #else
48 #   define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_A(4, t, v, __VA_ARGS__,,))
49 #endif
50
51 #if HAVE_LOCAL_ALIGNED
52 #   define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
53 #else
54 #   define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_A(8, t, v, __VA_ARGS__,,))
55 #endif
56
57 #if HAVE_LOCAL_ALIGNED
58 #   define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
59 #else
60 #   define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_A(16, t, v, __VA_ARGS__,,))
61 #endif
62
63 #if HAVE_LOCAL_ALIGNED
64 #   define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_D(32, t, v, __VA_ARGS__,,))
65 #else
66 #   define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_A(32, t, v, __VA_ARGS__,,))
67 #endif
68
69 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
70 {
71     void *val;
72
73     memcpy(&val, ptr, sizeof(val));
74     if (min_size <= *size) {
75         av_assert0(val || !min_size);
76         return 0;
77     }
78     min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
79     av_freep(ptr);
80     val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
81     memcpy(ptr, &val, sizeof(val));
82     if (!val)
83         min_size = 0;
84     *size = min_size;
85     return 1;
86 }
87 #endif /* AVUTIL_MEM_INTERNAL_H */