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 *val;
143 
144  if (!size) {
145  av_freep(ptr);
146  return 0;
147  }
148 
149  memcpy(&val, ptr, sizeof(val));
150  val = av_realloc(val, size);
151 
152  if (!val) {
153  av_freep(ptr);
154  return AVERROR(ENOMEM);
155  }
156 
157  memcpy(ptr, &val, sizeof(val));
158  return 0;
159 }
160 
161 void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
162 {
163  if (!size || nmemb >= INT_MAX / size)
164  return NULL;
165  return av_realloc(ptr, nmemb * size);
166 }
167 
168 int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
169 {
170  void *val;
171 
172  if (!size || nmemb >= INT_MAX / size)
173  return AVERROR(ENOMEM);
174  if (!nmemb) {
175  av_freep(ptr);
176  return 0;
177  }
178 
179  memcpy(&val, ptr, sizeof(val));
180  val = av_realloc(val, nmemb * size);
181  if (!val) {
182  av_freep(ptr);
183  return AVERROR(ENOMEM);
184  }
185 
186  memcpy(ptr, &val, sizeof(val));
187  return 0;
188 }
189 
190 void av_free(void *ptr)
191 {
192 #if CONFIG_MEMALIGN_HACK
193  if (ptr)
194  free((char *)ptr - ((char *)ptr)[-1]);
195 #elif HAVE_ALIGNED_MALLOC
196  _aligned_free(ptr);
197 #else
198  free(ptr);
199 #endif
200 }
201 
202 void av_freep(void *arg)
203 {
204  void *val;
205 
206  memcpy(&val, arg, sizeof(val));
207  memcpy(arg, &(void *){ NULL }, sizeof(val));
208  av_free(val);
209 }
210 
211 void *av_mallocz(size_t size)
212 {
213  void *ptr = av_malloc(size);
214  if (ptr)
215  memset(ptr, 0, size);
216  return ptr;
217 }
218 
219 char *av_strdup(const char *s)
220 {
221  char *ptr = NULL;
222  if (s) {
223  int len = strlen(s) + 1;
224  ptr = av_realloc(NULL, len);
225  if (ptr)
226  memcpy(ptr, s, len);
227  }
228  return ptr;
229 }
230 
231 char *av_strndup(const char *s, size_t len)
232 {
233  char *ret = NULL, *end;
234 
235  if (!s)
236  return NULL;
237 
238  end = memchr(s, 0, len);
239  if (end)
240  len = end - s;
241 
242  ret = av_realloc(NULL, len + 1);
243  if (!ret)
244  return NULL;
245 
246  memcpy(ret, s, len);
247  ret[len] = 0;
248  return ret;
249 }
250 
251 static void fill16(uint8_t *dst, int len)
252 {
253  uint32_t v = AV_RN16(dst - 2);
254 
255  v |= v << 16;
256 
257  while (len >= 4) {
258  AV_WN32(dst, v);
259  dst += 4;
260  len -= 4;
261  }
262 
263  while (len--) {
264  *dst = dst[-2];
265  dst++;
266  }
267 }
268 
269 static void fill24(uint8_t *dst, int len)
270 {
271 #if HAVE_BIGENDIAN
272  uint32_t v = AV_RB24(dst - 3);
273  uint32_t a = v << 8 | v >> 16;
274  uint32_t b = v << 16 | v >> 8;
275  uint32_t c = v << 24 | v;
276 #else
277  uint32_t v = AV_RL24(dst - 3);
278  uint32_t a = v | v << 24;
279  uint32_t b = v >> 8 | v << 16;
280  uint32_t c = v >> 16 | v << 8;
281 #endif
282 
283  while (len >= 12) {
284  AV_WN32(dst, a);
285  AV_WN32(dst + 4, b);
286  AV_WN32(dst + 8, c);
287  dst += 12;
288  len -= 12;
289  }
290 
291  if (len >= 4) {
292  AV_WN32(dst, a);
293  dst += 4;
294  len -= 4;
295  }
296 
297  if (len >= 4) {
298  AV_WN32(dst, b);
299  dst += 4;
300  len -= 4;
301  }
302 
303  while (len--) {
304  *dst = dst[-3];
305  dst++;
306  }
307 }
308 
309 static void fill32(uint8_t *dst, int len)
310 {
311  uint32_t v = AV_RN32(dst - 4);
312 
313  while (len >= 4) {
314  AV_WN32(dst, v);
315  dst += 4;
316  len -= 4;
317  }
318 
319  while (len--) {
320  *dst = dst[-4];
321  dst++;
322  }
323 }
324 
325 void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
326 {
327  const uint8_t *src = &dst[-back];
328  if (!back)
329  return;
330 
331  if (back == 1) {
332  memset(dst, *src, cnt);
333  } else if (back == 2) {
334  fill16(dst, cnt);
335  } else if (back == 3) {
336  fill24(dst, cnt);
337  } else if (back == 4) {
338  fill32(dst, cnt);
339  } else {
340  if (cnt >= 16) {
341  int blocklen = back;
342  while (cnt > blocklen) {
343  memcpy(dst, src, blocklen);
344  dst += blocklen;
345  cnt -= blocklen;
346  blocklen <<= 1;
347  }
348  memcpy(dst, src, cnt);
349  return;
350  }
351  if (cnt >= 8) {
352  AV_COPY32U(dst, src);
353  AV_COPY32U(dst + 4, src + 4);
354  src += 8;
355  dst += 8;
356  cnt -= 8;
357  }
358  if (cnt >= 4) {
359  AV_COPY32U(dst, src);
360  src += 4;
361  dst += 4;
362  cnt -= 4;
363  }
364  if (cnt >= 2) {
365  AV_COPY16U(dst, src);
366  src += 2;
367  dst += 2;
368  cnt -= 2;
369  }
370  if (cnt)
371  *dst = *src;
372  }
373 }
374 
375 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
376 {
377  if (min_size < *size)
378  return ptr;
379 
380  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
381 
382  ptr = av_realloc(ptr, min_size);
383  /* we could set this to the unmodified min_size but this is safer
384  * if the user lost the ptr and uses NULL now
385  */
386  if (!ptr)
387  min_size = 0;
388 
389  *size = min_size;
390 
391  return ptr;
392 }
393 
394 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
395 {
396  void **p = ptr;
397  if (min_size < *size)
398  return;
399  min_size = FFMAX(17 * min_size / 16 + 32, min_size);
400  av_free(*p);
401  *p = av_malloc(min_size);
402  if (!*p)
403  min_size = 0;
404  *size = min_size;
405 }
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:251
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:202
uint8_t
static void fill32(uint8_t *dst, int len)
Definition: mem.c:309
#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
char * av_strndup(const char *s, size_t len)
Duplicate a substring of the string s.
Definition: mem.c:231
#define src
Definition: vp8dsp.c:254
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:168
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
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:375
#define AV_COPY16U(d, s)
Definition: intreadwrite.h:484
#define AVERROR(e)
Definition: error.h:43
#define FFMAX(a, b)
Definition: common.h:64
#define AV_COPY32U(d, s)
Definition: intreadwrite.h:488
NULL
Definition: eval.c:55
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
#define AV_RN16(p)
Definition: intreadwrite.h:333
#define AV_RN32(p)
Definition: intreadwrite.h:337
#define AV_RL24
Definition: intreadwrite.h:78
static void fill24(uint8_t *dst, int len)
Definition: mem.c:269
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:394
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:349
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:161
int len
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:325
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:211