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