Commit | Line | Data |
---|---|---|
792098c2 PI |
1 | /* |
2 | * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> | |
3 | * | |
2912e87a | 4 | * This file is part of Libav. |
792098c2 | 5 | * |
2912e87a | 6 | * Libav is free software; you can redistribute it and/or |
792098c2 PI |
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 | * | |
2912e87a | 11 | * Libav is distributed in the hope that it will be useful, |
792098c2 PI |
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 | |
2912e87a | 17 | * License along with Libav; if not, write to the Free Software |
792098c2 PI |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ | |
20 | ||
21 | /** | |
ba87f080 | 22 | * @file |
89c9ff50 | 23 | * memory handling functions |
792098c2 PI |
24 | */ |
25 | ||
98790382 SS |
26 | #ifndef AVUTIL_MEM_H |
27 | #define AVUTIL_MEM_H | |
792098c2 | 28 | |
1d9c2dc8 | 29 | #include <limits.h> |
5bac2d0c | 30 | #include <stdint.h> |
1d9c2dc8 | 31 | |
2ed6f399 | 32 | #include "attributes.h" |
276fc8a4 | 33 | #include "avutil.h" |
52476c1b | 34 | |
757cd8d8 LB |
35 | /** |
36 | * @addtogroup lavu_mem | |
37 | * @{ | |
38 | */ | |
39 | ||
40 | ||
8c5ed7a6 | 41 | #if defined(__ICC) && __ICC < 1200 || defined(__SUNPRO_C) |
8a24e98d | 42 | #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v |
16c2e214 | 43 | #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v |
34e52abb MR |
44 | #elif defined(__TI_COMPILER_VERSION__) |
45 | #define DECLARE_ALIGNED(n,t,v) \ | |
46 | AV_PRAGMA(DATA_ALIGN(v,n)) \ | |
47 | t __attribute__((aligned(n))) v | |
48 | #define DECLARE_ASM_CONST(n,t,v) \ | |
49 | AV_PRAGMA(DATA_ALIGN(v,n)) \ | |
50 | static const t __attribute__((aligned(n))) v | |
16c2e214 | 51 | #elif defined(__GNUC__) |
8a24e98d | 52 | #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v |
365e3c78 | 53 | #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v |
16c2e214 RP |
54 | #elif defined(_MSC_VER) |
55 | #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v | |
56 | #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v | |
57 | #else | |
58 | #define DECLARE_ALIGNED(n,t,v) t v | |
59 | #define DECLARE_ASM_CONST(n,t,v) static const t v | |
60 | #endif | |
61 | ||
52476c1b | 62 | #if AV_GCC_VERSION_AT_LEAST(3,1) |
85074d3c ZM |
63 | #define av_malloc_attrib __attribute__((__malloc__)) |
64 | #else | |
65 | #define av_malloc_attrib | |
66 | #endif | |
67 | ||
820818a3 | 68 | #if AV_GCC_VERSION_AT_LEAST(4,3) |
f3e5e6f0 | 69 | #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) |
cca6d953 | 70 | #else |
f3e5e6f0 | 71 | #define av_alloc_size(...) |
cca6d953 ZM |
72 | #endif |
73 | ||
792098c2 | 74 | /** |
49bd8e4b | 75 | * Allocate a block of size bytes with alignment suitable for all |
0ee97f0d SS |
76 | * memory accesses (including vectors if available on the CPU). |
77 | * @param size Size in bytes for the memory block to be allocated. | |
89c9ff50 DB |
78 | * @return Pointer to the allocated block, NULL if the block cannot |
79 | * be allocated. | |
0ee97f0d | 80 | * @see av_mallocz() |
792098c2 | 81 | */ |
490a022d | 82 | void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1); |
792098c2 PI |
83 | |
84 | /** | |
b634b36f | 85 | * Allocate a block of size * nmemb bytes with av_malloc(). |
f3e5e6f0 LB |
86 | * @param nmemb Number of elements |
87 | * @param size Size of the single element | |
88 | * @return Pointer to the allocated block, NULL if the block cannot | |
89 | * be allocated. | |
90 | * @see av_malloc() | |
91 | */ | |
5532cf31 | 92 | av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size) |
f3e5e6f0 | 93 | { |
b284e1ff | 94 | if (!size || nmemb >= INT_MAX / size) |
f3e5e6f0 LB |
95 | return NULL; |
96 | return av_malloc(nmemb * size); | |
97 | } | |
98 | ||
99 | /** | |
49bd8e4b MR |
100 | * Allocate or reallocate a block of memory. |
101 | * If ptr is NULL and size > 0, allocate a new block. If | |
102 | * size is zero, free the memory block pointed to by ptr. | |
0ee97f0d | 103 | * @param ptr Pointer to a memory block already allocated with |
b4ec7a5f | 104 | * av_realloc() or NULL. |
b634b36f | 105 | * @param size Size in bytes of the memory block to be allocated or |
66f6bab7 | 106 | * reallocated. |
b634b36f | 107 | * @return Pointer to a newly-reallocated block or NULL if the block |
89c9ff50 | 108 | * cannot be reallocated or the function is used to free the memory block. |
9997a812 DB |
109 | * @warning Pointers originating from the av_malloc() family of functions must |
110 | * not be passed to av_realloc(). The former can be implemented using | |
111 | * memalign() (or other functions), and there is no guarantee that | |
112 | * pointers from such functions can be passed to realloc() at all. | |
113 | * The situation is undefined according to POSIX and may crash with | |
114 | * some libc implementations. | |
0ee97f0d | 115 | * @see av_fast_realloc() |
792098c2 | 116 | */ |
490a022d | 117 | void *av_realloc(void *ptr, size_t size) av_alloc_size(2); |
792098c2 PI |
118 | |
119 | /** | |
3feb3d6c LB |
120 | * Allocate or reallocate a block of memory. |
121 | * If *ptr is NULL and size > 0, allocate a new block. If | |
122 | * size is zero, free the memory block pointed to by ptr. | |
123 | * @param ptr Pointer to a pointer to a memory block already allocated | |
124 | * with av_realloc(), or pointer to a pointer to NULL. | |
125 | * The pointer is updated on success, or freed on failure. | |
126 | * @param size Size in bytes for the memory block to be allocated or | |
127 | * reallocated | |
128 | * @return Zero on success, an AVERROR error code on failure. | |
129 | * @warning Pointers originating from the av_malloc() family of functions must | |
130 | * not be passed to av_reallocp(). The former can be implemented using | |
131 | * memalign() (or other functions), and there is no guarantee that | |
132 | * pointers from such functions can be passed to realloc() at all. | |
133 | * The situation is undefined according to POSIX and may crash with | |
134 | * some libc implementations. | |
135 | */ | |
136 | int av_reallocp(void *ptr, size_t size); | |
137 | ||
138 | /** | |
fc962d4e MS |
139 | * Allocate or reallocate an array. |
140 | * If ptr is NULL and nmemb > 0, allocate a new block. If | |
141 | * nmemb is zero, free the memory block pointed to by ptr. | |
142 | * @param ptr Pointer to a memory block already allocated with | |
b4ec7a5f | 143 | * av_realloc() or NULL. |
fc962d4e MS |
144 | * @param nmemb Number of elements |
145 | * @param size Size of the single element | |
b634b36f | 146 | * @return Pointer to a newly-reallocated block or NULL if the block |
fc962d4e | 147 | * cannot be reallocated or the function is used to free the memory block. |
9997a812 DB |
148 | * @warning Pointers originating from the av_malloc() family of functions must |
149 | * not be passed to av_realloc(). The former can be implemented using | |
150 | * memalign() (or other functions), and there is no guarantee that | |
151 | * pointers from such functions can be passed to realloc() at all. | |
152 | * The situation is undefined according to POSIX and may crash with | |
153 | * some libc implementations. | |
fc962d4e MS |
154 | */ |
155 | av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size); | |
156 | ||
157 | /** | |
b634b36f | 158 | * Allocate or reallocate an array through a pointer to a pointer. |
fc962d4e MS |
159 | * If *ptr is NULL and nmemb > 0, allocate a new block. If |
160 | * nmemb is zero, free the memory block pointed to by ptr. | |
161 | * @param ptr Pointer to a pointer to a memory block already allocated | |
b4ec7a5f | 162 | * with av_realloc(), or pointer to a pointer to NULL. |
fc962d4e MS |
163 | * The pointer is updated on success, or freed on failure. |
164 | * @param nmemb Number of elements | |
165 | * @param size Size of the single element | |
166 | * @return Zero on success, an AVERROR error code on failure. | |
9997a812 DB |
167 | * @warning Pointers originating from the av_malloc() family of functions must |
168 | * not be passed to av_realloc(). The former can be implemented using | |
169 | * memalign() (or other functions), and there is no guarantee that | |
170 | * pointers from such functions can be passed to realloc() at all. | |
171 | * The situation is undefined according to POSIX and may crash with | |
172 | * some libc implementations. | |
fc962d4e MS |
173 | */ |
174 | av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size); | |
175 | ||
176 | /** | |
49bd8e4b | 177 | * Free a memory block which has been allocated with av_malloc(z)() or |
0ee97f0d SS |
178 | * av_realloc(). |
179 | * @param ptr Pointer to the memory block which should be freed. | |
22baf42c SS |
180 | * @note ptr = NULL is explicitly allowed. |
181 | * @note It is recommended that you use av_freep() instead. | |
0ee97f0d | 182 | * @see av_freep() |
792098c2 PI |
183 | */ |
184 | void av_free(void *ptr); | |
185 | ||
0ee97f0d | 186 | /** |
49bd8e4b | 187 | * Allocate a block of size bytes with alignment suitable for all |
0ee97f0d | 188 | * memory accesses (including vectors if available on the CPU) and |
49bd8e4b | 189 | * zero all the bytes of the block. |
0ee97f0d | 190 | * @param size Size in bytes for the memory block to be allocated. |
89c9ff50 | 191 | * @return Pointer to the allocated block, NULL if it cannot be allocated. |
0ee97f0d SS |
192 | * @see av_malloc() |
193 | */ | |
490a022d | 194 | void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1); |
d3de3ee2 SS |
195 | |
196 | /** | |
b634b36f | 197 | * Allocate a block of size * nmemb bytes with av_mallocz(). |
f3e5e6f0 LB |
198 | * @param nmemb Number of elements |
199 | * @param size Size of the single element | |
200 | * @return Pointer to the allocated block, NULL if the block cannot | |
201 | * be allocated. | |
202 | * @see av_mallocz() | |
203 | * @see av_malloc_array() | |
204 | */ | |
5532cf31 | 205 | av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size) |
f3e5e6f0 | 206 | { |
b284e1ff | 207 | if (!size || nmemb >= INT_MAX / size) |
f3e5e6f0 LB |
208 | return NULL; |
209 | return av_mallocz(nmemb * size); | |
210 | } | |
211 | ||
212 | /** | |
49bd8e4b | 213 | * Duplicate the string s. |
89c9ff50 | 214 | * @param s string to be duplicated |
b634b36f | 215 | * @return Pointer to a newly-allocated string containing a |
bf7e799c | 216 | * copy of s or NULL if the string cannot be allocated. |
d3de3ee2 | 217 | */ |
85074d3c | 218 | char *av_strdup(const char *s) av_malloc_attrib; |
792098c2 PI |
219 | |
220 | /** | |
49bd8e4b | 221 | * Free a memory block which has been allocated with av_malloc(z)() or |
89c9ff50 | 222 | * av_realloc() and set the pointer pointing to it to NULL. |
0ee97f0d SS |
223 | * @param ptr Pointer to the pointer to the memory block which should |
224 | * be freed. | |
225 | * @see av_free() | |
792098c2 PI |
226 | */ |
227 | void av_freep(void *ptr); | |
228 | ||
757cd8d8 | 229 | /** |
b634b36f | 230 | * deliberately overlapping memcpy implementation |
5bac2d0c DB |
231 | * @param dst destination buffer |
232 | * @param back how many bytes back we start (the initial size of the overlapping window) | |
233 | * @param cnt number of bytes to copy, must be >= 0 | |
234 | * | |
235 | * cnt > back is valid, this will copy the bytes we just copied, | |
236 | * thus creating a repeating pattern with a period length of back. | |
237 | */ | |
238 | void av_memcpy_backptr(uint8_t *dst, int back, int cnt); | |
239 | ||
240 | /** | |
757cd8d8 LB |
241 | * @} |
242 | */ | |
243 | ||
98790382 | 244 | #endif /* AVUTIL_MEM_H */ |