A4 programming

profilesehj
ObjectManager.c

//------------------------------------------------ // NAME : YOUR NAME // STUDENT# : ####### // // COURSE : COMP 2160, SECTION: A01 // INSTRUCTOR: Stela H. Seo // ASSIGNMENT: 04 //------------------------------------------------ //------------------------------------------------------------------------------ // INCLUDE HEADERS //------------------------------------------------------------------------------ #include <stdlib.h> #include <string.h> #include <stdio.h> #include <assert.h> #include "ObjectManager.h" //------------------------------------------------------------------------------ // CONSTANTS AND TYPES //------------------------------------------------------------------------------ // A Memblock holds the relevent information associated with an allocated block of memory by our memory manager typedef struct MEMBLOCK MemBlock; struct MEMBLOCK { // Information needed to track our objects in memory int numBytes; // how big is this object? int startAddr; // where the object starts Ref ref; // the reference used to identify the object int count; // the number of references to this object MemBlock * next; // pointer to next block. Blocks stored in a linked list. }; typedef unsigned char byte; //------------------------------------------------------------------------------ // PROTOTYPES //------------------------------------------------------------------------------ //-------------------------------------------------- // PURPOSE: Performs our garbage collection //-------------------------------------------------- static void compact( void ); //-------------------------------------------------- // PURPOSE: Find a node that has given reference and its previous node. // INPUT PARAMETERS: // [ref]<IN> Object reference to find the index node. // [curr]<IN/OUT> Pointer to a current node pointer to begin searching. // At the return, this will point to NULL or valid node depending on the search result. // [prev]<IN/OUT> Pointer to a previous node pointer. // This must point previous node of current node. If current node is the first node, this must be NULL. // At the return, this will point to previous node of current node depending on the search result. // OUTPUT PARAMETERS: // [boolean]<OUT> True, if we found the node that we are looking for; otherwise, false. // In case of false, do not use the pointers since it may point invalid location. //-------------------------------------------------- static boolean find( const Ref ref, MemBlock ** const curr, MemBlock ** const prev ); //-------------------------------------------------- // PURPOSE: Check our invariant //-------------------------------------------------- static void validate( void ); //------------------------------------------------------------------------------ // VARIABLES //------------------------------------------------------------------------------ // This is where we allocate memory. One of these is always the current buffer. The other is used for swapping during compaction stage. static int bufferSize = 0; static byte * buffer1 = NULL; static byte * buffer2 = NULL; static byte * currBuffer = NULL; // points to the current buffer containing our data static Ref nextRef = 1; // tracks the next reference to use, we start at 1 so we can use 0 as the NULL reference static int freeIndex = 0; // points to the location of the next available memory location static int numBlocks = 0; // number of blocks allocated our buffers. // The blocks are stored in a linked list where the start of the list is pointed to by memBlockStart. static MemBlock * memBlockStart = NULL; // start of linked list of blocks allocated static MemBlock * memBlockEnd = NULL; // end of linked list of blocks allocated //------------------------------------------------------------------------------ // FUNCTIONS //------------------------------------------------------------------------------ boolean initialize( const int size ) { // TODO: Implement this function return false; } void destroy( void ) { // TODO: Implement this function } Ref insert( const int size ) { Ref newRef = NULL_REF; // TODO: Implement this function return newRef; } void addRef( const Ref ref ) { MemBlock * prev = NULL; MemBlock * curr = memBlockStart; assert( NULL_REF < ref && ref < nextRef ); validate( ); if( find( ref, &curr, &prev ) ) { assert( NULL != curr ); assert( ref == curr->ref ); curr->count++; } } void removeRef( const Ref ref ) { // TODO: Implement this function } void * get( const Ref ref ) { MemBlock * prev = NULL; MemBlock * curr = memBlockStart; void * object = NULL; assert( NULL_REF < ref && ref < nextRef ); validate( ); if( find( ref, &curr, &prev ) ) { assert( NULL != curr ); assert( ref == curr->ref ); object = &currBuffer[curr->startAddr]; } return object; } void dump( void ) { MemBlock * curr = memBlockStart; while( NULL != curr ) { printf( "RefID: %lu, Start address: %d, Num bytes: %d, Reference count: %d\n", curr->ref, curr->startAddr, curr->numBytes, curr->count ); curr = curr->next; } printf( "Next available index: %d\n", freeIndex ); printf( "Number of objects: %d\n\n", numBlocks ); } static void compact( void ) { // TODO: Implement this function } static boolean find( const Ref ref, MemBlock ** const curr, MemBlock ** const prev ) { /* Let's discuss this function. This function helps us to iterate our linked list. It is useful because we only implement once and reuse this multiple times. If we made a mistake in the implementation, we have one place to fix. However, the implementation is not so great. First, we have pointers to pointers (curr and prev), which increase complexity of the code. In addition, we always keep updating the previous node pointer as well, even though some functions does not need the previous node. */ assert( NULL_REF != ref ); assert( NULL != curr ); assert( NULL != prev ); assert( (*prev == NULL && *curr == memBlockStart) || (*prev != NULL && (*prev)->next == *curr) ); while( NULL != *curr && ref != (*curr)->ref ) { *prev = *curr; *curr = (*curr)->next; } return NULL != *curr && ref == (*curr)->ref; } static void validate( void ) { // TODO: Implement this function }