nengel@2: /* nengel@2: * default memory allocator for libavutil nengel@2: * Copyright (c) 2002 Fabrice Bellard nengel@2: * nengel@2: * This file is part of FFmpeg. nengel@2: * nengel@2: * FFmpeg is free software; you can redistribute it and/or nengel@2: * modify it under the terms of the GNU Lesser General Public nengel@2: * License as published by the Free Software Foundation; either nengel@2: * version 2.1 of the License, or (at your option) any later version. nengel@2: * nengel@2: * FFmpeg is distributed in the hope that it will be useful, nengel@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of nengel@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nengel@2: * Lesser General Public License for more details. nengel@2: * nengel@2: * You should have received a copy of the GNU Lesser General Public nengel@2: * License along with FFmpeg; if not, write to the Free Software nengel@2: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA nengel@2: */ nengel@2: nengel@2: /** nengel@2: * @file nengel@2: * default memory allocator for libavutil nengel@2: */ nengel@2: nengel@2: #include "config.h" nengel@2: nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #if HAVE_MALLOC_H nengel@2: #include nengel@2: #endif nengel@2: nengel@2: #include "mem.h" nengel@2: nengel@2: /* here we can use OS-dependent allocation functions */ nengel@2: #undef free nengel@2: #undef malloc nengel@2: #undef realloc nengel@2: nengel@2: #ifdef MALLOC_PREFIX nengel@2: nengel@2: #define malloc AV_JOIN(MALLOC_PREFIX, malloc) nengel@2: #define memalign AV_JOIN(MALLOC_PREFIX, memalign) nengel@2: #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign) nengel@2: #define realloc AV_JOIN(MALLOC_PREFIX, realloc) nengel@2: #define free AV_JOIN(MALLOC_PREFIX, free) nengel@2: nengel@2: void *malloc(size_t size); nengel@2: void *memalign(size_t align, size_t size); nengel@2: int posix_memalign(void **ptr, size_t align, size_t size); nengel@2: void *realloc(void *ptr, size_t size); nengel@2: void free(void *ptr); nengel@2: nengel@2: #endif /* MALLOC_PREFIX */ nengel@2: nengel@2: nengel@2: /* You can redefine av_malloc and av_free in your project to use your nengel@2: memory allocator. You do not need to suppress this file because the nengel@2: linker will do it automatically. */ nengel@2: nengel@2: void *av_malloc(unsigned int size) nengel@2: { nengel@2: void *ptr = NULL; nengel@2: /* let's disallow possible ambiguous cases */ nengel@2: if(size > (INT_MAX-16) ) nengel@2: return NULL; nengel@2: nengel@2: //FIXME: when no aligned mallocs vector code should be disabled. nengel@2: #if HAVE_POSIX_MEMALIGN nengel@2: if (posix_memalign(&ptr,16,size)) nengel@2: ptr = NULL; nengel@2: #elif HAVE_MEMALIGN nengel@2: ptr = memalign(16,size); nengel@2: #else nengel@2: ptr = malloc(size); nengel@2: #endif nengel@2: return ptr; nengel@2: } nengel@2: nengel@2: void *av_realloc(void *ptr, unsigned int size) nengel@2: { nengel@2: /* let's disallow possible ambiguous cases */ nengel@2: if(size > (INT_MAX-16) ) nengel@2: return NULL; nengel@2: nengel@2: return realloc(ptr, size); nengel@2: nengel@2: } nengel@2: nengel@2: void av_free(void *ptr) nengel@2: { nengel@2: /* XXX: this test should not be needed on most libcs */ nengel@2: if (ptr) nengel@2: free(ptr); nengel@2: nengel@2: } nengel@2: nengel@2: void av_freep(void *arg) nengel@2: { nengel@2: void **ptr= (void**)arg; nengel@2: av_free(*ptr); nengel@2: *ptr = NULL; nengel@2: } nengel@2: nengel@2: void *av_mallocz(unsigned int size) nengel@2: { nengel@2: void *ptr = av_malloc(size); nengel@2: if (ptr) nengel@2: memset(ptr, 0, size); nengel@2: return ptr; nengel@2: } nengel@2: nengel@2: char *av_strdup(const char *s) nengel@2: { nengel@2: char *ptr= NULL; nengel@2: if(s){ nengel@2: int len = strlen(s) + 1; nengel@2: ptr = av_malloc(len); nengel@2: if (ptr) nengel@2: memcpy(ptr, s, len); nengel@2: } nengel@2: return ptr; nengel@2: }