/* * This file was stolen from ffmpeg and modified for usability here. * The original is * * Copyright (c) 2002 Fabrice Bellard * * This version is a derivative work of that version. It is not free * software; it is licensed under the GNU Lesser General Public * License version 2.1, which places nontrivial restrictions on what * may be done with it. (I'm not terribly happy about that, but for * my purposes accepting LGPL-infected code is a lower price than * reimplementing it all myself.) * * The LGPL v2.1 as distributed with ffmpeg is in the accompanying file * `LGPL-v2.1'. */ #define _XOPEN_SOURCE 600 #include #include #include #include #include #include "config.h" #include "avutil-mem.h" /* here we can use OS-dependent allocation functions */ #undef free #undef malloc #undef realloc #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 */ #define ALIGN (HAVE_AVX ? 32 : 16) /* You can redefine av_malloc and av_free in your project to use your memory allocator. You do not need to suppress this file because the linker will do it automatically. */ static size_t max_alloc_size= INT_MAX; void av_max_alloc(size_t max){ max_alloc_size = max; } void *av_malloc(size_t size) { void *ptr = 0; /* let's disallow possible ambiguous cases */ if (size > (max_alloc_size-32)) return 0; ptr = malloc(size); if(!ptr && !size) ptr= av_malloc(1); return ptr; } void *av_realloc(void *ptr, size_t size) { /* let's disallow possible ambiguous cases */ if (size > (max_alloc_size-32)) return 0; return realloc(ptr, size + !size); } 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); if (!r && size) av_free(ptr); return r; } void av_free(void *ptr) { free(ptr); } void av_freep(void *arg) { void **ptr= (void**)arg; av_free(*ptr); *ptr = NULL; } void *av_mallocz(size_t size) { void *ptr = av_malloc(size); if (ptr) memset(ptr, 0, size); return ptr; } void *av_calloc(size_t nmemb, size_t size) { if (size <= 0 || nmemb >= INT_MAX / size) return NULL; return av_mallocz(nmemb * size); } char *av_strdup(const char *s) { char *ptr= NULL; if(s){ int len = strlen(s) + 1; ptr = av_malloc(len); if (ptr) memcpy(ptr, s, len); } return ptr; } /* add one element to a dynamic array */ void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem) { /* see similar ffmpeg.c:grow_array() */ int nb, nb_alloc; intptr_t *tab; nb = *nb_ptr; tab = *(intptr_t**)tab_ptr; if ((nb & (nb - 1)) == 0) { if (nb == 0) nb_alloc = 1; else nb_alloc = nb * 2; tab = av_realloc(tab, nb_alloc * sizeof(intptr_t)); *(intptr_t**)tab_ptr = tab; } tab[nb++] = (intptr_t)elem; *nb_ptr = nb; }