// *************** HEAP.C ********************* // Fixed Block Memory manager // Global structures of Fixed Block Memory manager // The "Heap" is the statically allocated memory to be managed // Last modified 1/16/03 by Jonathan W. Valvano // Copyright 2003 by Jonathan W. Valvano, valvano@uts.cc.utexas.edu // You may use, edit, run or distribute this file // as long as the above copyright notice remains unsigned short Heap[BLOCKSIZE*NUMBLOCKS]; unsigned short *FreePt; // Pointer the first free block void Heap_Init(void){ unsigned short *pt; FreePt = &Heap[0]; for(pt=&Heap[0];pt!=&Heap[BLOCKSIZE*(NUMBLOCKS-1)];pt=pt+BLOCKSIZE){ *pt = (unsigned short)pt+2*BLOCKSIZE; /* Create linked list */ } *pt = null; } /* Returns a pointer to a memory block of "BLOCKSIZE" integers The returned pointer will be null (0) if the Heap is empty */ unsigned short *Heap_Allocate(void){ unsigned short *pt; pt = FreePt; if(pt!=null){ FreePt = (unsigned short*)*pt; } return(pt); } /* Releases the memory block of "BLOCKSIZE" integers pointed to by "pt" No protection, we assume "pt" points to a previously allocated block */ void Heap_Release(unsigned short *pt){ unsigned short *oldFreePt; oldFreePt = FreePt; FreePt = pt; *pt = (unsigned short)oldFreePt; }