00001 #ifndef __J2K__FAST_ALLOC_H__ 00002 #define __J2K__FAST_ALLOC_H__ 1 00003 00004 #include <assert.h> 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 #include <string.h> 00008 #include <malloc.h> 00009 00010 /* 00011 The fast and basic allocation primitive: 00012 ======================================== 00013 Always round request to something close to a power of two. 00014 This ensures a bit of padding, which often means that 00015 buffer increase won't have to realloc. Plus it tends to 00016 be faster when lots of objects are created and discarded, 00017 since just about any version of malloc will be faster 00018 when it can reuse identically-sized chunks. 00019 00020 Basically, we use internal memory fragmentation 00021 for our own space craving needs, which is a very good thing to do! 00022 Why bothering realloc() when space is already there for us !? 00023 */ 00024 00025 /* 00026 Roughtly the length of the memory allocated, 00027 which is 4 bytes on 32-bit machines 00028 */ 00029 #ifndef MALLOC_MIN_OVERHEAD 00030 #define MALLOC_MIN_OVERHEAD sizeof( size_t ) 00031 #endif 00032 00033 /* 16 bit == sizeof( short ) */ 00034 #ifndef MALLOC_MAX_THRESHOLD 00035 #define MALLOC_MAX_THRESHOLD sizeof( short ) 00036 #define MALLOC_MAX_THRESHOLD_BIT 16 00037 #endif 00038 00039 /* Using register and shifting for optimal performance */ 00040 00041 /* Calculate the amount of memory needed ideally */ 00042 size_t fast_mem_size( register size_t min_size ); 00043 00044 /* Malloc using the ideal amount of memory */ 00045 void* fast_malloc( register size_t min_size ); 00046 00047 /* Realloc using the ideal amount of memory for the extra bytes */ 00048 void* fast_realloc( register void* buffer, register size_t buf_size, register size_t extra ); 00049 00050 /* Same as free, provide for convenience */ 00051 void fast_free( void* buffer ); 00052 00053 #endif