annotate libavutil/mem.c @ 4:96e628866d41

naming some tasks to help debugging
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Wed, 19 Dec 2012 15:40:26 +0100
parents
children
rev   line source
nengel@2 1 /*
nengel@2 2 * default memory allocator for libavutil
nengel@2 3 * Copyright (c) 2002 Fabrice Bellard
nengel@2 4 *
nengel@2 5 * This file is part of FFmpeg.
nengel@2 6 *
nengel@2 7 * FFmpeg is free software; you can redistribute it and/or
nengel@2 8 * modify it under the terms of the GNU Lesser General Public
nengel@2 9 * License as published by the Free Software Foundation; either
nengel@2 10 * version 2.1 of the License, or (at your option) any later version.
nengel@2 11 *
nengel@2 12 * FFmpeg is distributed in the hope that it will be useful,
nengel@2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nengel@2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
nengel@2 15 * Lesser General Public License for more details.
nengel@2 16 *
nengel@2 17 * You should have received a copy of the GNU Lesser General Public
nengel@2 18 * License along with FFmpeg; if not, write to the Free Software
nengel@2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
nengel@2 20 */
nengel@2 21
nengel@2 22 /**
nengel@2 23 * @file
nengel@2 24 * default memory allocator for libavutil
nengel@2 25 */
nengel@2 26
nengel@2 27 #include "config.h"
nengel@2 28
nengel@2 29 #include <limits.h>
nengel@2 30 #include <stdlib.h>
nengel@2 31 #include <stdint.h>
nengel@2 32 #include <string.h>
nengel@2 33 #if HAVE_MALLOC_H
nengel@2 34 #include <malloc.h>
nengel@2 35 #endif
nengel@2 36
nengel@2 37 #include "mem.h"
nengel@2 38
nengel@2 39 /* here we can use OS-dependent allocation functions */
nengel@2 40 #undef free
nengel@2 41 #undef malloc
nengel@2 42 #undef realloc
nengel@2 43
nengel@2 44 #ifdef MALLOC_PREFIX
nengel@2 45
nengel@2 46 #define malloc AV_JOIN(MALLOC_PREFIX, malloc)
nengel@2 47 #define memalign AV_JOIN(MALLOC_PREFIX, memalign)
nengel@2 48 #define posix_memalign AV_JOIN(MALLOC_PREFIX, posix_memalign)
nengel@2 49 #define realloc AV_JOIN(MALLOC_PREFIX, realloc)
nengel@2 50 #define free AV_JOIN(MALLOC_PREFIX, free)
nengel@2 51
nengel@2 52 void *malloc(size_t size);
nengel@2 53 void *memalign(size_t align, size_t size);
nengel@2 54 int posix_memalign(void **ptr, size_t align, size_t size);
nengel@2 55 void *realloc(void *ptr, size_t size);
nengel@2 56 void free(void *ptr);
nengel@2 57
nengel@2 58 #endif /* MALLOC_PREFIX */
nengel@2 59
nengel@2 60
nengel@2 61 /* You can redefine av_malloc and av_free in your project to use your
nengel@2 62 memory allocator. You do not need to suppress this file because the
nengel@2 63 linker will do it automatically. */
nengel@2 64
nengel@2 65 void *av_malloc(unsigned int size)
nengel@2 66 {
nengel@2 67 void *ptr = NULL;
nengel@2 68 /* let's disallow possible ambiguous cases */
nengel@2 69 if(size > (INT_MAX-16) )
nengel@2 70 return NULL;
nengel@2 71
nengel@2 72 //FIXME: when no aligned mallocs vector code should be disabled.
nengel@2 73 #if HAVE_POSIX_MEMALIGN
nengel@2 74 if (posix_memalign(&ptr,16,size))
nengel@2 75 ptr = NULL;
nengel@2 76 #elif HAVE_MEMALIGN
nengel@2 77 ptr = memalign(16,size);
nengel@2 78 #else
nengel@2 79 ptr = malloc(size);
nengel@2 80 #endif
nengel@2 81 return ptr;
nengel@2 82 }
nengel@2 83
nengel@2 84 void *av_realloc(void *ptr, unsigned int size)
nengel@2 85 {
nengel@2 86 /* let's disallow possible ambiguous cases */
nengel@2 87 if(size > (INT_MAX-16) )
nengel@2 88 return NULL;
nengel@2 89
nengel@2 90 return realloc(ptr, size);
nengel@2 91
nengel@2 92 }
nengel@2 93
nengel@2 94 void av_free(void *ptr)
nengel@2 95 {
nengel@2 96 /* XXX: this test should not be needed on most libcs */
nengel@2 97 if (ptr)
nengel@2 98 free(ptr);
nengel@2 99
nengel@2 100 }
nengel@2 101
nengel@2 102 void av_freep(void *arg)
nengel@2 103 {
nengel@2 104 void **ptr= (void**)arg;
nengel@2 105 av_free(*ptr);
nengel@2 106 *ptr = NULL;
nengel@2 107 }
nengel@2 108
nengel@2 109 void *av_mallocz(unsigned int size)
nengel@2 110 {
nengel@2 111 void *ptr = av_malloc(size);
nengel@2 112 if (ptr)
nengel@2 113 memset(ptr, 0, size);
nengel@2 114 return ptr;
nengel@2 115 }
nengel@2 116
nengel@2 117 char *av_strdup(const char *s)
nengel@2 118 {
nengel@2 119 char *ptr= NULL;
nengel@2 120 if(s){
nengel@2 121 int len = strlen(s) + 1;
nengel@2 122 ptr = av_malloc(len);
nengel@2 123 if (ptr)
nengel@2 124 memcpy(ptr, s, len);
nengel@2 125 }
nengel@2 126 return ptr;
nengel@2 127 }