]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.c
Merge remote-tracking branch 'qatar/master'
[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 <stdlib.h>
33 #include <string.h>
34 #if HAVE_MALLOC_H
35 #include <malloc.h>
36 #endif
37
38 #include "avutil.h"
39 #include "mem.h"
40
41 /* here we can use OS-dependent allocation functions */
42 #undef free
43 #undef malloc
44 #undef realloc
45
46 #ifdef MALLOC_PREFIX
47
48 #define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
49 #define memalign       AV_JOIN(MALLOC_PREFIX, memalign)
50 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
51 #define realloc        AV_JOIN(MALLOC_PREFIX, realloc)
52 #define free           AV_JOIN(MALLOC_PREFIX, free)
53
54 void *malloc(size_t size);
55 void *memalign(size_t align, size_t size);
56 int   posix_memalign(void **ptr, size_t align, size_t size);
57 void *realloc(void *ptr, size_t size);
58 void  free(void *ptr);
59
60 #endif /* MALLOC_PREFIX */
61
62 #define ALIGN (HAVE_AVX ? 32 : 16)
63
64 /* NOTE: if you want to override these functions with your own
65  * implementations (not recommended) you have to link libav* as
66  * dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
67  * Note that this will cost performance. */
68
69 static size_t max_alloc_size= INT_MAX;
70
71 void av_max_alloc(size_t max){
72     max_alloc_size = max;
73 }
74
75 void *av_malloc(size_t size)
76 {
77     void *ptr = NULL;
78 #if CONFIG_MEMALIGN_HACK
79     long diff;
80 #endif
81
82     /* let's disallow possible ambiguous cases */
83     if (size > (max_alloc_size-32))
84         return NULL;
85
86 #if CONFIG_MEMALIGN_HACK
87     ptr = malloc(size+ALIGN);
88     if(!ptr)
89         return ptr;
90     diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
91     ptr = (char*)ptr + diff;
92     ((char*)ptr)[-1]= diff;
93 #elif HAVE_POSIX_MEMALIGN
94     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
95     if (posix_memalign(&ptr,ALIGN,size))
96         ptr = NULL;
97 #elif HAVE_ALIGNED_MALLOC
98     ptr = _aligned_malloc(size, ALIGN);
99 #elif HAVE_MEMALIGN
100     ptr = memalign(ALIGN,size);
101     /* Why 64?
102        Indeed, we should align it:
103          on 4 for 386
104          on 16 for 486
105          on 32 for 586, PPro - K6-III
106          on 64 for K7 (maybe for P3 too).
107        Because L1 and L2 caches are aligned on those values.
108        But I don't want to code such logic here!
109      */
110      /* Why 32?
111         For AVX ASM. SSE / NEON needs only 16.
112         Why not larger? Because I did not see a difference in benchmarks ...
113      */
114      /* benchmarks with P3
115         memalign(64)+1          3071,3051,3032
116         memalign(64)+2          3051,3032,3041
117         memalign(64)+4          2911,2896,2915
118         memalign(64)+8          2545,2554,2550
119         memalign(64)+16         2543,2572,2563
120         memalign(64)+32         2546,2545,2571
121         memalign(64)+64         2570,2533,2558
122
123         BTW, malloc seems to do 8-byte alignment by default here.
124      */
125 #else
126     ptr = malloc(size);
127 #endif
128     if(!ptr && !size) {
129         size = 1;
130         ptr= av_malloc(1);
131     }
132 #if CONFIG_MEMORY_POISONING
133     if (ptr)
134         memset(ptr, 0x2a, size);
135 #endif
136     return ptr;
137 }
138
139 void *av_realloc(void *ptr, size_t size)
140 {
141 #if CONFIG_MEMALIGN_HACK
142     int diff;
143 #endif
144
145     /* let's disallow possible ambiguous cases */
146     if (size > (max_alloc_size-32))
147         return NULL;
148
149 #if CONFIG_MEMALIGN_HACK
150     //FIXME this isn't aligned correctly, though it probably isn't needed
151     if(!ptr) return av_malloc(size);
152     diff= ((char*)ptr)[-1];
153     ptr= realloc((char*)ptr - diff, size + diff);
154     if(ptr) ptr = (char*)ptr + diff;
155     return ptr;
156 #elif HAVE_ALIGNED_MALLOC
157     return _aligned_realloc(ptr, size + !size, ALIGN);
158 #else
159     return realloc(ptr, size + !size);
160 #endif
161 }
162
163 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
164 {
165     size_t size;
166     void *r;
167
168     if (av_size_mult(elsize, nelem, &size)) {
169         av_free(ptr);
170         return NULL;
171     }
172     r = av_realloc(ptr, size);
173     if (!r && size)
174         av_free(ptr);
175     return r;
176 }
177
178 void av_free(void *ptr)
179 {
180 #if CONFIG_MEMALIGN_HACK
181     if (ptr)
182         free((char*)ptr - ((char*)ptr)[-1]);
183 #elif HAVE_ALIGNED_MALLOC
184     _aligned_free(ptr);
185 #else
186     free(ptr);
187 #endif
188 }
189
190 void av_freep(void *arg)
191 {
192     void **ptr= (void**)arg;
193     av_free(*ptr);
194     *ptr = NULL;
195 }
196
197 void *av_mallocz(size_t size)
198 {
199     void *ptr = av_malloc(size);
200     if (ptr)
201         memset(ptr, 0, size);
202     return ptr;
203 }
204
205 void *av_calloc(size_t nmemb, size_t size)
206 {
207     if (size <= 0 || nmemb >= INT_MAX / size)
208         return NULL;
209     return av_mallocz(nmemb * size);
210 }
211
212 char *av_strdup(const char *s)
213 {
214     char *ptr= NULL;
215     if(s){
216         int len = strlen(s) + 1;
217         ptr = av_malloc(len);
218         if (ptr)
219             memcpy(ptr, s, len);
220     }
221     return ptr;
222 }
223
224 /* add one element to a dynamic array */
225 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
226 {
227     /* see similar ffmpeg.c:grow_array() */
228     int nb, nb_alloc;
229     intptr_t *tab;
230
231     nb = *nb_ptr;
232     tab = *(intptr_t**)tab_ptr;
233     if ((nb & (nb - 1)) == 0) {
234         if (nb == 0)
235             nb_alloc = 1;
236         else
237             nb_alloc = nb * 2;
238         tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
239         *(intptr_t**)tab_ptr = tab;
240     }
241     tab[nb++] = (intptr_t)elem;
242     *nb_ptr = nb;
243 }
244