]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.c
hwcontext_vaapi: Try to support the VDPAU wrapper
[ffmpeg] / libavutil / mem.c
1 /*
2  * default memory allocator for libavutil
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * default memory allocator for libavutil
25  */
26
27 #include "config.h"
28
29 #include <limits.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #if HAVE_MALLOC_H
34 #include <malloc.h>
35 #endif
36
37 #include "avutil.h"
38 #include "common.h"
39 #include "intreadwrite.h"
40 #include "mem.h"
41
42 #ifdef MALLOC_PREFIX
43
44 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
45 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
46 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
47 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
48 #define free           AV_JOIN(MALLOC_PREFIX, free)
49
50 void *malloc(size_t size);
51 void *memalign(size_t align, size_t size);
52 int   posix_memalign(void **ptr, size_t align, size_t size);
53 void *realloc(void *ptr, size_t size);
54 void  free(void *ptr);
55
56 #endif /* MALLOC_PREFIX */
57
58 /* You can redefine av_malloc and av_free in your project to use your
59  * memory allocator. You do not need to suppress this file because the
60  * linker will do it automatically. */
61
62 void *av_malloc(size_t size)
63 {
64     void *ptr = NULL;
65
66     /* let's disallow possibly ambiguous cases */
67     if (size > (INT_MAX - 32) || !size)
68         return NULL;
69
70 #if HAVE_POSIX_MEMALIGN
71     if (posix_memalign(&ptr, 32, size))
72         ptr = NULL;
73 #elif HAVE_ALIGNED_MALLOC
74     ptr = _aligned_malloc(size, 32);
75 #elif HAVE_MEMALIGN
76     ptr = memalign(32, size);
77     /* Why 64?
78      * Indeed, we should align it:
79      *   on  4 for 386
80      *   on 16 for 486
81      *   on 32 for 586, PPro - K6-III
82      *   on 64 for K7 (maybe for P3 too).
83      * Because L1 and L2 caches are aligned on those values.
84      * But I don't want to code such logic here!
85      */
86     /* Why 32?
87      * For AVX ASM. SSE / NEON needs only 16.
88      * Why not larger? Because I did not see a difference in benchmarks ...
89      */
90     /* benchmarks with P3
91      * memalign(64) + 1          3071, 3051, 3032
92      * memalign(64) + 2          3051, 3032, 3041
93      * memalign(64) + 4          2911, 2896, 2915
94      * memalign(64) + 8          2545, 2554, 2550
95      * memalign(64) + 16         2543, 2572, 2563
96      * memalign(64) + 32         2546, 2545, 2571
97      * memalign(64) + 64         2570, 2533, 2558
98      *
99      * BTW, malloc seems to do 8-byte alignment by default here.
100      */
101 #else
102     ptr = malloc(size);
103 #endif
104     return ptr;
105 }
106
107 void *av_realloc(void *ptr, size_t size)
108 {
109     /* let's disallow possibly ambiguous cases */
110     if (size > (INT_MAX - 16))
111         return NULL;
112
113 #if HAVE_ALIGNED_MALLOC
114     return _aligned_realloc(ptr, size, 32);
115 #else
116     return realloc(ptr, size);
117 #endif
118 }
119
120 int av_reallocp(void *ptr, size_t size)
121 {
122     void *val;
123
124     if (!size) {
125         av_freep(ptr);
126         return 0;
127     }
128
129     memcpy(&val, ptr, sizeof(val));
130     val = av_realloc(val, size);
131
132     if (!val) {
133         av_freep(ptr);
134         return AVERROR(ENOMEM);
135     }
136
137     memcpy(ptr, &val, sizeof(val));
138     return 0;
139 }
140
141 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
142 {
143     if (!size || nmemb >= INT_MAX / size)
144         return NULL;
145     return av_realloc(ptr, nmemb * size);
146 }
147
148 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
149 {
150     void *val;
151
152     if (!size || nmemb >= INT_MAX / size)
153         return AVERROR(ENOMEM);
154     if (!nmemb) {
155         av_freep(ptr);
156         return 0;
157     }
158
159     memcpy(&val, ptr, sizeof(val));
160     val = av_realloc(val, nmemb * size);
161     if (!val) {
162         av_freep(ptr);
163         return AVERROR(ENOMEM);
164     }
165
166     memcpy(ptr, &val, sizeof(val));
167     return 0;
168 }
169
170 void av_free(void *ptr)
171 {
172 #if HAVE_ALIGNED_MALLOC
173     _aligned_free(ptr);
174 #else
175     free(ptr);
176 #endif
177 }
178
179 void av_freep(void *arg)
180 {
181     void *val;
182
183     memcpy(&val, arg, sizeof(val));
184     memcpy(arg, &(void *){ NULL }, sizeof(val));
185     av_free(val);
186 }
187
188 void *av_mallocz(size_t size)
189 {
190     void *ptr = av_malloc(size);
191     if (ptr)
192         memset(ptr, 0, size);
193     return ptr;
194 }
195
196 char *av_strdup(const char *s)
197 {
198     char *ptr = NULL;
199     if (s) {
200         int len = strlen(s) + 1;
201         ptr = av_realloc(NULL, len);
202         if (ptr)
203             memcpy(ptr, s, len);
204     }
205     return ptr;
206 }
207
208 char *av_strndup(const char *s, size_t len)
209 {
210     char *ret = NULL, *end;
211
212     if (!s)
213         return NULL;
214
215     end = memchr(s, 0, len);
216     if (end)
217         len = end - s;
218
219     ret = av_realloc(NULL, len + 1);
220     if (!ret)
221         return NULL;
222
223     memcpy(ret, s, len);
224     ret[len] = 0;
225     return ret;
226 }
227
228 static void fill16(uint8_t *dst, int len)
229 {
230     uint32_t v = AV_RN16(dst - 2);
231
232     v |= v << 16;
233
234     while (len >= 4) {
235         AV_WN32(dst, v);
236         dst += 4;
237         len -= 4;
238     }
239
240     while (len--) {
241         *dst = dst[-2];
242         dst++;
243     }
244 }
245
246 static void fill24(uint8_t *dst, int len)
247 {
248 #if HAVE_BIGENDIAN
249     uint32_t v = AV_RB24(dst - 3);
250     uint32_t a = v << 8  | v >> 16;
251     uint32_t b = v << 16 | v >> 8;
252     uint32_t c = v << 24 | v;
253 #else
254     uint32_t v = AV_RL24(dst - 3);
255     uint32_t a = v       | v << 24;
256     uint32_t b = v >> 8  | v << 16;
257     uint32_t c = v >> 16 | v << 8;
258 #endif
259
260     while (len >= 12) {
261         AV_WN32(dst,     a);
262         AV_WN32(dst + 4, b);
263         AV_WN32(dst + 8, c);
264         dst += 12;
265         len -= 12;
266     }
267
268     if (len >= 4) {
269         AV_WN32(dst, a);
270         dst += 4;
271         len -= 4;
272     }
273
274     if (len >= 4) {
275         AV_WN32(dst, b);
276         dst += 4;
277         len -= 4;
278     }
279
280     while (len--) {
281         *dst = dst[-3];
282         dst++;
283     }
284 }
285
286 static void fill32(uint8_t *dst, int len)
287 {
288     uint32_t v = AV_RN32(dst - 4);
289
290     while (len >= 4) {
291         AV_WN32(dst, v);
292         dst += 4;
293         len -= 4;
294     }
295
296     while (len--) {
297         *dst = dst[-4];
298         dst++;
299     }
300 }
301
302 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
303 {
304     const uint8_t *src = &dst[-back];
305     if (!back)
306         return;
307
308     if (back == 1) {
309         memset(dst, *src, cnt);
310     } else if (back == 2) {
311         fill16(dst, cnt);
312     } else if (back == 3) {
313         fill24(dst, cnt);
314     } else if (back == 4) {
315         fill32(dst, cnt);
316     } else {
317         if (cnt >= 16) {
318             int blocklen = back;
319             while (cnt > blocklen) {
320                 memcpy(dst, src, blocklen);
321                 dst       += blocklen;
322                 cnt       -= blocklen;
323                 blocklen <<= 1;
324             }
325             memcpy(dst, src, cnt);
326             return;
327         }
328         if (cnt >= 8) {
329             AV_COPY32U(dst,     src);
330             AV_COPY32U(dst + 4, src + 4);
331             src += 8;
332             dst += 8;
333             cnt -= 8;
334         }
335         if (cnt >= 4) {
336             AV_COPY32U(dst, src);
337             src += 4;
338             dst += 4;
339             cnt -= 4;
340         }
341         if (cnt >= 2) {
342             AV_COPY16U(dst, src);
343             src += 2;
344             dst += 2;
345             cnt -= 2;
346         }
347         if (cnt)
348             *dst = *src;
349     }
350 }
351
352 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
353 {
354     if (min_size < *size)
355         return ptr;
356
357     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
358
359     ptr = av_realloc(ptr, min_size);
360     /* we could set this to the unmodified min_size but this is safer
361      * if the user lost the ptr and uses NULL now
362      */
363     if (!ptr)
364         min_size = 0;
365
366     *size = min_size;
367
368     return ptr;
369 }
370
371 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
372 {
373     void **p = ptr;
374     if (min_size < *size)
375         return;
376     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
377     av_free(*p);
378     *p = av_malloc(min_size);
379     if (!*p)
380         min_size = 0;
381     *size = min_size;
382 }