]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.c
Merge commit '9ce02e14f01de50fcc6f7f459544b140be66d615'
[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 "avutil.h"
40 #include "intreadwrite.h"
41 #include "mem.h"
42
43 /* here we can use OS-dependent allocation functions */
44 #undef free
45 #undef malloc
46 #undef realloc
47
48 #ifdef MALLOC_PREFIX
49
50 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
51 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
52 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
53 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
54 #define free           AV_JOIN(MALLOC_PREFIX, free)
55
56 void *malloc(size_t size);
57 void *memalign(size_t align, size_t size);
58 int   posix_memalign(void **ptr, size_t align, size_t size);
59 void *realloc(void *ptr, size_t size);
60 void  free(void *ptr);
61
62 #endif /* MALLOC_PREFIX */
63
64 #define ALIGN (HAVE_AVX ? 32 : 16)
65
66 /* NOTE: if you want to override these functions with your own
67  * implementations (not recommended) you have to link libav* as
68  * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
69  * Note that this will cost performance. */
70
71 static size_t max_alloc_size= INT_MAX;
72
73 void av_max_alloc(size_t max){
74     max_alloc_size = max;
75 }
76
77 void *av_malloc(size_t size)
78 {
79     void *ptr = NULL;
80 #if CONFIG_MEMALIGN_HACK
81     long diff;
82 #endif
83
84     /* let's disallow possible ambiguous cases */
85     if (size > (max_alloc_size - 32))
86         return NULL;
87
88 #if CONFIG_MEMALIGN_HACK
89     ptr = malloc(size + ALIGN);
90     if (!ptr)
91         return ptr;
92     diff              = ((-(long)ptr - 1)&(ALIGN - 1)) + 1;
93     ptr               = (char *)ptr + diff;
94     ((char *)ptr)[-1] = diff;
95 #elif HAVE_POSIX_MEMALIGN
96     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
97     if (posix_memalign(&ptr, ALIGN, size))
98         ptr = NULL;
99 #elif HAVE_ALIGNED_MALLOC
100     ptr = _aligned_malloc(size, ALIGN);
101 #elif HAVE_MEMALIGN
102     ptr = memalign(ALIGN, size);
103     /* Why 64?
104      * Indeed, we should align it:
105      *   on  4 for 386
106      *   on 16 for 486
107      *   on 32 for 586, PPro - K6-III
108      *   on 64 for K7 (maybe for P3 too).
109      * Because L1 and L2 caches are aligned on those values.
110      * But I don't want to code such logic here!
111      */
112     /* Why 32?
113      * For AVX ASM. SSE / NEON needs only 16.
114      * Why not larger? Because I did not see a difference in benchmarks ...
115      */
116     /* benchmarks with P3
117      * memalign(64) + 1          3071, 3051, 3032
118      * memalign(64) + 2          3051, 3032, 3041
119      * memalign(64) + 4          2911, 2896, 2915
120      * memalign(64) + 8          2545, 2554, 2550
121      * memalign(64) + 16         2543, 2572, 2563
122      * memalign(64) + 32         2546, 2545, 2571
123      * memalign(64) + 64         2570, 2533, 2558
124      *
125      * BTW, malloc seems to do 8-byte alignment by default here.
126      */
127 #else
128     ptr = malloc(size);
129 #endif
130     if(!ptr && !size) {
131         size = 1;
132         ptr= av_malloc(1);
133     }
134 #if CONFIG_MEMORY_POISONING
135     if (ptr)
136         memset(ptr, 0x2a, size);
137 #endif
138     return ptr;
139 }
140
141 void *av_realloc(void *ptr, size_t size)
142 {
143 #if CONFIG_MEMALIGN_HACK
144     int diff;
145 #endif
146
147     /* let's disallow possible ambiguous cases */
148     if (size > (max_alloc_size - 32))
149         return NULL;
150
151 #if CONFIG_MEMALIGN_HACK
152     //FIXME this isn't aligned correctly, though it probably isn't needed
153     if (!ptr)
154         return av_malloc(size);
155     diff = ((char *)ptr)[-1];
156     ptr = realloc((char *)ptr - diff, size + diff);
157     if (ptr)
158         ptr = (char *)ptr + diff;
159     return ptr;
160 #elif HAVE_ALIGNED_MALLOC
161     return _aligned_realloc(ptr, size + !size, ALIGN);
162 #else
163     return realloc(ptr, size + !size);
164 #endif
165 }
166
167 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
168 {
169     size_t size;
170     void *r;
171
172     if (av_size_mult(elsize, nelem, &size)) {
173         av_free(ptr);
174         return NULL;
175     }
176     r = av_realloc(ptr, size);
177     if (!r && size)
178         av_free(ptr);
179     return r;
180 }
181
182 void av_free(void *ptr)
183 {
184 #if CONFIG_MEMALIGN_HACK
185     if (ptr)
186         free((char *)ptr - ((char *)ptr)[-1]);
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 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
250 {
251     const uint8_t *src = &dst[-back];
252     if (back <= 1) {
253         memset(dst, *src, cnt);
254     } else {
255         if (cnt >= 4) {
256             AV_COPY16U(dst,     src);
257             AV_COPY16U(dst + 2, src + 2);
258             src += 4;
259             dst += 4;
260             cnt -= 4;
261         }
262         if (cnt >= 8) {
263             AV_COPY16U(dst,     src);
264             AV_COPY16U(dst + 2, src + 2);
265             AV_COPY16U(dst + 4, src + 4);
266             AV_COPY16U(dst + 6, src + 6);
267             src += 8;
268             dst += 8;
269             cnt -= 8;
270         }
271         if (cnt > 0) {
272             int blocklen = back;
273             while (cnt > blocklen) {
274                 memcpy(dst, src, blocklen);
275                 dst       += blocklen;
276                 cnt       -= blocklen;
277                 blocklen <<= 1;
278             }
279             memcpy(dst, src, cnt);
280         }
281     }
282 }
283