00001 00019 #include "reo.h" 00020 00024 00025 static void reoUnitsAddToFreeUnitList( reo_man * p ); 00026 00030 00042 reo_unit * reoUnitsGetNextUnit(reo_man * p ) 00043 { 00044 reo_unit * pUnit; 00045 // check there are stil units to extract 00046 if ( p->pUnitFreeList == NULL ) 00047 reoUnitsAddToFreeUnitList( p ); 00048 // extract the next unit from the linked list 00049 pUnit = p->pUnitFreeList; 00050 p->pUnitFreeList = pUnit->Next; 00051 p->nUnitsUsed++; 00052 return pUnit; 00053 } 00054 00066 void reoUnitsRecycleUnit( reo_man * p, reo_unit * pUnit ) 00067 { 00068 pUnit->Next = p->pUnitFreeList; 00069 p->pUnitFreeList = pUnit; 00070 p->nUnitsUsed--; 00071 } 00072 00084 void reoUnitsRecycleUnitList( reo_man * p, reo_plane * pPlane ) 00085 { 00086 reo_unit * pUnit; 00087 reo_unit * pTail; 00088 00089 if ( pPlane->pHead == NULL ) 00090 return; 00091 00092 // find the tail 00093 for ( pUnit = pPlane->pHead; pUnit; pUnit = pUnit->Next ) 00094 pTail = pUnit; 00095 pTail->Next = p->pUnitFreeList; 00096 p->pUnitFreeList = pPlane->pHead; 00097 memset( pPlane, 0, sizeof(reo_plane) ); 00098 } 00099 00111 void reoUnitsStopDispenser( reo_man * p ) 00112 { 00113 int i; 00114 for ( i = 0; i < p->nMemChunks; i++ ) 00115 free( p->pMemChunks[i] ); 00116 // printf("\nThe number of chunks used is %d, each of them %d units\n", p->nMemChunks, REO_CHUNK_SIZE ); 00117 p->nMemChunks = 0; 00118 } 00119 00131 void reoUnitsAddUnitToPlane( reo_plane * pPlane, reo_unit * pUnit ) 00132 { 00133 if ( pPlane->pHead == NULL ) 00134 { 00135 pPlane->pHead = pUnit; 00136 pUnit->Next = NULL; 00137 } 00138 else 00139 { 00140 pUnit->Next = pPlane->pHead; 00141 pPlane->pHead = pUnit; 00142 } 00143 pPlane->statsNodes++; 00144 } 00145 00146 00158 void reoUnitsAddToFreeUnitList( reo_man * p ) 00159 { 00160 int c; 00161 // check that we still have chunks left 00162 if ( p->nMemChunks == p->nMemChunksAlloc ) 00163 { 00164 printf( "reoUnitsAddToFreeUnitList(): Memory manager ran out of memory!\n" ); 00165 fflush( stdout ); 00166 return; 00167 } 00168 // allocate the next chunk 00169 assert( p->pUnitFreeList == NULL ); 00170 p->pUnitFreeList = ALLOC( reo_unit, REO_CHUNK_SIZE ); 00171 // split chunks into list-connected units 00172 for ( c = 0; c < REO_CHUNK_SIZE-1; c++ ) 00173 (p->pUnitFreeList + c)->Next = p->pUnitFreeList + c + 1; 00174 // set the last pointer to NULL 00175 (p->pUnitFreeList + REO_CHUNK_SIZE-1)->Next = NULL; 00176 // add the chunk to the array of chunks 00177 p->pMemChunks[p->nMemChunks++] = p->pUnitFreeList; 00178 } 00179 00180 00184