mem.c
Go to the documentation of this file.
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 
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 "intreadwrite.h"
39 #include "mem.h"
40 
41 #ifdef MALLOC_PREFIX
42 
43 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
44 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
45 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
46 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
47 #define free AV_JOIN(MALLOC_PREFIX, free)
48 
49 void *malloc(size_t size);
50 void *memalign(size_t align, size_t size);
51 int posix_memalign(void **ptr, size_t align, size_t size);
52 void *realloc(void *ptr, size_t size);
53 void free(void *ptr);
54 
55 #endif /* MALLOC_PREFIX */
56 
57 /* You can redefine av_malloc and av_free in your project to use your
58  * memory allocator. You do not need to suppress this file because the
59  * linker will do it automatically. */
60 
61 void *av_malloc(size_t size)
62 {
63  void *ptr = NULL;
64 #if CONFIG_MEMALIGN_HACK
65  long diff;
66 #endif
67 
68  /* let's disallow possible ambiguous cases */
69  if (size > (INT_MAX - 32) || !size)
70  return NULL;
71 
72 #if CONFIG_MEMALIGN_HACK
73  ptr = malloc(size + 32);
74  if (!ptr)
75  return ptr;
76  diff = ((-(long)ptr - 1) & 31) + 1;
77  ptr = (char *)ptr + diff;
78  ((char *)ptr)[-1] = diff;
79 #elif HAVE_POSIX_MEMALIGN
80  if (posix_memalign(&ptr, 32, size))
81  ptr = NULL;
82 #elif HAVE_ALIGNED_MALLOC
83  ptr = _aligned_malloc(size, 32);
84 #elif HAVE_MEMALIGN
85  ptr = memalign(32, size);
86  /* Why 64?
87  * Indeed, we should align it:
88  * on 4 for 386
89  * on 16 for 486
90  * on 32 for 586, PPro - K6-III
91  * on 64 for K7 (maybe for P3 too).
92  * Because L1 and L2 caches are aligned on those values.
93  * But I don't want to code such logic here!
94  */
95  /* Why 32?
96  * For AVX ASM. SSE / NEON needs only 16.
97  * Why not larger? Because I did not see a difference in benchmarks ...
98  */
99  /* benchmarks with P3
100  * memalign(64) + 1 3071, 3051, 3032
101  * memalign(64) + 2 3051, 3032, 3041
102  * memalign(64) + 4 2911, 2896, 2915
103  * memalign(64) + 8 2545, 2554, 2550
104  * memalign(64) + 16 2543, 2572, 2563
105  * memalign(64) + 32 2546, 2545, 2571
106  * memalign(64) + 64 2570, 2533, 2558
107  *
108  * BTW, malloc seems to do 8-byte alignment by default here.
109  */
110 #else
111  ptr = malloc(size);
112 #endif
113  return ptr;
114 }
115 
116 void *av_realloc(void *ptr, size_t size)
117 {
118 #if CONFIG_MEMALIGN_HACK
119  int diff;
120 #endif
121 
122  /* let's disallow possible ambiguous cases */
123  if (size > (INT_MAX - 16))
124  return NULL;
125 
126 #if CONFIG_MEMALIGN_HACK
127  //FIXME this isn't aligned correctly, though it probably isn't needed
128  if (!ptr)
129  return av_malloc(size);
130  diff = ((char *)ptr)[-1];
131  return (char *)realloc((char *)ptr - diff, size + diff) + diff;
132 #elif HAVE_ALIGNED_MALLOC
133  return _aligned_realloc(ptr, size, 32);
134 #else
135  return realloc(ptr, size);
136 #endif
137 }
138 
139 void av_free(void *ptr)
140 {
141 #if CONFIG_MEMALIGN_HACK
142  if (ptr)
143  free((char *)ptr - ((char *)ptr)[-1]);
144 #elif HAVE_ALIGNED_MALLOC
145  _aligned_free(ptr);
146 #else
147  free(ptr);
148 #endif
149 }
150 
151 void av_freep(void *arg)
152 {
153  void **ptr = (void **)arg;
154  av_free(*ptr);
155  *ptr = NULL;
156 }
157 
158 void *av_mallocz(size_t size)
159 {
160  void *ptr = av_malloc(size);
161  if (ptr)
162  memset(ptr, 0, size);
163  return ptr;
164 }
165 
166 char *av_strdup(const char *s)
167 {
168  char *ptr = NULL;
169  if (s) {
170  int len = strlen(s) + 1;
171  ptr = av_malloc(len);
172  if (ptr)
173  memcpy(ptr, s, len);
174  }
175  return ptr;
176 }
177 
178 static void fill16(uint8_t *dst, int len)
179 {
180  uint32_t v = AV_RN16(dst - 2);
181 
182  v |= v << 16;
183 
184  while (len >= 4) {
185  AV_WN32(dst, v);
186  dst += 4;
187  len -= 4;
188  }
189 
190  while (len--) {
191  *dst = dst[-2];
192  dst++;
193  }
194 }
195 
196 static void fill24(uint8_t *dst, int len)
197 {
198 #if HAVE_BIGENDIAN
199  uint32_t v = AV_RB24(dst - 3);
200  uint32_t a = v << 8 | v >> 16;
201  uint32_t b = v << 16 | v >> 8;
202  uint32_t c = v << 24 | v;
203 #else
204  uint32_t v = AV_RL24(dst - 3);
205  uint32_t a = v | v << 24;
206  uint32_t b = v >> 8 | v << 16;
207  uint32_t c = v >> 16 | v << 8;
208 #endif
209 
210  while (len >= 12) {
211  AV_WN32(dst, a);
212  AV_WN32(dst + 4, b);
213  AV_WN32(dst + 8, c);
214  dst += 12;
215  len -= 12;
216  }
217 
218  if (len >= 4) {
219  AV_WN32(dst, a);
220  dst += 4;
221  len -= 4;
222  }
223 
224  if (len >= 4) {
225  AV_WN32(dst, b);
226  dst += 4;
227  len -= 4;
228  }
229 
230  while (len--) {
231  *dst = dst[-3];
232  dst++;
233  }
234 }
235 
236 static void fill32(uint8_t *dst, int len)
237 {
238  uint32_t v = AV_RN32(dst - 4);
239 
240  while (len >= 4) {
241  AV_WN32(dst, v);
242  dst += 4;
243  len -= 4;
244  }
245 
246  while (len--) {
247  *dst = dst[-4];
248  dst++;
249  }
250 }
251 
252 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
253 {
254  const uint8_t *src = &dst[-back];
255  if (!back)
256  return;
257 
258  if (back == 1) {
259  memset(dst, *src, cnt);
260  } else if (back == 2) {
261  fill16(dst, cnt);
262  } else if (back == 3) {
263  fill24(dst, cnt);
264  } else if (back == 4) {
265  fill32(dst, cnt);
266  } else {
267  if (cnt >= 16) {
268  int blocklen = back;
269  while (cnt > blocklen) {
270  memcpy(dst, src, blocklen);
271  dst += blocklen;
272  cnt -= blocklen;
273  blocklen <<= 1;
274  }
275  memcpy(dst, src, cnt);
276  return;
277  }
278  if (cnt >= 8) {
279  AV_COPY32U(dst, src);
280  AV_COPY32U(dst + 4, src + 4);
281  src += 8;
282  dst += 8;
283  cnt -= 8;
284  }
285  if (cnt >= 4) {
286  AV_COPY32U(dst, src);
287  src += 4;
288  dst += 4;
289  cnt -= 4;
290  }
291  if (cnt >= 2) {
292  AV_COPY16U(dst, src);
293  src += 2;
294  dst += 2;
295  cnt -= 2;
296  }
297  if (cnt)
298  *dst = *src;
299  }
300 }