build: Drop support for legacy TI ARM compiler
[libav.git] / libavutil / mem.h
CommitLineData
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
f637046d 44#elif defined(__GNUC__) || defined(__clang__)
8a24e98d 45 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
365e3c78 46 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
16c2e214
RP
47#elif defined(_MSC_VER)
48 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
49 #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
50#else
51 #define DECLARE_ALIGNED(n,t,v) t v
52 #define DECLARE_ASM_CONST(n,t,v) static const t v
53#endif
54
52476c1b 55#if AV_GCC_VERSION_AT_LEAST(3,1)
85074d3c
ZM
56 #define av_malloc_attrib __attribute__((__malloc__))
57#else
58 #define av_malloc_attrib
59#endif
60
820818a3 61#if AV_GCC_VERSION_AT_LEAST(4,3)
f3e5e6f0 62 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
cca6d953 63#else
f3e5e6f0 64 #define av_alloc_size(...)
cca6d953
ZM
65#endif
66
792098c2 67/**
49bd8e4b 68 * Allocate a block of size bytes with alignment suitable for all
0ee97f0d
SS
69 * memory accesses (including vectors if available on the CPU).
70 * @param size Size in bytes for the memory block to be allocated.
89c9ff50
DB
71 * @return Pointer to the allocated block, NULL if the block cannot
72 * be allocated.
0ee97f0d 73 * @see av_mallocz()
792098c2 74 */
490a022d 75void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
792098c2
PI
76
77/**
b634b36f 78 * Allocate a block of size * nmemb bytes with av_malloc().
f3e5e6f0
LB
79 * @param nmemb Number of elements
80 * @param size Size of the single element
81 * @return Pointer to the allocated block, NULL if the block cannot
82 * be allocated.
83 * @see av_malloc()
84 */
04b0f0e3 85av_alloc_size(1, 2) void *av_malloc_array(size_t nmemb, size_t size);
f3e5e6f0
LB
86
87/**
49bd8e4b
MR
88 * Allocate or reallocate a block of memory.
89 * If ptr is NULL and size > 0, allocate a new block. If
90 * size is zero, free the memory block pointed to by ptr.
0ee97f0d 91 * @param ptr Pointer to a memory block already allocated with
b4ec7a5f 92 * av_realloc() or NULL.
b634b36f 93 * @param size Size in bytes of the memory block to be allocated or
66f6bab7 94 * reallocated.
b634b36f 95 * @return Pointer to a newly-reallocated block or NULL if the block
89c9ff50 96 * cannot be reallocated or the function is used to free the memory block.
9997a812
DB
97 * @warning Pointers originating from the av_malloc() family of functions must
98 * not be passed to av_realloc(). The former can be implemented using
99 * memalign() (or other functions), and there is no guarantee that
100 * pointers from such functions can be passed to realloc() at all.
101 * The situation is undefined according to POSIX and may crash with
102 * some libc implementations.
0ee97f0d 103 * @see av_fast_realloc()
792098c2 104 */
490a022d 105void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
792098c2
PI
106
107/**
3feb3d6c
LB
108 * Allocate or reallocate a block of memory.
109 * If *ptr is NULL and size > 0, allocate a new block. If
110 * size is zero, free the memory block pointed to by ptr.
111 * @param ptr Pointer to a pointer to a memory block already allocated
112 * with av_realloc(), or pointer to a pointer to NULL.
113 * The pointer is updated on success, or freed on failure.
114 * @param size Size in bytes for the memory block to be allocated or
115 * reallocated
116 * @return Zero on success, an AVERROR error code on failure.
117 * @warning Pointers originating from the av_malloc() family of functions must
118 * not be passed to av_reallocp(). The former can be implemented using
119 * memalign() (or other functions), and there is no guarantee that
120 * pointers from such functions can be passed to realloc() at all.
121 * The situation is undefined according to POSIX and may crash with
122 * some libc implementations.
123 */
124int av_reallocp(void *ptr, size_t size);
125
126/**
fc962d4e
MS
127 * Allocate or reallocate an array.
128 * If ptr is NULL and nmemb > 0, allocate a new block. If
129 * nmemb is zero, free the memory block pointed to by ptr.
130 * @param ptr Pointer to a memory block already allocated with
b4ec7a5f 131 * av_realloc() or NULL.
fc962d4e
MS
132 * @param nmemb Number of elements
133 * @param size Size of the single element
b634b36f 134 * @return Pointer to a newly-reallocated block or NULL if the block
fc962d4e 135 * cannot be reallocated or the function is used to free the memory block.
9997a812
DB
136 * @warning Pointers originating from the av_malloc() family of functions must
137 * not be passed to av_realloc(). The former can be implemented using
138 * memalign() (or other functions), and there is no guarantee that
139 * pointers from such functions can be passed to realloc() at all.
140 * The situation is undefined according to POSIX and may crash with
141 * some libc implementations.
fc962d4e
MS
142 */
143av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
144
145/**
b634b36f 146 * Allocate or reallocate an array through a pointer to a pointer.
fc962d4e
MS
147 * If *ptr is NULL and nmemb > 0, allocate a new block. If
148 * nmemb is zero, free the memory block pointed to by ptr.
149 * @param ptr Pointer to a pointer to a memory block already allocated
b4ec7a5f 150 * with av_realloc(), or pointer to a pointer to NULL.
fc962d4e
MS
151 * The pointer is updated on success, or freed on failure.
152 * @param nmemb Number of elements
153 * @param size Size of the single element
154 * @return Zero on success, an AVERROR error code on failure.
9997a812
DB
155 * @warning Pointers originating from the av_malloc() family of functions must
156 * not be passed to av_realloc(). The former can be implemented using
157 * memalign() (or other functions), and there is no guarantee that
158 * pointers from such functions can be passed to realloc() at all.
159 * The situation is undefined according to POSIX and may crash with
160 * some libc implementations.
fc962d4e
MS
161 */
162av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
163
164/**
49bd8e4b 165 * Free a memory block which has been allocated with av_malloc(z)() or
0ee97f0d
SS
166 * av_realloc().
167 * @param ptr Pointer to the memory block which should be freed.
22baf42c
SS
168 * @note ptr = NULL is explicitly allowed.
169 * @note It is recommended that you use av_freep() instead.
0ee97f0d 170 * @see av_freep()
792098c2
PI
171 */
172void av_free(void *ptr);
173
0ee97f0d 174/**
49bd8e4b 175 * Allocate a block of size bytes with alignment suitable for all
0ee97f0d 176 * memory accesses (including vectors if available on the CPU) and
49bd8e4b 177 * zero all the bytes of the block.
0ee97f0d 178 * @param size Size in bytes for the memory block to be allocated.
89c9ff50 179 * @return Pointer to the allocated block, NULL if it cannot be allocated.
0ee97f0d
SS
180 * @see av_malloc()
181 */
490a022d 182void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
d3de3ee2
SS
183
184/**
b634b36f 185 * Allocate a block of size * nmemb bytes with av_mallocz().
f3e5e6f0
LB
186 * @param nmemb Number of elements
187 * @param size Size of the single element
188 * @return Pointer to the allocated block, NULL if the block cannot
189 * be allocated.
190 * @see av_mallocz()
191 * @see av_malloc_array()
192 */
04b0f0e3 193av_alloc_size(1, 2) void *av_mallocz_array(size_t nmemb, size_t size);
f3e5e6f0
LB
194
195/**
49bd8e4b 196 * Duplicate the string s.
89c9ff50 197 * @param s string to be duplicated
b634b36f 198 * @return Pointer to a newly-allocated string containing a
bf7e799c 199 * copy of s or NULL if the string cannot be allocated.
d3de3ee2 200 */
85074d3c 201char *av_strdup(const char *s) av_malloc_attrib;
792098c2
PI
202
203/**
8ddc3262
AK
204 * Duplicate a substring of the string s.
205 * @param s string to be duplicated
206 * @param len the maximum length of the resulting string (not counting the
207 * terminating byte).
208 * @return Pointer to a newly-allocated string containing a
209 * copy of s or NULL if the string cannot be allocated.
210 */
211char *av_strndup(const char *s, size_t len) av_malloc_attrib;
212
213/**
49bd8e4b 214 * Free a memory block which has been allocated with av_malloc(z)() or
89c9ff50 215 * av_realloc() and set the pointer pointing to it to NULL.
0ee97f0d
SS
216 * @param ptr Pointer to the pointer to the memory block which should
217 * be freed.
218 * @see av_free()
792098c2
PI
219 */
220void av_freep(void *ptr);
221
757cd8d8 222/**
b634b36f 223 * deliberately overlapping memcpy implementation
5bac2d0c
DB
224 * @param dst destination buffer
225 * @param back how many bytes back we start (the initial size of the overlapping window)
226 * @param cnt number of bytes to copy, must be >= 0
227 *
228 * cnt > back is valid, this will copy the bytes we just copied,
229 * thus creating a repeating pattern with a period length of back.
230 */
231void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
232
233/**
cce3e0a4
AK
234 * Reallocate the given block if it is not large enough, otherwise do nothing.
235 *
236 * @see av_realloc
237 */
238void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
239
240/**
241 * Allocate a buffer, reusing the given one if large enough.
242 *
243 * Contrary to av_fast_realloc the current buffer contents might not be
244 * preserved and on error the old buffer is freed, thus no special
245 * handling to avoid memleaks is necessary.
246 *
247 * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
248 * @param size size of the buffer *ptr points to
249 * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
250 * *size 0 if an error occurred.
251 */
252void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
253
254/**
757cd8d8
LB
255 * @}
256 */
257
98790382 258#endif /* AVUTIL_MEM_H */