/* Create a new, blank dictionary */ void Niall_NewDictionary(void) { ClearDictionary(); WordList=AddWord(""); if(!WordList) Niall_Error("Out of memory."); }
void ClearAllGC() { unsigned int index; /* contexts */ for (index = 0; index < gGCManager.ctx_size; index++) { CONTEXT* context = gGCManager.contexts[index]; ClearContext(context); } /* functions */ for (index = 0; index < gGCManager.func_size; index++) { FUNCTION* function = gGCManager.functions[index]; ClearFunction(function); } /* dictionaries */ for (index = 0; index < gGCManager.table_size; index++) { HASH_TABLE* table = gGCManager.tables[index]; ClearDictionary(table); } /* strings */ for (index = 0; index < gGCManager.str_size; index++) { char* string = gGCManager.strings[index]; ClearString(string); } /* parse tree */ AST_STORE *cur_ast, *next_ast; cur_ast = gGCManager.parseTrees; while (cur_ast) { next_ast = cur_ast->next; FreeParseTree(cur_ast->parseTree); free(cur_ast); cur_ast = next_ast; } /* lexings */ LEXING_STORE *cur_lex, *next_lex; cur_lex = gGCManager.lexings; while (cur_lex) { next_lex = cur_lex->next; FreeLexing(cur_lex->tokens, cur_lex->buffer); free(cur_lex); cur_lex = next_lex; } }
/* Load a dictionary */ void Niall_LoadDictionary(char *FileName) { char Buffer[BUFSIZ]; FILE *fHandle; int nWords,i,d; int nAsocs,j,w,p,k; WORD *Word; fHandle=fopen(FileName,"r"); if(!fHandle) { Niall_Warning("File %s not found.",FileName); return; } fscanf(fHandle,"%s %d\n",Buffer,&nWords); if((strcmp(Buffer,FILE_ID))||(nWords<2)) { Niall_Warning("File %s is not a valid Niall file.",FileName); return; } ClearDictionary(); for(i=0;i<nWords;i++) { fscanf(fHandle,"%4d: %s %d|",&d,Buffer,&nAsocs); if((d!=i)||(nAsocs<1)) { Niall_Warning("Word %d is corrupted.",i); Niall_NewDictionary(); return; } if(Buffer[0]=='>') { if(i!=0) { Niall_Warning("Word %d is corrupted.",i); Niall_NewDictionary(); return; } Buffer[0]=0; } Word=AddWord(Buffer); if(Word==NULL) Niall_Error("Out of memory."); for(j=0;j<nAsocs;) { fscanf(fHandle," %d(%d)",&w,&p); if(w>=nWords) { Niall_Warning("Word %d/Assoc %d is corrupted.",i,j); Niall_NewDictionary(); return; } for(k=0;k<p;k++) Associate(Word,w); j+=p; } } }
void CallGCTraceRoot(CONTEXT* root, VALUE curExpr) { //printf("RUNNING GARBAGE COLLECTOR\n"); //printf("."); GC_DATA_MANAGEMENT managed; unsigned int index; managed = InitGC(/*managed*/); GCAddValue(curExpr, &managed); // CallGCRecurseContext(root, &managed); CallGCRecurseContext(gCurrentContext, &managed); CONTEXT_STACK* stack = gExecutionStack; while (stack) { CallGCRecurseContext(stack->context, &managed); stack = stack->next; } //CallGCRecurseContext(gGlobalContext, &managed); // find elements in gGCManager that are not // members of gc managed // then remove those elements and free() // their memory /* contexts */ for (index = 0; index < gGCManager.ctx_size; index++) { CONTEXT* context = gGCManager.contexts[index]; if (!CONTAINS_POINTER_ARRAY((void**)managed.contexts, managed.ctx_size, (void*)context)) { ClearContext(context); gGCManager.contexts[index] = NULL; gGCManager.contexts[index] = gGCManager.contexts[gGCManager.ctx_size-1]; gGCManager.ctx_size--; index--; } } /* functions */ for (index = 0; index < gGCManager.func_size; index++) { FUNCTION* function = gGCManager.functions[index]; if (!CONTAINS_POINTER_ARRAY((void**)managed.functions, managed.func_size, (void*)function)) { ClearFunction(function); gGCManager.functions[index] = NULL; gGCManager.functions[index] = gGCManager.functions[gGCManager.func_size-1]; gGCManager.func_size--; index--; } } /* dictionaries */ for (index = 0; index < gGCManager.table_size; index++) { HASH_TABLE* table = gGCManager.tables[index]; if (!CONTAINS_POINTER_ARRAY((void**)managed.tables, managed.table_size, (void*)table)) { ClearDictionary(table); gGCManager.tables[index] = NULL; gGCManager.tables[index] = gGCManager.tables[gGCManager.table_size-1]; gGCManager.table_size--; index--; } } /* strings */ for (index = 0; index < gGCManager.str_size; index++) { char* string = gGCManager.strings[index]; if (!CONTAINS_POINTER_ARRAY((void**)managed.strings, managed.str_size, (void*)string)) { ClearString(string); gGCManager.strings[index] = NULL; gGCManager.strings[index] = gGCManager.strings[gGCManager.str_size-1]; gGCManager.str_size--; index--; } } FreeGCMgmtObject(managed); }
CDictionary::~CDictionary() { ClearDictionary(); }
void Niall_Free(void) { ClearDictionary(); }