yading@11
|
1 /*
|
yading@11
|
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 /**
|
yading@11
|
22 * @file
|
yading@11
|
23 * memory handling functions
|
yading@11
|
24 */
|
yading@11
|
25
|
yading@11
|
26 #ifndef AVUTIL_MEM_H
|
yading@11
|
27 #define AVUTIL_MEM_H
|
yading@11
|
28
|
yading@11
|
29 #include <limits.h>
|
yading@11
|
30 #include <stdint.h>
|
yading@11
|
31
|
yading@11
|
32 #include "attributes.h"
|
yading@11
|
33 #include "error.h"
|
yading@11
|
34 #include "avutil.h"
|
yading@11
|
35
|
yading@11
|
36 /**
|
yading@11
|
37 * @addtogroup lavu_mem
|
yading@11
|
38 * @{
|
yading@11
|
39 */
|
yading@11
|
40
|
yading@11
|
41
|
yading@11
|
42 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1110 || defined(__SUNPRO_C)
|
yading@11
|
43 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
|
yading@11
|
44 #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
|
yading@11
|
45 #elif defined(__TI_COMPILER_VERSION__)
|
yading@11
|
46 #define DECLARE_ALIGNED(n,t,v) \
|
yading@11
|
47 AV_PRAGMA(DATA_ALIGN(v,n)) \
|
yading@11
|
48 t __attribute__((aligned(n))) v
|
yading@11
|
49 #define DECLARE_ASM_CONST(n,t,v) \
|
yading@11
|
50 AV_PRAGMA(DATA_ALIGN(v,n)) \
|
yading@11
|
51 static const t __attribute__((aligned(n))) v
|
yading@11
|
52 #elif defined(__GNUC__)
|
yading@11
|
53 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
|
yading@11
|
54 #define DECLARE_ASM_CONST(n,t,v) static const t av_used __attribute__ ((aligned (n))) v
|
yading@11
|
55 #elif defined(_MSC_VER)
|
yading@11
|
56 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
|
yading@11
|
57 #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
|
yading@11
|
58 #else
|
yading@11
|
59 #define DECLARE_ALIGNED(n,t,v) t v
|
yading@11
|
60 #define DECLARE_ASM_CONST(n,t,v) static const t v
|
yading@11
|
61 #endif
|
yading@11
|
62
|
yading@11
|
63 #if AV_GCC_VERSION_AT_LEAST(3,1)
|
yading@11
|
64 #define av_malloc_attrib __attribute__((__malloc__))
|
yading@11
|
65 #else
|
yading@11
|
66 #define av_malloc_attrib
|
yading@11
|
67 #endif
|
yading@11
|
68
|
yading@11
|
69 #if AV_GCC_VERSION_AT_LEAST(4,3)
|
yading@11
|
70 #define av_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
|
yading@11
|
71 #else
|
yading@11
|
72 #define av_alloc_size(...)
|
yading@11
|
73 #endif
|
yading@11
|
74
|
yading@11
|
75 /**
|
yading@11
|
76 * Allocate a block of size bytes with alignment suitable for all
|
yading@11
|
77 * memory accesses (including vectors if available on the CPU).
|
yading@11
|
78 * @param size Size in bytes for the memory block to be allocated.
|
yading@11
|
79 * @return Pointer to the allocated block, NULL if the block cannot
|
yading@11
|
80 * be allocated.
|
yading@11
|
81 * @see av_mallocz()
|
yading@11
|
82 */
|
yading@11
|
83 void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
|
yading@11
|
84
|
yading@11
|
85 /**
|
yading@11
|
86 * Helper function to allocate a block of size * nmemb bytes with
|
yading@11
|
87 * using av_malloc()
|
yading@11
|
88 * @param nmemb Number of elements
|
yading@11
|
89 * @param size Size of the single element
|
yading@11
|
90 * @return Pointer to the allocated block, NULL if the block cannot
|
yading@11
|
91 * be allocated.
|
yading@11
|
92 * @see av_malloc()
|
yading@11
|
93 */
|
yading@11
|
94 av_alloc_size(1, 2) static inline void *av_malloc_array(size_t nmemb, size_t size)
|
yading@11
|
95 {
|
yading@11
|
96 if (size <= 0 || nmemb >= INT_MAX / size)
|
yading@11
|
97 return NULL;
|
yading@11
|
98 return av_malloc(nmemb * size);
|
yading@11
|
99 }
|
yading@11
|
100
|
yading@11
|
101 /**
|
yading@11
|
102 * Allocate or reallocate a block of memory.
|
yading@11
|
103 * If ptr is NULL and size > 0, allocate a new block. If
|
yading@11
|
104 * size is zero, free the memory block pointed to by ptr.
|
yading@11
|
105 * @param ptr Pointer to a memory block already allocated with
|
yading@11
|
106 * av_malloc(z)() or av_realloc() or NULL.
|
yading@11
|
107 * @param size Size in bytes for the memory block to be allocated or
|
yading@11
|
108 * reallocated.
|
yading@11
|
109 * @return Pointer to a newly reallocated block or NULL if the block
|
yading@11
|
110 * cannot be reallocated or the function is used to free the memory block.
|
yading@11
|
111 * @see av_fast_realloc()
|
yading@11
|
112 */
|
yading@11
|
113 void *av_realloc(void *ptr, size_t size) av_alloc_size(2);
|
yading@11
|
114
|
yading@11
|
115 /**
|
yading@11
|
116 * Allocate or reallocate a block of memory.
|
yading@11
|
117 * This function does the same thing as av_realloc, except:
|
yading@11
|
118 * - It takes two arguments and checks the result of the multiplication for
|
yading@11
|
119 * integer overflow.
|
yading@11
|
120 * - It frees the input block in case of failure, thus avoiding the memory
|
yading@11
|
121 * leak with the classic "buf = realloc(buf); if (!buf) return -1;".
|
yading@11
|
122 */
|
yading@11
|
123 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize);
|
yading@11
|
124
|
yading@11
|
125 /**
|
yading@11
|
126 * Free a memory block which has been allocated with av_malloc(z)() or
|
yading@11
|
127 * av_realloc().
|
yading@11
|
128 * @param ptr Pointer to the memory block which should be freed.
|
yading@11
|
129 * @note ptr = NULL is explicitly allowed.
|
yading@11
|
130 * @note It is recommended that you use av_freep() instead.
|
yading@11
|
131 * @see av_freep()
|
yading@11
|
132 */
|
yading@11
|
133 void av_free(void *ptr);
|
yading@11
|
134
|
yading@11
|
135 /**
|
yading@11
|
136 * Allocate a block of size bytes with alignment suitable for all
|
yading@11
|
137 * memory accesses (including vectors if available on the CPU) and
|
yading@11
|
138 * zero all the bytes of the block.
|
yading@11
|
139 * @param size Size in bytes for the memory block to be allocated.
|
yading@11
|
140 * @return Pointer to the allocated block, NULL if it cannot be allocated.
|
yading@11
|
141 * @see av_malloc()
|
yading@11
|
142 */
|
yading@11
|
143 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
|
yading@11
|
144
|
yading@11
|
145 /**
|
yading@11
|
146 * Allocate a block of nmemb * size bytes with alignment suitable for all
|
yading@11
|
147 * memory accesses (including vectors if available on the CPU) and
|
yading@11
|
148 * zero all the bytes of the block.
|
yading@11
|
149 * The allocation will fail if nmemb * size is greater than or equal
|
yading@11
|
150 * to INT_MAX.
|
yading@11
|
151 * @param nmemb
|
yading@11
|
152 * @param size
|
yading@11
|
153 * @return Pointer to the allocated block, NULL if it cannot be allocated.
|
yading@11
|
154 */
|
yading@11
|
155 void *av_calloc(size_t nmemb, size_t size) av_malloc_attrib;
|
yading@11
|
156
|
yading@11
|
157 /**
|
yading@11
|
158 * Helper function to allocate a block of size * nmemb bytes with
|
yading@11
|
159 * using av_mallocz()
|
yading@11
|
160 * @param nmemb Number of elements
|
yading@11
|
161 * @param size Size of the single element
|
yading@11
|
162 * @return Pointer to the allocated block, NULL if the block cannot
|
yading@11
|
163 * be allocated.
|
yading@11
|
164 * @see av_mallocz()
|
yading@11
|
165 * @see av_malloc_array()
|
yading@11
|
166 */
|
yading@11
|
167 av_alloc_size(1, 2) static inline void *av_mallocz_array(size_t nmemb, size_t size)
|
yading@11
|
168 {
|
yading@11
|
169 if (size <= 0 || nmemb >= INT_MAX / size)
|
yading@11
|
170 return NULL;
|
yading@11
|
171 return av_mallocz(nmemb * size);
|
yading@11
|
172 }
|
yading@11
|
173
|
yading@11
|
174 /**
|
yading@11
|
175 * Duplicate the string s.
|
yading@11
|
176 * @param s string to be duplicated
|
yading@11
|
177 * @return Pointer to a newly allocated string containing a
|
yading@11
|
178 * copy of s or NULL if the string cannot be allocated.
|
yading@11
|
179 */
|
yading@11
|
180 char *av_strdup(const char *s) av_malloc_attrib;
|
yading@11
|
181
|
yading@11
|
182 /**
|
yading@11
|
183 * Free a memory block which has been allocated with av_malloc(z)() or
|
yading@11
|
184 * av_realloc() and set the pointer pointing to it to NULL.
|
yading@11
|
185 * @param ptr Pointer to the pointer to the memory block which should
|
yading@11
|
186 * be freed.
|
yading@11
|
187 * @see av_free()
|
yading@11
|
188 */
|
yading@11
|
189 void av_freep(void *ptr);
|
yading@11
|
190
|
yading@11
|
191 /**
|
yading@11
|
192 * Add an element to a dynamic array.
|
yading@11
|
193 *
|
yading@11
|
194 * @param tab_ptr Pointer to the array.
|
yading@11
|
195 * @param nb_ptr Pointer to the number of elements in the array.
|
yading@11
|
196 * @param elem Element to be added.
|
yading@11
|
197 */
|
yading@11
|
198 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
|
yading@11
|
199
|
yading@11
|
200 /**
|
yading@11
|
201 * Multiply two size_t values checking for overflow.
|
yading@11
|
202 * @return 0 if success, AVERROR(EINVAL) if overflow.
|
yading@11
|
203 */
|
yading@11
|
204 static inline int av_size_mult(size_t a, size_t b, size_t *r)
|
yading@11
|
205 {
|
yading@11
|
206 size_t t = a * b;
|
yading@11
|
207 /* Hack inspired from glibc: only try the division if nelem and elsize
|
yading@11
|
208 * are both greater than sqrt(SIZE_MAX). */
|
yading@11
|
209 if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)
|
yading@11
|
210 return AVERROR(EINVAL);
|
yading@11
|
211 *r = t;
|
yading@11
|
212 return 0;
|
yading@11
|
213 }
|
yading@11
|
214
|
yading@11
|
215 /**
|
yading@11
|
216 * Set the maximum size that may me allocated in one block.
|
yading@11
|
217 */
|
yading@11
|
218 void av_max_alloc(size_t max);
|
yading@11
|
219
|
yading@11
|
220 /**
|
yading@11
|
221 * @brief deliberately overlapping memcpy implementation
|
yading@11
|
222 * @param dst destination buffer
|
yading@11
|
223 * @param back how many bytes back we start (the initial size of the overlapping window), must be > 0
|
yading@11
|
224 * @param cnt number of bytes to copy, must be >= 0
|
yading@11
|
225 *
|
yading@11
|
226 * cnt > back is valid, this will copy the bytes we just copied,
|
yading@11
|
227 * thus creating a repeating pattern with a period length of back.
|
yading@11
|
228 */
|
yading@11
|
229 void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
|
yading@11
|
230
|
yading@11
|
231 /**
|
yading@11
|
232 * @}
|
yading@11
|
233 */
|
yading@11
|
234
|
yading@11
|
235 #endif /* AVUTIL_MEM_H */
|