1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdint.h>
20 #include <string.h>
21
26
28 void (*free)(
void *opaque,
uint8_t *data),
29 void *opaque,
int flags)
30 {
33
35 if (!buf)
36 return NULL;
37
43
46
48 if (!ref) {
50 return NULL;
51 }
52
56
57 return ref;
58 }
59
61 {
63 }
64
66 {
69
71 if (!data)
72 return NULL;
73
75 if (!ret)
77
79 }
80
82 {
84 if (!ret)
85 return NULL;
86
87 memset(ret->
data, 0, size);
89 }
90
92 {
94
95 if (!ret)
96 return NULL;
97
99
101
103 }
104
106 {
108
109 if (!buf || !*buf)
110 return;
111 b = (*buf)->buffer;
113
117 }
118 }
119
121 {
123 return 0;
124
126 }
127
129 {
131 }
132
134 {
136 }
137
139 {
141
143 return 0;
144
146 if (!newbuf)
148
151 *pbuf = newbuf;
152
153 return 0;
154 }
155
157 {
160
161 if (!buf) {
162 /* allocate a new buffer with av_realloc(), so it will be reallocatable
163 * later */
165 if (!data)
167
169 if (!buf) {
172 }
173
176
177 return 0;
178 }
else if (buf->
size == size)
179 return 0;
180
183 /* cannot realloc, allocate a new reallocable buffer and copy data */
185
187 if (!new)
189
191
193 *pbuf = new;
194 return 0;
195 }
196
198 if (!tmp)
200
203 return 0;
204 }
205
207 {
209 if (!pool)
210 return NULL;
211
214
216
217 return pool;
218 }
219
220 /*
221 * This function gets called when the pool has been uninited and
222 * all the buffers returned to it.
223 */
225 {
229
232 }
234 }
235
237 {
239
240 if (!ppool || !*ppool)
241 return;
242 pool = *ppool;
243 *ppool = NULL;
244
247 }
248
249 /* remove the whole buffer list from the pool and return it */
251 {
253
254 while (cur != last) {
255 last = cur;
257 if (!cur)
258 return NULL;
259 }
260
261 return cur;
262 }
263
265 {
268
269 if (!buf)
270 return;
272
275
277 /* pool is not empty, retrieve it and append it to our list */
282 }
283 }
284
286 {
289
290 if(CONFIG_MEMORY_POISONING)
292
296 }
297
298 /* allocate a new buffer and override its free() callback so that
299 * it is returned to the pool on free */
301 {
304
306 if (!ret)
307 return NULL;
308
310 if (!buf) {
312 return NULL;
313 }
314
319
322
325
327 }
328
330 {
333
334 /* check whether the pool is empty */
337 av_log(NULL,
AV_LOG_DEBUG,
"Pool race dectected, spining to avoid overallocation and eventual OOM\n");
340 }
341
342 if (!buf)
344
345 /* keep the first entry, return the rest of the list to the pool */
348
350 buf, 0);
351 if (!ret) {
353 return NULL;
354 }
356
358 }