00001 #ifndef __J2K__FreeList_C__
00002 #define __J2K__FreeList_C__ 1
00003
00004 #include "FreeList.h"
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FreeList_t* FreeList_create( register size_t sz )
00020 {
00021 FreeList_t* fl = (FreeList_t*)malloc( sizeof( FreeList_t ) );
00022
00023
00024 assert( fl != NULL );
00025
00026
00027 fl->head = NULL;
00028
00029
00030 if ( sz >= sizeof( FreeLink_t* ) )
00031 {
00032
00033 fl->size = sz;
00034 }
00035 else
00036 {
00037
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
00049 return fl;
00050 }
00051
00052
00053
00054
00055
00056 void* FreeList_calloc( FreeList_t* this )
00057 {
00058
00059
00060
00061
00062 register void* curr = NULL;
00063
00064
00065 assert( this != NULL );
00066
00067 if ( this->head != NULL )
00068 {
00069
00070 curr = this->head;
00071 this->head = this->head->next;
00072 }
00073 else
00074 {
00075
00076 curr = malloc( this->size );
00077
00078
00079 assert( curr != NULL );
00080 }
00081
00082
00083 memset( curr, 0, this->size );
00084
00085
00086 return curr;
00087 }
00088
00089
00090
00091
00092
00093
00094 void FreeList_free( FreeList_t* this, void* dead )
00095 {
00096
00097 assert( this != NULL );
00098
00099
00100 FreeLink_t* link = (FreeLink_t*)dead;
00101 link->next = this->head;
00102 this->head = link;
00103 }
00104
00105
00106
00107
00108 void FreeList_purge( FreeList_t* this )
00109 {
00110
00111 register FreeLink_t* curr = NULL
00112
00113
00114 assert( this != NULL );
00115
00116
00117 while( this->head != NULL )
00118 {
00119 curr = this->head;
00120 this->head = this->head->next;
00121 free( curr );
00122 }
00123 }
00124
00125
00126
00127
00128
00129 void FreeList_delete( FreeList_t* this )
00130 {
00131
00132 assert( this != NULL );
00133
00134
00135 FreeList_purge( this );
00136
00137
00138 free( this );
00139 }
00140
00141 #endif