]> git.sesse.net Git - ffmpeg/blob - libavutil/mem.c
lavf: Don't try to update files atomically with renames on windows
[ffmpeg] / libavutil / mem.c
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 void *av_malloc(size_t size)
63 {
64     void *ptr = NULL;
65 #if CONFIG_MEMALIGN_HACK
66     long diff;
67 #endif
68
69     /* let's disallow possibly ambiguous cases */
70     if (size > (INT_MAX - 32) || !size)
71         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     if (posix_memalign(&ptr, 32, size))
82         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     return ptr;
115 }
116
117 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     if (size > (INT_MAX - 16))
125         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     return realloc(ptr, size);
137 #endif
138 }
139
140 int av_reallocp(void *ptr, size_t size)
141 {
142     void **ptrptr = ptr;
143     void *ret;
144
145     if (!size) {
146         av_freep(ptr);
147         return 0;
148     }
149     ret = av_realloc(*ptrptr, size);
150
151     if (!ret) {
152         av_freep(ptr);
153         return AVERROR(ENOMEM);
154     }
155
156     *ptrptr = ret;
157     return 0;
158 }
159
160 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
161 {
162     if (!size || nmemb >= INT_MAX / size)
163         return NULL;
164     return av_realloc(ptr, nmemb * size);
165 }
166
167 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
168 {
169     void **ptrptr = ptr;
170     void *ret;
171     if (!size || nmemb >= INT_MAX / size)
172         return AVERROR(ENOMEM);
173     if (!nmemb) {
174         av_freep(ptr);
175         return 0;
176     }
177     ret = av_realloc(*ptrptr, nmemb * size);
178     if (!ret) {
179         av_freep(ptr);
180         return AVERROR(ENOMEM);
181     }
182     *ptrptr = ret;
183     return 0;
184 }
185
186 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     free(ptr);
195 #endif
196 }
197
198 void av_freep(void *arg)
199 {
200     void **ptr = (void **)arg;
201     av_free(*ptr);
202     *ptr = NULL;
203 }
204
205 void *av_mallocz(size_t size)
206 {
207     void *ptr = av_malloc(size);
208     if (ptr)
209         memset(ptr, 0, size);
210     return ptr;
211 }
212
213 char *av_strdup(const char *s)
214 {
215     char *ptr = NULL;
216     if (s) {
217         int len = strlen(s) + 1;
218         ptr = av_realloc(NULL, len);
219         if (ptr)
220             memcpy(ptr, s, len);
221     }
222     return ptr;
223 }
224
225 char *av_strndup(const char *s, size_t len)
226 {
227     char *ret = NULL, *end;
228
229     if (!s)
230         return NULL;
231
232     end = memchr(s, 0, len);
233     if (end)
234         len = end - s;
235
236     ret = av_realloc(NULL, len + 1);
237     if (!ret)
238         return NULL;
239
240     memcpy(ret, s, len);
241     ret[len] = 0;
242     return ret;
243 }
244
245 static void fill16(uint8_t *dst, int len)
246 {
247     uint32_t v = AV_RN16(dst - 2);
248
249     v |= v << 16;
250
251     while (len >= 4) {
252         AV_WN32(dst, v);
253         dst += 4;
254         len -= 4;
255     }
256
257     while (len--) {
258         *dst = dst[-2];
259         dst++;
260     }
261 }
262
263 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     uint32_t v = AV_RL24(dst - 3);
272     uint32_t a = v       | v << 24;
273     uint32_t b = v >> 8  | v << 16;
274     uint32_t c = v >> 16 | v << 8;
275 #endif
276
277     while (len >= 12) {
278         AV_WN32(dst,     a);
279         AV_WN32(dst + 4, b);
280         AV_WN32(dst + 8, c);
281         dst += 12;
282         len -= 12;
283     }
284
285     if (len >= 4) {
286         AV_WN32(dst, a);
287         dst += 4;
288         len -= 4;
289     }
290
291     if (len >= 4) {
292         AV_WN32(dst, b);
293         dst += 4;
294         len -= 4;
295     }
296
297     while (len--) {
298         *dst = dst[-3];
299         dst++;
300     }
301 }
302
303 static void fill32(uint8_t *dst, int len)
304 {
305     uint32_t v = AV_RN32(dst - 4);
306
307     while (len >= 4) {
308         AV_WN32(dst, v);
309         dst += 4;
310         len -= 4;
311     }
312
313     while (len--) {
314         *dst = dst[-4];
315         dst++;
316     }
317 }
318
319 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
320 {
321     const uint8_t *src = &dst[-back];
322     if (!back)
323         return;
324
325     if (back == 1) {
326         memset(dst, *src, cnt);
327     } else if (back == 2) {
328         fill16(dst, cnt);
329     } else if (back == 3) {
330         fill24(dst, cnt);
331     } else if (back == 4) {
332         fill32(dst, cnt);
333     } else {
334         if (cnt >= 16) {
335             int blocklen = back;
336             while (cnt > blocklen) {
337                 memcpy(dst, src, blocklen);
338                 dst       += blocklen;
339                 cnt       -= blocklen;
340                 blocklen <<= 1;
341             }
342             memcpy(dst, src, cnt);
343             return;
344         }
345         if (cnt >= 8) {
346             AV_COPY32U(dst,     src);
347             AV_COPY32U(dst + 4, src + 4);
348             src += 8;
349             dst += 8;
350             cnt -= 8;
351         }
352         if (cnt >= 4) {
353             AV_COPY32U(dst, src);
354             src += 4;
355             dst += 4;
356             cnt -= 4;
357         }
358         if (cnt >= 2) {
359             AV_COPY16U(dst, src);
360             src += 2;
361             dst += 2;
362             cnt -= 2;
363         }
364         if (cnt)
365             *dst = *src;
366     }
367 }
368
369 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
370 {
371     if (min_size < *size)
372         return ptr;
373
374     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
375
376     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     if (!ptr)
381         min_size = 0;
382
383     *size = min_size;
384
385     return ptr;
386 }
387
388 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
389 {
390     void **p = ptr;
391     if (min_size < *size)
392         return;
393     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
394     av_free(*p);
395     *p = av_malloc(min_size);
396     if (!*p)
397         min_size = 0;
398     *size = min_size;
399 }