]> 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 /* You can redefine av_malloc and av_free in your project to use your
65    memory allocator. You do not need to suppress this file because the
66    linker will do it automatically. */
67
68 static size_t max_alloc_size= INT_MAX;
69
70 void av_max_alloc(size_t max){
71     max_alloc_size = max;
72 }
73
74 void *av_malloc(size_t size)
75 {
76     void *ptr = NULL;
77 #if CONFIG_MEMALIGN_HACK
78     long diff;
79 #endif
80
81     /* let's disallow possible ambiguous cases */
82     if (size > (max_alloc_size-32))
83         return NULL;
84
85 #if CONFIG_MEMALIGN_HACK
86     ptr = malloc(size+ALIGN);
87     if(!ptr)
88         return ptr;
89     diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
90     ptr = (char*)ptr + diff;
91     ((char*)ptr)[-1]= diff;
92 #elif HAVE_POSIX_MEMALIGN
93     if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
94     if (posix_memalign(&ptr,ALIGN,size))
95         ptr = NULL;
96 #elif HAVE_MEMALIGN
97     ptr = memalign(ALIGN,size);
98     /* Why 64?
99        Indeed, we should align it:
100          on 4 for 386
101          on 16 for 486
102          on 32 for 586, PPro - K6-III
103          on 64 for K7 (maybe for P3 too).
104        Because L1 and L2 caches are aligned on those values.
105        But I don't want to code such logic here!
106      */
107      /* Why 32?
108         For AVX ASM. SSE / NEON needs only 16.
109         Why not larger? Because I did not see a difference in benchmarks ...
110      */
111      /* benchmarks with P3
112         memalign(64)+1          3071,3051,3032
113         memalign(64)+2          3051,3032,3041
114         memalign(64)+4          2911,2896,2915
115         memalign(64)+8          2545,2554,2550
116         memalign(64)+16         2543,2572,2563
117         memalign(64)+32         2546,2545,2571
118         memalign(64)+64         2570,2533,2558
119
120         BTW, malloc seems to do 8-byte alignment by default here.
121      */
122 #else
123     ptr = malloc(size);
124 #endif
125     if(!ptr && !size)
126         ptr= av_malloc(1);
127     return ptr;
128 }
129
130 void *av_realloc(void *ptr, size_t size)
131 {
132 #if CONFIG_MEMALIGN_HACK
133     int diff;
134 #endif
135
136     /* let's disallow possible ambiguous cases */
137     if (size > (max_alloc_size-32))
138         return NULL;
139
140 #if CONFIG_MEMALIGN_HACK
141     //FIXME this isn't aligned correctly, though it probably isn't needed
142     if(!ptr) return av_malloc(size);
143     diff= ((char*)ptr)[-1];
144     ptr= realloc((char*)ptr - diff, size + diff);
145     if(ptr) ptr = (char*)ptr + diff;
146     return ptr;
147 #else
148     return realloc(ptr, size + !size);
149 #endif
150 }
151
152 void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
153 {
154     size_t size;
155     void *r;
156
157     if (av_size_mult(elsize, nelem, &size)) {
158         av_free(ptr);
159         return NULL;
160     }
161     r = av_realloc(ptr, size);
162     if (!r && size)
163         av_free(ptr);
164     return r;
165 }
166
167 void av_free(void *ptr)
168 {
169 #if CONFIG_MEMALIGN_HACK
170     if (ptr)
171         free((char*)ptr - ((char*)ptr)[-1]);
172 #else
173     free(ptr);
174 #endif
175 }
176
177 void av_freep(void *arg)
178 {
179     void **ptr= (void**)arg;
180     av_free(*ptr);
181     *ptr = NULL;
182 }
183
184 void *av_mallocz(size_t size)
185 {
186     void *ptr = av_malloc(size);
187     if (ptr)
188         memset(ptr, 0, size);
189     return ptr;
190 }
191
192 void *av_calloc(size_t nmemb, size_t size)
193 {
194     if (size <= 0 || nmemb >= INT_MAX / size)
195         return NULL;
196     return av_mallocz(nmemb * size);
197 }
198
199 char *av_strdup(const char *s)
200 {
201     char *ptr= NULL;
202     if(s){
203         int len = strlen(s) + 1;
204         ptr = av_malloc(len);
205         if (ptr)
206             memcpy(ptr, s, len);
207     }
208     return ptr;
209 }
210
211 /* add one element to a dynamic array */
212 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
213 {
214     /* see similar ffmpeg.c:grow_array() */
215     int nb, nb_alloc;
216     intptr_t *tab;
217
218     nb = *nb_ptr;
219     tab = *(intptr_t**)tab_ptr;
220     if ((nb & (nb - 1)) == 0) {
221         if (nb == 0)
222             nb_alloc = 1;
223         else
224             nb_alloc = nb * 2;
225         tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
226         *(intptr_t**)tab_ptr = tab;
227     }
228     tab[nb++] = (intptr_t)elem;
229     *nb_ptr = nb;
230 }