d01fe86d |
/* |
cea8f6f3 |
* default memory allocator for libavutil |
406792e7 |
* Copyright (c) 2002 Fabrice Bellard |
d01fe86d |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
d01fe86d |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
d01fe86d |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
d01fe86d |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
d01fe86d |
*/ |
115329f1 |
|
983e3246 |
/** |
ba87f080 |
* @file |
89c9ff50 |
* default memory allocator for libavutil |
983e3246 |
*/ |
115329f1 |
|
518cdff8 |
#define _XOPEN_SOURCE 600
|
dfcb6b56 |
#include "config.h" |
8e1e6f31 |
|
dfcb6b56 |
#include <limits.h> |
5bac2d0c |
#include <stdint.h> |
1f91cdce |
#include <stdlib.h> |
dfcb6b56 |
#include <string.h> |
b250f9c6 |
#if HAVE_MALLOC_H |
d01fe86d |
#include <malloc.h>
#endif
|
2c21d34e |
#include "avassert.h" |
4ae40685 |
#include "avutil.h" |
cce3e0a4 |
#include "common.h" |
b0dcf765 |
#include "dynarray.h" |
5bac2d0c |
#include "intreadwrite.h" |
77652a6a |
#include "mem.h"
|
4ae40685 |
#ifdef MALLOC_PREFIX
#define malloc AV_JOIN(MALLOC_PREFIX, malloc)
#define memalign AV_JOIN(MALLOC_PREFIX, memalign)
#define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
#define realloc AV_JOIN(MALLOC_PREFIX, realloc)
#define free AV_JOIN(MALLOC_PREFIX, free)
void *malloc(size_t size);
void *memalign(size_t align, size_t size);
int posix_memalign(void **ptr, size_t align, size_t size);
void *realloc(void *ptr, size_t size);
void free(void *ptr);
#endif /* MALLOC_PREFIX */
|
d6ff68ad |
#include "mem_internal.h"
|
08675bb3 |
#define ALIGN (HAVE_AVX ? 32 : 16)
|
1a65d50e |
/* NOTE: if you want to override these functions with your own
* implementations (not recommended) you have to link libav* as
* dynamic libraries and remove -Wl,-Bsymbolic from the linker flags.
* Note that this will cost performance. */ |
d01fe86d |
|
5a8e9942 |
static size_t max_alloc_size= INT_MAX;
void av_max_alloc(size_t max){
max_alloc_size = max;
} |
6b4c0be5 |
|
490a022d |
void *av_malloc(size_t size) |
d01fe86d |
{ |
1f91cdce |
void *ptr = NULL; |
b250f9c6 |
#if CONFIG_MEMALIGN_HACK |
ed96aeea |
long diff; |
88730be6 |
#endif |
0ecca7a4 |
|
b634b36f |
/* let's disallow possibly ambiguous cases */ |
6912e7a0 |
if (size > (max_alloc_size - 32)) |
0ecca7a4 |
return NULL; |
115329f1 |
|
b250f9c6 |
#if CONFIG_MEMALIGN_HACK |
6912e7a0 |
ptr = malloc(size + ALIGN); |
5532cf31 |
if (!ptr) |
a9493601 |
return ptr; |
58f30175 |
diff = ((~(long)ptr)&(ALIGN - 1)) + 1; |
5532cf31 |
ptr = (char *)ptr + diff;
((char *)ptr)[-1] = diff; |
b250f9c6 |
#elif HAVE_POSIX_MEMALIGN |
bed04e2b |
if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation |
6912e7a0 |
if (posix_memalign(&ptr, ALIGN, size)) |
a90de11d |
ptr = NULL; |
be1a839c |
#elif HAVE_ALIGNED_MALLOC |
cabbd271 |
ptr = _aligned_malloc(size, ALIGN); |
b250f9c6 |
#elif HAVE_MEMALIGN |
91b747ac |
#ifndef __DJGPP__ |
6912e7a0 |
ptr = memalign(ALIGN, size); |
91b747ac |
#else
ptr = memalign(size, ALIGN);
#endif |
115329f1 |
/* Why 64? |
5532cf31 |
* Indeed, we should align it:
* on 4 for 386
* on 16 for 486
* on 32 for 586, PPro - K6-III
* on 64 for K7 (maybe for P3 too).
* Because L1 and L2 caches are aligned on those values.
* But I don't want to code such logic here! |
d01fe86d |
*/ |
5532cf31 |
/* Why 32?
* For AVX ASM. SSE / NEON needs only 16.
* Why not larger? Because I did not see a difference in benchmarks ... |
8f2b21a8 |
*/ |
5532cf31 |
/* benchmarks with P3
* memalign(64) + 1 3071, 3051, 3032
* memalign(64) + 2 3051, 3032, 3041
* memalign(64) + 4 2911, 2896, 2915
* memalign(64) + 8 2545, 2554, 2550
* memalign(64) + 16 2543, 2572, 2563
* memalign(64) + 32 2546, 2545, 2571
* memalign(64) + 64 2570, 2533, 2558
*
* BTW, malloc seems to do 8-byte alignment by default here. |
8f2b21a8 |
*/ |
d01fe86d |
#else
ptr = malloc(size);
#endif |
7c84e7d3 |
if(!ptr && !size) {
size = 1; |
c8981edd |
ptr= av_malloc(1); |
7c84e7d3 |
}
#if CONFIG_MEMORY_POISONING
if (ptr) |
84be8069 |
memset(ptr, FF_MEMORY_POISON, size); |
7c84e7d3 |
#endif |
d01fe86d |
return ptr;
}
|
490a022d |
void *av_realloc(void *ptr, size_t size) |
8e1e6f31 |
{ |
b250f9c6 |
#if CONFIG_MEMALIGN_HACK |
0a7c36af |
int diff;
#endif |
88730be6 |
|
b634b36f |
/* let's disallow possibly ambiguous cases */ |
6912e7a0 |
if (size > (max_alloc_size - 32)) |
0ecca7a4 |
return NULL;
|
b250f9c6 |
#if CONFIG_MEMALIGN_HACK |
0a7c36af |
//FIXME this isn't aligned correctly, though it probably isn't needed |
5532cf31 |
if (!ptr)
return av_malloc(size);
diff = ((char *)ptr)[-1]; |
2c21d34e |
av_assert0(diff>0 && diff<=ALIGN); |
6912e7a0 |
ptr = realloc((char *)ptr - diff, size + diff);
if (ptr)
ptr = (char *)ptr + diff; |
fc119278 |
return ptr; |
be1a839c |
#elif HAVE_ALIGNED_MALLOC |
cabbd271 |
return _aligned_realloc(ptr, size + !size, ALIGN); |
0a7c36af |
#else |
91ff05f6 |
return realloc(ptr, size + !size); |
da9b170c |
#endif |
b7a22d84 |
}
|
5cd754bc |
void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)
{
size_t size;
void *r;
if (av_size_mult(elsize, nelem, &size)) {
av_free(ptr);
return NULL;
}
r = av_realloc(ptr, size); |
65b2feb8 |
if (!r) |
5cd754bc |
av_free(ptr);
return r;
}
|
3feb3d6c |
int av_reallocp(void *ptr, size_t size)
{ |
60392480 |
void *val; |
3feb3d6c |
|
67e285ce |
if (!size) {
av_freep(ptr);
return 0;
} |
3feb3d6c |
|
60392480 |
memcpy(&val, ptr, sizeof(val));
val = av_realloc(val, size);
if (!val) { |
3feb3d6c |
av_freep(ptr);
return AVERROR(ENOMEM);
}
|
60392480 |
memcpy(ptr, &val, sizeof(val)); |
3feb3d6c |
return 0;
}
|
fc962d4e |
void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
{ |
c3e6e8f0 |
if (!size || nmemb >= INT_MAX / size) |
fc962d4e |
return NULL;
return av_realloc(ptr, nmemb * size);
}
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
{ |
60392480 |
void *val;
memcpy(&val, ptr, sizeof(val)); |
fa73358c |
val = av_realloc_f(val, nmemb, size);
memcpy(ptr, &val, sizeof(val));
if (!val && nmemb && size) |
fc962d4e |
return AVERROR(ENOMEM); |
60392480 |
|
fc962d4e |
return 0;
}
|
d01fe86d |
void av_free(void *ptr)
{ |
b250f9c6 |
#if CONFIG_MEMALIGN_HACK |
2c21d34e |
if (ptr) {
int v= ((char *)ptr)[-1];
av_assert0(v>0 && v<=ALIGN);
free((char *)ptr - v);
} |
be1a839c |
#elif HAVE_ALIGNED_MALLOC
_aligned_free(ptr); |
da9b170c |
#else |
0c8eb72e |
free(ptr); |
da9b170c |
#endif |
d01fe86d |
}
|
79e47000 |
void av_freep(void *arg)
{ |
60392480 |
void *val;
memcpy(&val, arg, sizeof(val));
memcpy(arg, &(void *){ NULL }, sizeof(val));
av_free(val); |
79e47000 |
}
|
490a022d |
void *av_mallocz(size_t size) |
79e47000 |
{ |
11362767 |
void *ptr = av_malloc(size); |
79e47000 |
if (ptr)
memset(ptr, 0, size);
return ptr;
}
|
ccecab4a |
void *av_calloc(size_t nmemb, size_t size)
{
if (size <= 0 || nmemb >= INT_MAX / size)
return NULL;
return av_mallocz(nmemb * size);
}
|
79e47000 |
char *av_strdup(const char *s)
{ |
5532cf31 |
char *ptr = NULL;
if (s) { |
4950bd4e |
size_t len = strlen(s) + 1; |
d433e1ae |
ptr = av_realloc(NULL, len); |
19757f61 |
if (ptr)
memcpy(ptr, s, len); |
fdf35f26 |
} |
79e47000 |
return ptr;
}
|
8ddc3262 |
char *av_strndup(const char *s, size_t len)
{
char *ret = NULL, *end;
if (!s)
return NULL;
end = memchr(s, 0, len);
if (end)
len = end - s;
ret = av_realloc(NULL, len + 1);
if (!ret)
return NULL;
memcpy(ret, s, len);
ret[len] = 0;
return ret;
}
|
89b5039f |
void *av_memdup(const void *p, size_t size)
{
void *ptr = NULL;
if (p) {
ptr = av_malloc(size);
if (ptr)
memcpy(ptr, p, size);
}
return ptr;
}
|
cd50a44b |
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
{ |
63186ac5 |
void **tab;
memcpy(&tab, tab_ptr, sizeof(tab)); |
cd50a44b |
|
04da20e5 |
FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, { |
cd50a44b |
tab[*nb_ptr] = elem; |
63186ac5 |
memcpy(tab_ptr, &tab, sizeof(tab)); |
cd50a44b |
}, {
return AVERROR(ENOMEM);
});
return 0;
}
|
35ceaa73 |
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
{ |
63186ac5 |
void **tab;
memcpy(&tab, tab_ptr, sizeof(tab)); |
b0dcf765 |
|
04da20e5 |
FF_DYNARRAY_ADD(INT_MAX, sizeof(*tab), tab, *nb_ptr, { |
9ba11902 |
tab[*nb_ptr] = elem; |
63186ac5 |
memcpy(tab_ptr, &tab, sizeof(tab)); |
b0dcf765 |
}, {
*nb_ptr = 0;
av_freep(tab_ptr);
}); |
35ceaa73 |
} |
c065255b |
|
e3984166 |
void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
const uint8_t *elem_data)
{ |
b0dcf765 |
uint8_t *tab_elem_data = NULL;
|
04da20e5 |
FF_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, { |
b0dcf765 |
tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size;
if (elem_data)
memcpy(tab_elem_data, elem_data, elem_size);
else if (CONFIG_MEMORY_POISONING)
memset(tab_elem_data, FF_MEMORY_POISON, elem_size);
}, {
av_freep(tab_ptr);
*nb_ptr = 0;
}); |
e3984166 |
return tab_elem_data;
}
|
4a606c83 |
static void fill16(uint8_t *dst, int len)
{
uint32_t v = AV_RN16(dst - 2);
v |= v << 16;
while (len >= 4) {
AV_WN32(dst, v);
dst += 4;
len -= 4;
}
while (len--) {
*dst = dst[-2];
dst++;
}
}
static void fill24(uint8_t *dst, int len)
{
#if HAVE_BIGENDIAN
uint32_t v = AV_RB24(dst - 3);
uint32_t a = v << 8 | v >> 16;
uint32_t b = v << 16 | v >> 8;
uint32_t c = v << 24 | v;
#else
uint32_t v = AV_RL24(dst - 3);
uint32_t a = v | v << 24;
uint32_t b = v >> 8 | v << 16;
uint32_t c = v >> 16 | v << 8;
#endif
while (len >= 12) {
AV_WN32(dst, a);
AV_WN32(dst + 4, b);
AV_WN32(dst + 8, c);
dst += 12;
len -= 12;
}
if (len >= 4) {
AV_WN32(dst, a);
dst += 4;
len -= 4;
}
if (len >= 4) {
AV_WN32(dst, b);
dst += 4;
len -= 4;
}
while (len--) {
*dst = dst[-3];
dst++;
}
}
static void fill32(uint8_t *dst, int len)
{
uint32_t v = AV_RN32(dst - 4);
while (len >= 4) {
AV_WN32(dst, v);
dst += 4;
len -= 4;
}
while (len--) {
*dst = dst[-4];
dst++;
}
}
|
5bac2d0c |
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
{
const uint8_t *src = &dst[-back]; |
f935aca4 |
if (!back)
return;
|
5bac2d0c |
if (back == 1) {
memset(dst, *src, cnt); |
4a606c83 |
} else if (back == 2) {
fill16(dst, cnt);
} else if (back == 3) {
fill24(dst, cnt);
} else if (back == 4) {
fill32(dst, cnt); |
5bac2d0c |
} else { |
4a606c83 |
if (cnt >= 16) { |
5bac2d0c |
int blocklen = back;
while (cnt > blocklen) {
memcpy(dst, src, blocklen);
dst += blocklen;
cnt -= blocklen;
blocklen <<= 1;
}
memcpy(dst, src, cnt); |
4a606c83 |
return;
}
if (cnt >= 8) {
AV_COPY32U(dst, src);
AV_COPY32U(dst + 4, src + 4);
src += 8;
dst += 8;
cnt -= 8;
}
if (cnt >= 4) {
AV_COPY32U(dst, src);
src += 4;
dst += 4;
cnt -= 4;
}
if (cnt >= 2) {
AV_COPY16U(dst, src);
src += 2;
dst += 2;
cnt -= 2; |
5bac2d0c |
} |
4a606c83 |
if (cnt)
*dst = *src; |
5bac2d0c |
}
} |
aa604e8e |
|
cce3e0a4 |
void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
{
if (min_size < *size)
return ptr;
|
b3415e4c |
min_size = FFMAX(min_size + min_size / 16 + 32, min_size); |
cce3e0a4 |
ptr = av_realloc(ptr, min_size);
/* we could set this to the unmodified min_size but this is safer
* if the user lost the ptr and uses NULL now
*/
if (!ptr)
min_size = 0;
*size = min_size;
return ptr;
}
|
7c888ae7 |
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
{
ff_fast_malloc(ptr, size, min_size, 0);
}
|
fc91eeab |
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
{
ff_fast_malloc(ptr, size, min_size, 1);
} |