00001 /* 00002 * default memory allocator for libavcodec 00003 * Copyright (c) 2002 Fabrice Bellard. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 00025 #include "avcodec.h" 00026 00027 /* here we can use OS dependant allocation functions */ 00028 #undef malloc 00029 #undef free 00030 #undef realloc 00031 00032 #ifdef HAVE_MALLOC_H 00033 #include <malloc.h> 00034 #endif 00035 00036 /* you can redefine av_malloc and av_free in your project to use your 00037 memory allocator. You do not need to suppress this file because the 00038 linker will do it automatically */ 00039 00045 void *av_malloc(unsigned int size) 00046 { 00047 void *ptr; 00048 #ifdef MEMALIGN_HACK 00049 int diff; 00050 #endif 00051 00052 /* lets disallow possible ambiguous cases */ 00053 if(size > INT_MAX) 00054 return NULL; 00055 00056 #ifdef MEMALIGN_HACK 00057 ptr = malloc(size+16+1); 00058 diff= ((-(int)ptr - 1)&15) + 1; 00059 ptr += diff; 00060 ((char*)ptr)[-1]= diff; 00061 #elif defined (HAVE_MEMALIGN) 00062 ptr = memalign(16,size); 00063 /* Why 64? 00064 Indeed, we should align it: 00065 on 4 for 386 00066 on 16 for 486 00067 on 32 for 586, PPro - k6-III 00068 on 64 for K7 (maybe for P3 too). 00069 Because L1 and L2 caches are aligned on those values. 00070 But I don't want to code such logic here! 00071 */ 00072 /* Why 16? 00073 because some cpus need alignment, for example SSE2 on P4, & most RISC cpus 00074 it will just trigger an exception and the unaligned load will be done in the 00075 exception handler or it will just segfault (SSE2 on P4) 00076 Why not larger? because i didnt see a difference in benchmarks ... 00077 */ 00078 /* benchmarks with p3 00079 memalign(64)+1 3071,3051,3032 00080 memalign(64)+2 3051,3032,3041 00081 memalign(64)+4 2911,2896,2915 00082 memalign(64)+8 2545,2554,2550 00083 memalign(64)+16 2543,2572,2563 00084 memalign(64)+32 2546,2545,2571 00085 memalign(64)+64 2570,2533,2558 00086 00087 btw, malloc seems to do 8 byte alignment by default here 00088 */ 00089 #else 00090 ptr = malloc(size); 00091 #endif 00092 return ptr; 00093 } 00094 00100 void *av_realloc(void *ptr, unsigned int size) 00101 { 00102 #ifdef MEMALIGN_HACK 00103 int diff; 00104 #endif 00105 00106 /* lets disallow possible ambiguous cases */ 00107 if(size > INT_MAX) 00108 return NULL; 00109 00110 #ifdef MEMALIGN_HACK 00111 //FIXME this isnt aligned correctly though it probably isnt needed 00112 if(!ptr) return av_malloc(size); 00113 diff= ((char*)ptr)[-1]; 00114 return realloc(ptr - diff, size + diff) + diff; 00115 #else 00116 return realloc(ptr, size); 00117 #endif 00118 } 00119 00120 /* NOTE: ptr = NULL is explicetly allowed */ 00121 void av_free(void *ptr) 00122 { 00123 /* XXX: this test should not be needed on most libcs */ 00124 if (ptr) 00125 #ifdef MEMALIGN_HACK 00126 free(ptr - ((char*)ptr)[-1]); 00127 #else 00128 free(ptr); 00129 #endif 00130 } 00131
1.5.5