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