]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.c
Merge commit '7e350379f87e7f74420b4813170fe808e2313911'
[ffmpeg] / libavutil / mem.c
1 /*
2  * default memory allocator for libavutil
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #define _XOPEN_SOURCE 600
28
29 #include "config.h"
30
31 #include <limits.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #if HAVE_MALLOC_H
36 #include <malloc.h>
37 #endif
38
39 #include "avassert.h"
40 #include "avutil.h"
41 #include "intreadwrite.h"
42 #include "mem.h"
43
44 #ifdef MALLOC_PREFIX
45
46 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
47 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
48 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
49 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
50 #define free           AV_JOIN(MALLOC_PREFIX, free)
51
52 void *malloc(size_t size);
53 void *memalign(size_t align, size_t size);
54 int   posix_memalign(void **ptr, size_t align, size_t size);
55 void *realloc(void *ptr, size_t size);
56 void  free(void *ptr);
57
58 #endif /* MALLOC_PREFIX */
59
60 #define ALIGN (HAVE_AVX ? 32 : 16)
61
62 /* NOTE: if you want to override these functions with your own
63  * implementations (not recommended) you have to link libav* as
64  * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
65  * Note that this will cost performance. */
66
67 static size_t max_alloc_size= INT_MAX;
68
69 void av_max_alloc(size_t max){
70     max_alloc_size = max;
71 }
72
73 void *av_malloc(size_t size)
74 {
75     void *ptr = NULL;
76 #if CONFIG_MEMALIGN_HACK
77     long diff;
78 #endif
79
80     /* let's disallow possible ambiguous cases */
81     if (size > (max_alloc_size - 32))
82         return NULL;
83
84 #if CONFIG_MEMALIGN_HACK
85     ptr = malloc(size + ALIGN);
86     if (!ptr)
87         return ptr;
88     diff              = ((~(long)ptr)&(ALIGN - 1)) + 1;
89     ptr               = (char *)ptr + diff;
90     ((char *)ptr)[-1] = diff;
91 #elif HAVE_POSIX_MEMALIGN
92     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
93     if (posix_memalign(&ptr, ALIGN, size))
94         ptr = NULL;
95 #elif HAVE_ALIGNED_MALLOC
96     ptr = _aligned_malloc(size, ALIGN);
97 #elif HAVE_MEMALIGN
98     ptr = memalign(ALIGN, size);
99     /* Why 64?
100      * Indeed, we should align it:
101      *   on  4 for 386
102      *   on 16 for 486
103      *   on 32 for 586, PPro - K6-III
104      *   on 64 for K7 (maybe for P3 too).
105      * Because L1 and L2 caches are aligned on those values.
106      * But I don't want to code such logic here!
107      */
108     /* Why 32?
109      * For AVX ASM. SSE / NEON needs only 16.
110      * Why not larger? Because I did not see a difference in benchmarks ...
111      */
112     /* benchmarks with P3
113      * memalign(64) + 1          3071, 3051, 3032
114      * memalign(64) + 2          3051, 3032, 3041
115      * memalign(64) + 4          2911, 2896, 2915
116      * memalign(64) + 8          2545, 2554, 2550
117      * memalign(64) + 16         2543, 2572, 2563
118      * memalign(64) + 32         2546, 2545, 2571
119      * memalign(64) + 64         2570, 2533, 2558
120      *
121      * BTW, malloc seems to do 8-byte alignment by default here.
122      */
123 #else
124     ptr = malloc(size);
125 #endif
126     if(!ptr && !size) {
127         size = 1;
128         ptr= av_malloc(1);
129     }
130 #if CONFIG_MEMORY_POISONING
131     if (ptr)
132         memset(ptr, 0x2a, size);
133 #endif
134     return ptr;
135 }
136
137 void *av_realloc(void *ptr, size_t size)
138 {
139 #if CONFIG_MEMALIGN_HACK
140     int diff;
141 #endif
142
143     /* let's disallow possible ambiguous cases */
144     if (size > (max_alloc_size - 32))
145         return NULL;
146
147 #if CONFIG_MEMALIGN_HACK
148     //FIXME this isn't aligned correctly, though it probably isn't needed
149     if (!ptr)
150         return av_malloc(size);
151     diff = ((char *)ptr)[-1];
152     av_assert0(diff>0 && diff<=ALIGN);
153     ptr = realloc((char *)ptr - diff, size + diff);
154     if (ptr)
155         ptr = (char *)ptr + diff;
156     return ptr;
157 #elif HAVE_ALIGNED_MALLOC
158     return _aligned_realloc(ptr, size + !size, ALIGN);
159 #else
160     return realloc(ptr, size + !size);
161 #endif
162 }
163
164 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
165 {
166     size_t size;
167     void *r;
168
169     if (av_size_mult(elsize, nelem, &size)) {
170         av_free(ptr);
171         return NULL;
172     }
173     r = av_realloc(ptr, size);
174     if (!r && size)
175         av_free(ptr);
176     return r;
177 }
178
179 void av_free(void *ptr)
180 {
181 #if CONFIG_MEMALIGN_HACK
182     if (ptr) {
183         int v= ((char *)ptr)[-1];
184         av_assert0(v>0 && v<=ALIGN);
185         free((char *)ptr - v);
186     }
187 #elif HAVE_ALIGNED_MALLOC
188     _aligned_free(ptr);
189 #else
190     free(ptr);
191 #endif
192 }
193
194 void av_freep(void *arg)
195 {
196     void **ptr = (void **)arg;
197     av_free(*ptr);
198     *ptr = NULL;
199 }
200
201 void *av_mallocz(size_t size)
202 {
203     void *ptr = av_malloc(size);
204     if (ptr)
205         memset(ptr, 0, size);
206     return ptr;
207 }
208
209 void *av_calloc(size_t nmemb, size_t size)
210 {
211     if (size <= 0 || nmemb >= INT_MAX / size)
212         return NULL;
213     return av_mallocz(nmemb * size);
214 }
215
216 char *av_strdup(const char *s)
217 {
218     char *ptr = NULL;
219     if (s) {
220         int len = strlen(s) + 1;
221         ptr = av_malloc(len);
222         if (ptr)
223             memcpy(ptr, s, len);
224     }
225     return ptr;
226 }
227
228 /* add one element to a dynamic array */
229 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
230 {
231     /* see similar ffmpeg.c:grow_array() */
232     int nb, nb_alloc;
233     intptr_t *tab;
234
235     nb = *nb_ptr;
236     tab = *(intptr_t**)tab_ptr;
237     if ((nb & (nb - 1)) == 0) {
238         if (nb == 0)
239             nb_alloc = 1;
240         else
241             nb_alloc = nb * 2;
242         tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
243         *(intptr_t**)tab_ptr = tab;
244     }
245     tab[nb++] = (intptr_t)elem;
246     *nb_ptr = nb;
247 }
248
249 static void fill16(uint8_t *dst, int len)
250 {
251     uint32_t v = AV_RN16(dst - 2);
252
253     v |= v << 16;
254
255     while (len >= 4) {
256         AV_WN32(dst, v);
257         dst += 4;
258         len -= 4;
259     }
260
261     while (len--) {
262         *dst = dst[-2];
263         dst++;
264     }
265 }
266
267 static void fill24(uint8_t *dst, int len)
268 {
269 #if HAVE_BIGENDIAN
270     uint32_t v = AV_RB24(dst - 3);
271     uint32_t a = v << 8  | v >> 16;
272     uint32_t b = v << 16 | v >> 8;
273     uint32_t c = v << 24 | v;
274 #else
275     uint32_t v = AV_RL24(dst - 3);
276     uint32_t a = v       | v << 24;
277     uint32_t b = v >> 8  | v << 16;
278     uint32_t c = v >> 16 | v << 8;
279 #endif
280
281     while (len >= 12) {
282         AV_WN32(dst,     a);
283         AV_WN32(dst + 4, b);
284         AV_WN32(dst + 8, c);
285         dst += 12;
286         len -= 12;
287     }
288
289     if (len >= 4) {
290         AV_WN32(dst, a);
291         dst += 4;
292         len -= 4;
293     }
294
295     if (len >= 4) {
296         AV_WN32(dst, b);
297         dst += 4;
298         len -= 4;
299     }
300
301     while (len--) {
302         *dst = dst[-3];
303         dst++;
304     }
305 }
306
307 static void fill32(uint8_t *dst, int len)
308 {
309     uint32_t v = AV_RN32(dst - 4);
310
311     while (len >= 4) {
312         AV_WN32(dst, v);
313         dst += 4;
314         len -= 4;
315     }
316
317     while (len--) {
318         *dst = dst[-4];
319         dst++;
320     }
321 }
322
323 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
324 {
325     const uint8_t *src = &dst[-back];
326     if (!back)
327         return;
328
329     if (back == 1) {
330         memset(dst, *src, cnt);
331     } else if (back == 2) {
332         fill16(dst, cnt);
333     } else if (back == 3) {
334         fill24(dst, cnt);
335     } else if (back == 4) {
336         fill32(dst, cnt);
337     } else {
338         if (cnt >= 16) {
339             int blocklen = back;
340             while (cnt > blocklen) {
341                 memcpy(dst, src, blocklen);
342                 dst       += blocklen;
343                 cnt       -= blocklen;
344                 blocklen <<= 1;
345             }
346             memcpy(dst, src, cnt);
347             return;
348         }
349         if (cnt >= 8) {
350             AV_COPY32U(dst,     src);
351             AV_COPY32U(dst + 4, src + 4);
352             src += 8;
353             dst += 8;
354             cnt -= 8;
355         }
356         if (cnt >= 4) {
357             AV_COPY32U(dst, src);
358             src += 4;
359             dst += 4;
360             cnt -= 4;
361         }
362         if (cnt >= 2) {
363             AV_COPY16U(dst, src);
364             src += 2;
365             dst += 2;
366             cnt -= 2;
367         }
368         if (cnt)
369             *dst = *src;
370     }
371 }
372