build: Drop support for legacy TI ARM compiler
[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(__GNUC__) || defined(__clang__)
45 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
46 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
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
55 #if AV_GCC_VERSION_AT_LEAST(3,1)
56 #define av_malloc_attrib __attribute__((__malloc__))
57 #else
58 #define av_malloc_attrib
59 #endif
60
61 #if AV_GCC_VERSION_AT_LEAST(4,3)
62 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
63 #else
64 #define av_alloc_size(...)
65 #endif
66
67 /**
68 * Allocate a block of size bytes with alignment suitable for all
69 * memory accesses (including vectors if available on the CPU).
70 * @param size Size in bytes for the memory block to be allocated.
71 * @return Pointer to the allocated block, NULL if the block cannot
72 * be allocated.
73 * @see av_mallocz()
74 */
75 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
76
77 /**
78 * Allocate a block of size * nmemb bytes with av_malloc().
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 */
85 av_alloc_size(1, 2) void *av_malloc_array(size_t nmemb, size_t size);
86
87 /**
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.
91 * @param ptr Pointer to a memory block already allocated with
92 * av_realloc() or NULL.
93 * @param size Size in bytes of the memory block to be allocated or
94 * reallocated.
95 * @return Pointer to a newly-reallocated block or NULL if the block
96 * cannot be reallocated or the function is used to free the memory block.
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.
103 * @see av_fast_realloc()
104 */
105 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
106
107 /**
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 */
124 int av_reallocp(void *ptr, size_t size);
125
126 /**
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
131 * av_realloc() or NULL.
132 * @param nmemb Number of elements
133 * @param size Size of the single element
134 * @return Pointer to a newly-reallocated block or NULL if the block
135 * cannot be reallocated or the function is used to free the memory block.
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.
142 */
143 av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size);
144
145 /**
146 * Allocate or reallocate an array through a pointer to a pointer.
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
150 * with av_realloc(), or pointer to a pointer to NULL.
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.
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.
161 */
162 av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size);
163
164 /**
165 * Free a memory block which has been allocated with av_malloc(z)() or
166 * av_realloc().
167 * @param ptr Pointer to the memory block which should be freed.
168 * @note ptr = NULL is explicitly allowed.
169 * @note It is recommended that you use av_freep() instead.
170 * @see av_freep()
171 */
172 void av_free(void *ptr);
173
174 /**
175 * Allocate a block of size bytes with alignment suitable for all
176 * memory accesses (including vectors if available on the CPU) and
177 * zero all the bytes of the block.
178 * @param size Size in bytes for the memory block to be allocated.
179 * @return Pointer to the allocated block, NULL if it cannot be allocated.
180 * @see av_malloc()
181 */
182 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
183
184 /**
185 * Allocate a block of size * nmemb bytes with av_mallocz().
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 */
193 av_alloc_size(1, 2) void *av_mallocz_array(size_t nmemb, size_t size);
194
195 /**
196 * Duplicate the string s.
197 * @param s string to be duplicated
198 * @return Pointer to a newly-allocated string containing a
199 * copy of s or NULL if the string cannot be allocated.
200 */
201 char *av_strdup(const char *s) av_malloc_attrib;
202
203 /**
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 */
211 char *av_strndup(const char *s, size_t len) av_malloc_attrib;
212
213 /**
214 * Free a memory block which has been allocated with av_malloc(z)() or
215 * av_realloc() and set the pointer pointing to it to NULL.
216 * @param ptr Pointer to the pointer to the memory block which should
217 * be freed.
218 * @see av_free()
219 */
220 void av_freep(void *ptr);
221
222 /**
223 * deliberately overlapping memcpy implementation
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 */
231 void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
232
233 /**
234 * Reallocate the given block if it is not large enough, otherwise do nothing.
235 *
236 * @see av_realloc
237 */
238 void *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 */
252 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
253
254 /**
255 * @}
256 */
257
258 #endif /* AVUTIL_MEM_H */