Libav
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 "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 static void fill16(uint8_t *dst, int len)
226 {
227  uint32_t v = AV_RN16(dst - 2);
228 
229  v |= v << 16;
230 
231  while (len >= 4) {
232  AV_WN32(dst, v);
233  dst += 4;
234  len -= 4;
235  }
236 
237  while (len--) {
238  *dst = dst[-2];
239  dst++;
240  }
241 }
242 
243 static void fill24(uint8_t *dst, int len)
244 {
245 #if HAVE_BIGENDIAN
246  uint32_t v = AV_RB24(dst - 3);
247  uint32_t a = v << 8 | v >> 16;
248  uint32_t b = v << 16 | v >> 8;
249  uint32_t c = v << 24 | v;
250 #else
251  uint32_t v = AV_RL24(dst - 3);
252  uint32_t a = v | v << 24;
253  uint32_t b = v >> 8 | v << 16;
254  uint32_t c = v >> 16 | v << 8;
255 #endif
256 
257  while (len >= 12) {
258  AV_WN32(dst, a);
259  AV_WN32(dst + 4, b);
260  AV_WN32(dst + 8, c);
261  dst += 12;
262  len -= 12;
263  }
264 
265  if (len >= 4) {
266  AV_WN32(dst, a);
267  dst += 4;
268  len -= 4;
269  }
270 
271  if (len >= 4) {
272  AV_WN32(dst, b);
273  dst += 4;
274  len -= 4;
275  }
276 
277  while (len--) {
278  *dst = dst[-3];
279  dst++;
280  }
281 }
282 
283 static void fill32(uint8_t *dst, int len)
284 {
285  uint32_t v = AV_RN32(dst - 4);
286 
287  while (len >= 4) {
288  AV_WN32(dst, v);
289  dst += 4;
290  len -= 4;
291  }
292 
293  while (len--) {
294  *dst = dst[-4];
295  dst++;
296  }
297 }
298 
299 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
300 {
301  const uint8_t *src = &dst[-back];
302  if (!back)
303  return;
304 
305  if (back == 1) {
306  memset(dst, *src, cnt);
307  } else if (back == 2) {
308  fill16(dst, cnt);
309  } else if (back == 3) {
310  fill24(dst, cnt);
311  } else if (back == 4) {
312  fill32(dst, cnt);
313  } else {
314  if (cnt >= 16) {
315  int blocklen = back;
316  while (cnt > blocklen) {
317  memcpy(dst, src, blocklen);
318  dst += blocklen;
319  cnt -= blocklen;
320  blocklen <<= 1;
321  }
322  memcpy(dst, src, cnt);
323  return;
324  }
325  if (cnt >= 8) {
326  AV_COPY32U(dst, src);
327  AV_COPY32U(dst + 4, src + 4);
328  src += 8;
329  dst += 8;
330  cnt -= 8;
331  }
332  if (cnt >= 4) {
333  AV_COPY32U(dst, src);
334  src += 4;
335  dst += 4;
336  cnt -= 4;
337  }
338  if (cnt >= 2) {
339  AV_COPY16U(dst, src);
340  src += 2;
341  dst += 2;
342  cnt -= 2;
343  }
344  if (cnt)
345  *dst = *src;
346  }
347 }
348 
349 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
350 {
351  if (min_size < *size)
352  return ptr;
353 
354  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
355 
356  ptr = av_realloc(ptr, min_size);
357  /* we could set this to the unmodified min_size but this is safer
358  * if the user lost the ptr and uses NULL now
359  */
360  if (!ptr)
361  min_size = 0;
362 
363  *size = min_size;
364 
365  return ptr;
366 }
367 
368 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
369 {
370  void **p = ptr;
371  if (min_size < *size)
372  return;
373  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
374  av_free(*p);
375  *p = av_malloc(min_size);
376  if (!*p)
377  min_size = 0;
378  *size = min_size;
379 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int size
memory handling functions
#define AV_RB24
Definition: intreadwrite.h:64
external API header
static void fill16(uint8_t *dst, int len)
Definition: mem.c:225
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
uint8_t
static void fill32(uint8_t *dst, int len)
Definition: mem.c:283
#define b
Definition: input.c:52
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:349
#define AV_COPY16U(d, s)
Definition: intreadwrite.h:473
#define AVERROR(e)
Definition: error.h:43
#define FFMAX(a, b)
Definition: common.h:55
#define AV_COPY32U(d, s)
Definition: intreadwrite.h:477
NULL
Definition: eval.c:55
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
#define AV_RN16(p)
Definition: intreadwrite.h:322
#define AV_RN32(p)
Definition: intreadwrite.h:326
#define AV_RL24
Definition: intreadwrite.h:78
static void fill24(uint8_t *dst, int len)
Definition: mem.c:243
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:368
common internal and external API header
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
#define AV_WN32(p, v)
Definition: intreadwrite.h:338
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:160
int len
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:299
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205