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

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

Go to the documentation of this file.
00001 #ifndef __J2K__FreeList_C__
00002 #define __J2K__FreeList_C__   1
00003 
00004 #include "FreeList.h"
00005 
00006 /*
00007    This function must be call in order to create a valid FreeList.
00008 
00009    GOTCHA:
00010    =======
00011      1. Don't hard code 4 bytes (32-bit address space), due to 16 and 64 bit machines !
00012      
00013      2. Doing a FreeList of char is equivalent to doing a FreeList of int or double !
00014 
00015      3. Notice that this way we save 4 bytes per FreeLink, so basically the FreeList 
00016         don't cost any space per item except the FreeList container itself 
00017         which is merely 8 bytes !
00018 */
00019 FreeList_t* FreeList_create( register size_t sz )
00020 {
00021   FreeList_t* fl = (FreeList_t*)malloc( sizeof( FreeList_t ) );
00022 
00023   /* Make sure the memory was allocated */
00024   assert( fl != NULL );
00025 
00026   /* Initialized the head, else purge and other won't work */
00027   fl->head = NULL;
00028 
00029   /* Make sure the allocate data can be convert into 'FreeLink_t* next' pointers */
00030   if ( sz >= sizeof( FreeLink_t* ) )
00031   {
00032     /* It's big enough */
00033     fl->size = sz;
00034   }
00035   else
00036   {
00037     /* Make it big enough */
00038     fl->size = sizeof( FreeLink_t* );
00039   }
00040 
00041 #ifdef __J2K__DEBUG_TRACE
00042   printf( "Needed space:    [%d]\n", sz  );
00043   printf( "Allocated space: [%d]\n", fl->size );
00044   printf( "FreeList  space: [%d]\n", sizeof( FreeList_t ) );
00045   fflush( stdout );
00046 #endif
00047 
00048   /* Here's your initialized empty FreeList */
00049   return fl;
00050 }
00051 
00052 /*
00053    Allocate the desired amount of memory,
00054    if no FreeList link available 
00055 */
00056 void* FreeList_calloc( FreeList_t* this )
00057 {
00058   /* The current head if ain't NULL 
00059       - OR -
00060      the new allocated memory to be used and returned 
00061   */
00062   register void* curr = NULL;
00063 
00064   /* Make sure the object pointer is valid */
00065   assert( this != NULL );
00066 
00067   if ( this->head != NULL )
00068   {
00069     /* Pop out of the FreeList stack a dead structure */
00070           curr = this->head;
00071     this->head = this->head->next;
00072   }
00073   else
00074   {
00075     /* No FreeList Link available, so do a real malloc */
00076     curr = malloc( this->size );  
00077 
00078     /* Make sure the memory was allocated */
00079     assert( curr != NULL );
00080   }
00081 
00082   /* Zero out the NEW allocated structure */
00083   memset( curr, 0, this->size );
00084 
00085   /* Here's your zero'ed out structure */
00086   return curr;
00087 }
00088 
00089 /* 
00090    Falsely delete the dead structure, 
00091    by stacking it up to the FreeList,
00092    for future usage.
00093 */
00094 void FreeList_free( FreeList_t* this, void* dead )
00095 {
00096   /* Make sure the object pointer is valid */
00097   assert( this != NULL );
00098 
00099   /* Convert the dead structure to a link to be stack up */
00100   FreeLink_t* link = (FreeLink_t*)dead;
00101   link->next = this->head;
00102   this->head = link;
00103 }
00104 
00105 /*
00106    Really destroy the entire FreeList elements
00107 */
00108 void FreeList_purge( FreeList_t* this )
00109 {
00110   /* Current purging pointer */
00111   register FreeLink_t* curr = NULL
00112 
00113   /* Make sure the object pointer is valid */
00114   assert( this != NULL );
00115 
00116   /* Go through the entire list and free every link */
00117   while( this->head != NULL )
00118   {
00119           curr = this->head;
00120     this->head = this->head->next;
00121     free( curr );
00122   }
00123 }
00124 
00125 /*
00126    Really destroy the entire FreeList elements
00127    including the FreeList itself.
00128 */
00129 void FreeList_delete( FreeList_t* this )
00130 {
00131   /* Make sure the object pointer is valid */
00132   assert( this != NULL );
00133 
00134   /* Free every link */
00135   FreeList_purge( this );
00136 
00137   /* Free the object itself */
00138   free( this );
00139 }
00140 
00141 #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