2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
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.
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.
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
23 * memory handling functions
32 #include "attributes.h"
36 * @addtogroup lavu_mem
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
58 #define DECLARE_ALIGNED(n,t,v) t v
59 #define DECLARE_ASM_CONST(n,t,v) static const t v
62 #if AV_GCC_VERSION_AT_LEAST(3,1)
63 #define av_malloc_attrib __attribute__((__malloc__))
65 #define av_malloc_attrib
68 #if AV_GCC_VERSION_AT_LEAST(4,3)
69 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
71 #define av_alloc_size(...)
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
82 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
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
92 av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size)
94 if (!size || nmemb >= INT_MAX / size)
96 return av_malloc(nmemb * size);
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
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()
117 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
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
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.
136 int av_reallocp(void *ptr, size_t size);
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.
155 av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
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.
174 av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
177 * Free a memory block which has been allocated with av_malloc(z)() or
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.
184 void av_free(void *ptr);
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.
194 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
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
203 * @see av_malloc_array()
205 av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
207 if (!size || nmemb >= INT_MAX / size)
209 return av_mallocz(nmemb * size);
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.
218 char *av_strdup(const char *s) av_malloc_attrib;
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
227 void av_freep(void *ptr);
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
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.
238 void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
241 * Reallocate the given block if it is not large enough, otherwise do nothing.
245 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
248 * Allocate a buffer, reusing the given one if large enough.
250 * Contrary to av_fast_realloc the current buffer contents might not be
251 * preserved and on error the old buffer is freed, thus no special
252 * handling to avoid memleaks is necessary.
254 * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
255 * @param size size of the buffer *ptr points to
256 * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
257 * *size 0 if an error occurred.
259 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
265 #endif /* AVUTIL_MEM_H */