f51682bb089330a4f9583b5df478a6cfaa12065a
[libav.git] / libavutil / mem.h
1 /*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * memory handling functions
24 */
25
26 #ifndef AVUTIL_MEM_H
27 #define AVUTIL_MEM_H
28
29 #include <limits.h>
30 #include <stdint.h>
31
32 #include "attributes.h"
33 #include "avutil.h"
34
35 /**
36 * @addtogroup lavu_mem
37 * @{
38 */
39
40
41 #if defined(__ICC) && __ICC < 1200 || defined(__SUNPRO_C)
42 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
43 #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
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
51 #elif defined(__GNUC__)
52 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
53 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
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
62 #if AV_GCC_VERSION_AT_LEAST(3,1)
63 #define av_malloc_attrib __attribute__((__malloc__))
64 #else
65 #define av_malloc_attrib
66 #endif
67
68 #if AV_GCC_VERSION_AT_LEAST(4,3)
69 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
70 #else
71 #define av_alloc_size(...)
72 #endif
73
74 /**
75 * Allocate a block of size bytes with alignment suitable for all
76 * memory accesses (including vectors if available on the CPU).
77 * @param size Size in bytes for the memory block to be allocated.
78 * @return Pointer to the allocated block, NULL if the block cannot
79 * be allocated.
80 * @see av_mallocz()
81 */
82 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
83
84 /**
85 * Allocate a block of size * nmemb bytes with av_malloc().
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 */
92 av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size)
93 {
94 if (size <= 0 || nmemb >= INT_MAX / size)
95 return NULL;
96 return av_malloc(nmemb * size);
97 }
98
99 /**
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.
103 * @param ptr Pointer to a memory block already allocated with
104 * av_realloc() or NULL.
105 * @param size Size in bytes of the memory block to be allocated or
106 * reallocated.
107 * @return Pointer to a newly-reallocated block or NULL if the block
108 * cannot be reallocated or the function is used to free the memory block.
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.
115 * @see av_fast_realloc()
116 */
117 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
118
119 /**
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 /**
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
143 * av_realloc() or NULL.
144 * @param nmemb Number of elements
145 * @param size Size of the single element
146 * @return Pointer to a newly-reallocated block or NULL if the block
147 * cannot be reallocated or the function is used to free the memory block.
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.
154 */
155 av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
156
157 /**
158 * Allocate or reallocate an array through a pointer to a pointer.
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
162 * with av_realloc(), or pointer to a pointer to NULL.
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.
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.
173 */
174 av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
175
176 /**
177 * Free a memory block which has been allocated with av_malloc(z)() or
178 * av_realloc().
179 * @param ptr Pointer to the memory block which should be freed.
180 * @note ptr = NULL is explicitly allowed.
181 * @note It is recommended that you use av_freep() instead.
182 * @see av_freep()
183 */
184 void av_free(void *ptr);
185
186 /**
187 * Allocate a block of size bytes with alignment suitable for all
188 * memory accesses (including vectors if available on the CPU) and
189 * zero all the bytes of the block.
190 * @param size Size in bytes for the memory block to be allocated.
191 * @return Pointer to the allocated block, NULL if it cannot be allocated.
192 * @see av_malloc()
193 */
194 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
195
196 /**
197 * Allocate a block of size * nmemb bytes with av_mallocz().
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 */
205 av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
206 {
207 if (size <= 0 || nmemb >= INT_MAX / size)
208 return NULL;
209 return av_mallocz(nmemb * size);
210 }
211
212 /**
213 * Duplicate the string s.
214 * @param s string to be duplicated
215 * @return Pointer to a newly-allocated string containing a
216 * copy of s or NULL if the string cannot be allocated.
217 */
218 char *av_strdup(const char *s) av_malloc_attrib;
219
220 /**
221 * Free a memory block which has been allocated with av_malloc(z)() or
222 * av_realloc() and set the pointer pointing to it to NULL.
223 * @param ptr Pointer to the pointer to the memory block which should
224 * be freed.
225 * @see av_free()
226 */
227 void av_freep(void *ptr);
228
229 /**
230 * deliberately overlapping memcpy implementation
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 /**
241 * @}
242 */
243
244 #endif /* AVUTIL_MEM_H */