Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/fast_alloc.c

Go to the documentation of this file.
00001 #ifndef __J2K__FAST_ALLOC_C__
00002 #define __J2K__FAST_ALLOC_C__   1
00003 
00004 #include "fast_alloc.h"
00005 
00006 /*
00007   The fast and basic allocation primitive:
00008   ========================================
00009   Always round request to something close to a power of two.
00010   This ensures a bit of padding, which often means that
00011   buffer increase won't have to realloc. Plus it tends to
00012   be faster when lots of objects are created and discarded,
00013   since just about any version of malloc will be faster 
00014   when it can reuse identically-sized chunks.
00015 
00016   Basically, we use internal memory fragmentation 
00017   for our own space craving needs, which is a very good thing to do!
00018   Why bothering realloc() when space is already there for us !?
00019 
00020   We use register and shifting for optimal performance.
00021 */
00022 
00023 /* Calculate the amount of memory needed ideally */
00024 size_t fast_mem_size( register size_t min_size )
00025 {
00026   register size_t  alloc_size = 1;
00027   register size_t  curr_size  = min_size;
00028 
00029   curr_size += MALLOC_MIN_OVERHEAD;
00030 
00031   if ( curr_size >= MALLOC_MAX_THRESHOLD ) 
00032   {
00033     curr_size >>= MALLOC_MAX_THRESHOLD_BIT;
00034     curr_size++;
00035     curr_size <<= MALLOC_MAX_THRESHOLD_BIT;
00036     alloc_size = curr_size;
00037   }
00038   else /* Too small, so exponential growth */
00039   {
00040     while( curr_size > 0 ) 
00041     {
00042       curr_size  >>= 1;  /* Get the highest power of two + 1 bit */
00043       alloc_size <<= 1;
00044     }
00045   }
00046 
00047 #ifdef __J2K__DEBUG_TRACE
00048   printf( "Wanted space:    [%d]\n", min_size   );
00049   printf( "Allocated space: [%d]\n", alloc_size );
00050   printf( "malloc'ed space: [%d]\n", alloc_size - MALLOC_MIN_OVERHEAD );
00051   fflush( stdout );
00052 #endif
00053 
00054   alloc_size -= MALLOC_MIN_OVERHEAD;
00055   
00056   /* Make sure what we allocate is big enough */
00057   assert( alloc_size >= curr_size );  
00058 
00059   return alloc_size;
00060 }
00061 
00062 /* Malloc using the ideal amount of memory */
00063 void* fast_malloc( register size_t min_size )
00064 {
00065   if ( min_size < 16 ) min_size = 16;
00066 
00067   void* buffer = malloc( fast_mem_size( min_size ) );
00068 
00069   /* Make sure we have some memory allocated ! */
00070   assert( buffer != NULL );
00071 
00072   return buffer;
00073 }
00074 
00075 /* Realloc using the ideal amount of memory for the extra bytes */
00076 void* fast_realloc( register void* buffer, register size_t buf_size, register size_t extra )
00077 {
00078   if ( extra < 16 ) extra = 16;
00079 
00080   register size_t sz = fast_mem_size( extra ) + buf_size;
00081   void* buffer = realloc( buffer, sz );
00082 
00083   /* Make sure we have some memory allocated ! */
00084   assert( buffer != NULL );
00085 
00086   return buffer;
00087 }
00088 
00089 /* Same as free, provide for convenience */
00090 void fast_free( void* buffer )
00091 {
00092   free( buffer );
00093 }
00094 
00095 #endif

Generated on Sun Oct 14 18:46:09 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001