/******************************************************************** Function name: st_create() Purpose: Creates a new empty symbol table Author: Warren Rainey History/Versions: 1.0 Called functions: malloc(), st_setsize(), b_create(), free() In parameters: int Return Value: STD (Symbol Table Descriptor) **********************************************************************/ STD st_create(int st_size){ STD symTable; symTable.plsBD=0; sym_table.st_size = 0; if(st_size <= 0){ return symTable; } symTable.pstvr = (STVR*) malloc(sizeof (STVR)*st_size); if(symTable.pstvr == NULL){ st_setsize();/*This is useless. Setting to 0 inorder to not return garbage*/ return symTable; } symTable.plsBD = b_create(INITIAL_CAPACITY,INCREMENT_FACTOR,'a'); symTable.st_offset = 0; if(symTable.plsBD == NULL){ free(sym_table.pstvr); st_setsize(); }else{ symTable.st_size = st_size; } return symTable; }
/******************************************************************************* * Purpose: Frees memory occupied by symbol table dynamic areas and sets * global symbol table size to 0. * Author: Skye Turriff * History: Version 1, 21 November 2015 * Called functions: b_destroy(), free(), st_setsize() * Parameters: STD sym_table struct * Return value: None *******************************************************************************/ void st_destroy(STD sym_table) { if (sym_table.st_size != 0) { b_destroy(sym_table.plsBD); free(sym_table.pstvr); st_setsize(); } }
/******************************************************************** Function name: st_destroy() Purpose: This function frees the memory occupied by the symbol table dynamic areas and sets st_size to 0. Author: Warren Rainey History/Versions: 1.0 Called functions: b_destroy(), free(), st_setsize() In parameters: STD Return Value: Void **********************************************************************/ void st_destroy(STD sym_table){ if(sym_table.plsBD==NULL && sym_table.pstvr == NULL){ return; } if(sym_table.st_size != 0){ b_destroy(sym_table.plsBD); free(sym_table.pstvr); st_setsize();/*Set size of symboltable to 0*/ } return; }
/********************************************************************************************************** Purpose: Free all the dynamically allocated memory associated with the symbol table. Author: Thom Palmer History/Versions: 10.18.13 Called functions: b_destroy(), free() Parameters: STD sym_table Return value: void Algorithm: Check to ensure both dynamically allocated structures have valid pointers before freeing the memory. We thought this was safer then checking the size. **********************************************************************************************************/ void st_destroy(STD sym_table) { /* Even though b_destroy handles NULL this check avoids creating an unnecessary stack frame */ if(sym_table.plsBD) { b_destroy(sym_table.plsBD); } /* Ensure the pointer is valid before freeing the memory to avoid crashing */ if(sym_table.pstvr) { free((STVR*)sym_table.pstvr); } /* Set the size to 0 so the STD will be marked as invalid */ st_setsize(); }