FFmpeg
mem_internal.h
Go to the documentation of this file.
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 #include "version.h"
31 
32 #if !FF_API_DECLARE_ALIGNED
33 /**
34  * @def DECLARE_ALIGNED(n,t,v)
35  * Declare a variable that is aligned in memory.
36  *
37  * @code{.c}
38  * DECLARE_ALIGNED(16, uint16_t, aligned_int) = 42;
39  * DECLARE_ALIGNED(32, uint8_t, aligned_array)[128];
40  *
41  * // The default-alignment equivalent would be
42  * uint16_t aligned_int = 42;
43  * uint8_t aligned_array[128];
44  * @endcode
45  *
46  * @param n Minimum alignment in bytes
47  * @param t Type of the variable (or array element)
48  * @param v Name of the variable
49  */
50 
51 /**
52  * @def DECLARE_ASM_ALIGNED(n,t,v)
53  * Declare an aligned variable appropriate for use in inline assembly code.
54  *
55  * @code{.c}
56  * DECLARE_ASM_ALIGNED(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
57  * @endcode
58  *
59  * @param n Minimum alignment in bytes
60  * @param t Type of the variable (or array element)
61  * @param v Name of the variable
62  */
63 
64 /**
65  * @def DECLARE_ASM_CONST(n,t,v)
66  * Declare a static constant aligned variable appropriate for use in inline
67  * assembly code.
68  *
69  * @code{.c}
70  * DECLARE_ASM_CONST(16, uint64_t, pw_08) = UINT64_C(0x0008000800080008);
71  * @endcode
72  *
73  * @param n Minimum alignment in bytes
74  * @param t Type of the variable (or array element)
75  * @param v Name of the variable
76  */
77 
78 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
79  #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
80  #define DECLARE_ASM_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
81  #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
82 #elif defined(__DJGPP__)
83  #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (FFMIN(n, 16)))) v
84  #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
85  #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (FFMIN(n, 16)))) v
86 #elif defined(__GNUC__) || defined(__clang__)
87  #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
88  #define DECLARE_ASM_ALIGNED(n,t,v) t av_used __attribute__ ((aligned (n))) v
89  #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
90 #elif defined(_MSC_VER)
91  #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
92  #define DECLARE_ASM_ALIGNED(n,t,v) __declspec(align(n)) t v
93  #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
94 #else
95  #define DECLARE_ALIGNED(n,t,v) t v
96  #define DECLARE_ASM_ALIGNED(n,t,v) t v
97  #define DECLARE_ASM_CONST(n,t,v) static const t v
98 #endif
99 #endif
100 
101 // Some broken preprocessors need a second expansion
102 // to be forced to tokenize __VA_ARGS__
103 #define E1(x) x
104 
105 #define LOCAL_ALIGNED_A(a, t, v, s, o, ...) \
106  uint8_t la_##v[sizeof(t s o) + (a)]; \
107  t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
108 
109 #define LOCAL_ALIGNED_D(a, t, v, s, o, ...) \
110  DECLARE_ALIGNED(a, t, la_##v) s o; \
111  t (*v) o = la_##v
112 
113 #define LOCAL_ALIGNED(a, t, v, ...) LOCAL_ALIGNED_##a(t, v, __VA_ARGS__)
114 
115 #if HAVE_LOCAL_ALIGNED
116 # define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_D(4, t, v, __VA_ARGS__,,))
117 #else
118 # define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_A(4, t, v, __VA_ARGS__,,))
119 #endif
120 
121 #if HAVE_LOCAL_ALIGNED
122 # define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
123 #else
124 # define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_A(8, t, v, __VA_ARGS__,,))
125 #endif
126 
127 #if HAVE_LOCAL_ALIGNED
128 # define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
129 #else
130 # define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_A(16, t, v, __VA_ARGS__,,))
131 #endif
132 
133 #if HAVE_LOCAL_ALIGNED
134 # define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_D(32, t, v, __VA_ARGS__,,))
135 #else
136 # define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_A(32, t, v, __VA_ARGS__,,))
137 #endif
138 
139 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
140 {
141  void *val;
142 
143  memcpy(&val, ptr, sizeof(val));
144  if (min_size <= *size) {
145  av_assert0(val || !min_size);
146  return 0;
147  }
148  min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
149  av_freep(ptr);
150  val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
151  memcpy(ptr, &val, sizeof(val));
152  if (!val)
153  min_size = 0;
154  *size = min_size;
155  return 1;
156 }
157 #endif /* AVUTIL_MEM_INTERNAL_H */
ff_fast_malloc
static int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
Definition: mem_internal.h:139
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
val
static double val(void *priv, double ch)
Definition: aeval.c:76
avassert.h
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
size
int size
Definition: twinvq_data.h:10344
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
version.h
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35