Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 245 lines (213 sloc) 6.025 kB
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
1 /*
cea8f6f Move av_malloc(), av_realloc(), and av_free() from libavcodec to liba…
Luca Abeni authored
2 * default memory allocator for libavutil
406792e @DonDiego cosmetics: Remove pointless period after copyright statement non-sent…
DonDiego authored
3 * Copyright (c) 2002 Fabrice Bellard
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
4 *
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
10 * version 2.1 of the License, or (at your option) any later version.
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
11 *
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
12 * FFmpeg is distributed in the hope that it will be useful,
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
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
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
18 * License along with FFmpeg; if not, write to the Free Software
5509bff @DonDiego Update licensing information: The FSF changed postal address.
DonDiego authored
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
20 */
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
21
983e324 @michaelni per file doxy
michaelni authored
22 /**
ba87f08 @DonDiego Remove explicit filename from Doxygen @file commands.
DonDiego authored
23 * @file
89c9ff5 @DonDiego spelling/grammar/consistency review part I
DonDiego authored
24 * default memory allocator for libavutil
983e324 @michaelni per file doxy
michaelni authored
25 */
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
26
518cdff @iiv3 Fix posix_memalign() usage when libavutil is compiled outside FFmpeg.
iiv3 authored
27 #define _XOPEN_SOURCE 600
28
dfcb6b5 @DonDiego Directly #include a bunch of indirectly #included headers.
DonDiego authored
29 #include "config.h"
8e1e6f3 use av_malloc() functions - added av_strdup and av_realloc()
Fabrice Bellard authored
30
dfcb6b5 @DonDiego Directly #include a bunch of indirectly #included headers.
DonDiego authored
31 #include <limits.h>
1f91cdc @ramiropolla Use posix_memalign() if available.
ramiropolla authored
32 #include <stdlib.h>
dfcb6b5 @DonDiego Directly #include a bunch of indirectly #included headers.
DonDiego authored
33 #include <string.h>
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
34 #if HAVE_MALLOC_H
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
35 #include <malloc.h>
36 #endif
37
4ae4068 @mansr Add --malloc-prefix to apply a prefix to malloc, free etc
mansr authored
38 #include "avutil.h"
77652a6 @DonDiego C files should #include the header files of the same name.
DonDiego authored
39 #include "mem.h"
40
89c9ff5 @DonDiego spelling/grammar/consistency review part I
DonDiego authored
41 /* here we can use OS-dependent allocation functions */
1e60e93 @DonDiego 10l: Add #undefs for system free/malloc/realloc, which must be used h…
DonDiego authored
42 #undef free
43 #undef malloc
44 #undef realloc
45
4ae4068 @mansr Add --malloc-prefix to apply a prefix to malloc, free etc
mansr authored
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
08675bb @michaelni mem: Choose alignment at compile time depending on AVX enabledness.
michaelni authored
62 #define ALIGN (HAVE_AVX ? 32 : 16)
63
1a65d50 mem.c: fix wrong comment.
Reimar Döffinger authored
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. */
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
68
5a8e994 @michaelni mem: add av_max_alloc() to limit the maximum amount that may be alloc…
michaelni authored
69 static size_t max_alloc_size= INT_MAX;
70
71 void av_max_alloc(size_t max){
72 max_alloc_size = max;
73 }
6b4c0be mem: define the MAX_MALLOC_SIZE constant and use it in place of INT_MAX
Stefano Sabatini authored
74
490a022 @elenril lavu: remove misc disabled cruft
elenril authored
75 void *av_malloc(size_t size)
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
76 {
1f91cdc @ramiropolla Use posix_memalign() if available.
ramiropolla authored
77 void *ptr = NULL;
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
78 #if CONFIG_MEMALIGN_HACK
ed96aee long is better than int for pointer differences
Dieter authored
79 long diff;
88730be @mansr kill warnings patch by (Måns Rullgård <mru inprovide com>)
mansr authored
80 #endif
0ecca7a @michaelni various security fixes and precautionary checks
michaelni authored
81
7d77d5f @DonDiego spelling fixes courtesy of "Hervé W." H.O.W.aka.V+ffmpeg (at) gmail (…
DonDiego authored
82 /* let's disallow possible ambiguous cases */
5a8e994 @michaelni mem: add av_max_alloc() to limit the maximum amount that may be alloc…
michaelni authored
83 if (size > (max_alloc_size-32))
0ecca7a @michaelni various security fixes and precautionary checks
michaelni authored
84 return NULL;
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
85
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
86 #if CONFIG_MEMALIGN_HACK
08675bb @michaelni mem: Choose alignment at compile time depending on AVX enabledness.
michaelni authored
87 ptr = malloc(size+ALIGN);
a949360 * Allocating 16 bytes more for the MEMALIGN_HACK is enough. There's no
Herve W authored
88 if(!ptr)
89 return ptr;
08675bb @michaelni mem: Choose alignment at compile time depending on AVX enabledness.
michaelni authored
90 diff= ((-(long)ptr - 1)&(ALIGN-1)) + 1;
90d3057 @nxtreaming Avoid void*-arithmetic.
nxtreaming authored
91 ptr = (char*)ptr + diff;
da9b170 @michaelni optional and disabled by default memalign hack for SSE/SSE2 on that a…
michaelni authored
92 ((char*)ptr)[-1]= diff;
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
93 #elif HAVE_POSIX_MEMALIGN
bed04e2 Correct use of OSX to Mac OS X and properly format yasm providers on …
Hanspeter Niederstrasser authored
94 if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
08675bb @michaelni mem: Choose alignment at compile time depending on AVX enabledness.
michaelni authored
95 if (posix_memalign(&ptr,ALIGN,size))
a90de11 Check return value of posix_memalign and explicitly set pointer to NU…
Patrik Kullman authored
96 ptr = NULL;
be1a839 @rbultje mem: add support for _aligned_malloc() as found on Windows
rbultje authored
97 #elif HAVE_ALIGNED_MALLOC
cabbd27 @michaelni Merge remote-tracking branch 'qatar/master'
michaelni authored
98 ptr = _aligned_malloc(size, ALIGN);
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
99 #elif HAVE_MEMALIGN
08675bb @michaelni mem: Choose alignment at compile time depending on AVX enabledness.
michaelni authored
100 ptr = memalign(ALIGN,size);
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
101 /* Why 64?
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
102 Indeed, we should align it:
103 on 4 for 386
104 on 16 for 486
89c9ff5 @DonDiego spelling/grammar/consistency review part I
DonDiego authored
105 on 32 for 586, PPro - K6-III
bb270c0 @DonDiego COSMETICS: tabs --> spaces, some prettyprinting
DonDiego authored
106 on 64 for K7 (maybe for P3 too).
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
107 Because L1 and L2 caches are aligned on those values.
108 But I don't want to code such logic here!
109 */
13dfce3 Increase alignment of av_malloc() as needed by AVX ASM.
Vitor Sessak authored
110 /* Why 32?
111 For AVX ASM. SSE / NEON needs only 16.
2cab640 @DonDiego typo fixes
DonDiego authored
112 Why not larger? Because I did not see a difference in benchmarks ...
8f2b21a @michaelni fixing memalign
michaelni authored
113 */
89c9ff5 @DonDiego spelling/grammar/consistency review part I
DonDiego authored
114 /* benchmarks with P3
bb270c0 @DonDiego COSMETICS: tabs --> spaces, some prettyprinting
DonDiego authored
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
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
122
89c9ff5 @DonDiego spelling/grammar/consistency review part I
DonDiego authored
123 BTW, malloc seems to do 8-byte alignment by default here.
8f2b21a @michaelni fixing memalign
michaelni authored
124 */
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
125 #else
126 ptr = malloc(size);
127 #endif
7c84e7d @ubitux mem: heap memory poisoning.
ubitux authored
128 if(!ptr && !size) {
129 size = 1;
c8981ed @michaelni Only add 1 byte to av_malloc(0) when it actually returned NULL
michaelni authored
130 ptr= av_malloc(1);
7c84e7d @ubitux mem: heap memory poisoning.
ubitux authored
131 }
132 #if CONFIG_MEMORY_POISONING
133 if (ptr)
134 memset(ptr, 0x2a, size);
135 #endif
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
136 return ptr;
137 }
138
490a022 @elenril lavu: remove misc disabled cruft
elenril authored
139 void *av_realloc(void *ptr, size_t size)
8e1e6f3 use av_malloc() functions - added av_strdup and av_realloc()
Fabrice Bellard authored
140 {
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
141 #if CONFIG_MEMALIGN_HACK
0a7c36a @michaelni revert aligned realloc() changesm this should be identical to r5784
michaelni authored
142 int diff;
143 #endif
88730be @mansr kill warnings patch by (Måns Rullgård <mru inprovide com>)
mansr authored
144
7d77d5f @DonDiego spelling fixes courtesy of "Hervé W." H.O.W.aka.V+ffmpeg (at) gmail (…
DonDiego authored
145 /* let's disallow possible ambiguous cases */
5a8e994 @michaelni mem: add av_max_alloc() to limit the maximum amount that may be alloc…
michaelni authored
146 if (size > (max_alloc_size-32))
0ecca7a @michaelni various security fixes and precautionary checks
michaelni authored
147 return NULL;
148
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
149 #if CONFIG_MEMALIGN_HACK
0a7c36a @michaelni revert aligned realloc() changesm this should be identical to r5784
michaelni authored
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];
fc11927 @michaelni mem: fix memalign hack av_realloc()
michaelni authored
153 ptr= realloc((char*)ptr - diff, size + diff);
154 if(ptr) ptr = (char*)ptr + diff;
155 return ptr;
be1a839 @rbultje mem: add support for _aligned_malloc() as found on Windows
rbultje authored
156 #elif HAVE_ALIGNED_MALLOC
cabbd27 @michaelni Merge remote-tracking branch 'qatar/master'
michaelni authored
157 return _aligned_realloc(ptr, size + !size, ALIGN);
0a7c36a @michaelni revert aligned realloc() changesm this should be identical to r5784
michaelni authored
158 #else
91ff05f @michaelni Make av_realloc(0) behave like our av_malloc(0)
michaelni authored
159 return realloc(ptr, size + !size);
da9b170 @michaelni optional and disabled by default memalign hack for SSE/SSE2 on that a…
michaelni authored
160 #endif
b7a22d8 @michaelni cleanup
michaelni authored
161 }
162
5cd754b Introduce av_realloc_f.
Nicolas George authored
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
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
178 void av_free(void *ptr)
179 {
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
180 #if CONFIG_MEMALIGN_HACK
0c8eb72 @ubitux Avoid pointless check before calling free
ubitux authored
181 if (ptr)
90d3057 @nxtreaming Avoid void*-arithmetic.
nxtreaming authored
182 free((char*)ptr - ((char*)ptr)[-1]);
be1a839 @rbultje mem: add support for _aligned_malloc() as found on Windows
rbultje authored
183 #elif HAVE_ALIGNED_MALLOC
184 _aligned_free(ptr);
da9b170 @michaelni optional and disabled by default memalign hack for SSE/SSE2 on that a…
michaelni authored
185 #else
0c8eb72 @ubitux Avoid pointless check before calling free
ubitux authored
186 free(ptr);
da9b170 @michaelni optional and disabled by default memalign hack for SSE/SSE2 on that a…
michaelni authored
187 #endif
d01fe86 put memory functions in a separate file so that the user can redefini…
Fabrice Bellard authored
188 }
189
79e4700 @lu-zero move memory functions from avcodec to avutil
lu-zero authored
190 void av_freep(void *arg)
191 {
192 void **ptr= (void**)arg;
193 av_free(*ptr);
194 *ptr = NULL;
195 }
196
490a022 @elenril lavu: remove misc disabled cruft
elenril authored
197 void *av_mallocz(size_t size)
79e4700 @lu-zero move memory functions from avcodec to avutil
lu-zero authored
198 {
1136276 @michaelni Merge declaration and initialization.
michaelni authored
199 void *ptr = av_malloc(size);
79e4700 @lu-zero move memory functions from avcodec to avutil
lu-zero authored
200 if (ptr)
201 memset(ptr, 0, size);
202 return ptr;
203 }
204
ccecab4 Add av_calloc() helper.
Laurent Aimar authored
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
79e4700 @lu-zero move memory functions from avcodec to avutil
lu-zero authored
212 char *av_strdup(const char *s)
213 {
fdf35f2 @michaelni make av_strdup(NULL) return NULL
michaelni authored
214 char *ptr= NULL;
215 if(s){
19757f6 @michaelni indent
michaelni authored
216 int len = strlen(s) + 1;
217 ptr = av_malloc(len);
218 if (ptr)
219 memcpy(ptr, s, len);
fdf35f2 @michaelni make av_strdup(NULL) return NULL
michaelni authored
220 }
79e4700 @lu-zero move memory functions from avcodec to avutil
lu-zero authored
221 return ptr;
222 }
223
35ceaa7 @elenril Move ff_dynarray_add to lavu and make it public.
elenril authored
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 }
c065255 @michaelni Merge remote-tracking branch 'qatar/master'
michaelni authored
244
Something went wrong with that request. Please try again.