annotate lib/libavutil/mem.h @ 2406:33659ef7fbac

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