00001 00021 #include "hop.h" 00022 00026 00027 // memory management 00028 #define IVY_PAGE_SIZE 12 // page size containing 2^IVY_PAGE_SIZE nodes 00029 #define IVY_PAGE_MASK 4095 // page bitmask (2^IVY_PAGE_SIZE)-1 00030 00034 00046 void Hop_ManStartMemory( Hop_Man_t * p ) 00047 { 00048 p->vChunks = Vec_PtrAlloc( 128 ); 00049 p->vPages = Vec_PtrAlloc( 128 ); 00050 } 00051 00063 void Hop_ManStopMemory( Hop_Man_t * p ) 00064 { 00065 void * pMemory; 00066 int i; 00067 Vec_PtrForEachEntry( p->vChunks, pMemory, i ) 00068 free( pMemory ); 00069 Vec_PtrFree( p->vChunks ); 00070 Vec_PtrFree( p->vPages ); 00071 p->pListFree = NULL; 00072 } 00073 00086 void Hop_ManAddMemory( Hop_Man_t * p ) 00087 { 00088 char * pMemory; 00089 int i, nBytes; 00090 assert( sizeof(Hop_Obj_t) <= 64 ); 00091 assert( p->pListFree == NULL ); 00092 // assert( (Hop_ManObjNum(p) & IVY_PAGE_MASK) == 0 ); 00093 // allocate new memory page 00094 nBytes = sizeof(Hop_Obj_t) * (1<<IVY_PAGE_SIZE) + 64; 00095 pMemory = ALLOC( char, nBytes ); 00096 Vec_PtrPush( p->vChunks, pMemory ); 00097 // align memory at the 32-byte boundary 00098 pMemory = pMemory + 64 - (((int)pMemory) & 63); 00099 // remember the manager in the first entry 00100 Vec_PtrPush( p->vPages, pMemory ); 00101 // break the memory down into nodes 00102 p->pListFree = (Hop_Obj_t *)pMemory; 00103 for ( i = 1; i <= IVY_PAGE_MASK; i++ ) 00104 { 00105 *((char **)pMemory) = pMemory + sizeof(Hop_Obj_t); 00106 pMemory += sizeof(Hop_Obj_t); 00107 } 00108 *((char **)pMemory) = NULL; 00109 } 00110 00114 00115