From 4b08707e671733e87e45f7565f8e2395d5b1102a Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Fri, 7 Oct 2022 20:46:55 +0000 Subject: [PATCH] Cleaned up the list implementation so as to NOT copy items into a new buffer. --- includes/list.h | 2 +- src/list.c | 24 +++++------------------- src/parser.c | 15 ++++++++------- src/scanner.c | 14 +++++++------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/includes/list.h b/includes/list.h index 0fef199..4cd3bef 100644 --- a/includes/list.h +++ b/includes/list.h @@ -15,7 +15,7 @@ typedef struct { } List; List* CreateList(void); -int AddListItem(const void *, size_t, List *); +int AddListItem(void *, List *); void DestroyList(List*); #endif \ No newline at end of file diff --git a/src/list.c b/src/list.c index c87acc9..2a24ebd 100644 --- a/src/list.c +++ b/src/list.c @@ -1,4 +1,5 @@ #include "../includes/list.h" +#include List* CreateList() { List *new = malloc(sizeof(List)); @@ -8,7 +9,7 @@ List* CreateList() { return NULL; } - new->content = malloc(sizeof(void*) * LISTDEFAULTSIZE); + new->content = calloc(LISTDEFAULTSIZE, sizeof(void*)); if (!new->content) { fprintf(stderr, "Failed to malloc() memory for List contents.\n"); @@ -22,10 +23,8 @@ List* CreateList() { return new; } -int AddListItem(const void *value, size_t size, List* list) { - if (!list) return -1; - if (!value) return -1; - if (size == 0) return -1; +int AddListItem(void *item, List* list) { + if (!list || !item) return -1; if (list->capacity < list->size + 1) { void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2); @@ -36,18 +35,9 @@ int AddListItem(const void *value, size_t size, List* list) { } list->content = ptr; - list->capacity = list->capacity * 2; + list->capacity *= 2; } - void* item = calloc(1, size); - - if (!item) { - fprintf(stderr, "Failed to calloc() new memory (%lu bytes).\n", size); - return -1; - } - - memcpy(item, value, size); - list->content[list->size] = item; list->size++; @@ -57,10 +47,6 @@ int AddListItem(const void *value, size_t size, List* list) { void DestroyList(List* list) { if (!list) return; - for(int i = 0; i < list->size; i++) { - free(list->content[i]); - } - free(list->content); free(list); } \ No newline at end of file diff --git a/src/parser.c b/src/parser.c index 988f9f5..7e3bec5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -42,12 +42,15 @@ IRState* ParseTokens(List* tokens) { AdvanceParser(); //Consume the label token - if (!symbol) AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1; - else symbol->Resolved = 1; + if (!symbol) symbol = AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable); + + symbol->Resolved = 1; AdvanceParser(); //Consume the NewLine - // Instruction* ins = HandleOperation(); + Instruction* ins = HandleOperation(); + + if (!ins) printf("No op %s\n", symbol->Name); // if (!ins) continue; @@ -211,13 +214,11 @@ Instruction* HandleOperation() { InsertInstruction: - AddListItem(ins, sizeof(Instruction), MachineState.Instructions); + AddListItem(ins, MachineState.Instructions); AdvanceParser(); //Consume the line break - free(ins); - - return MachineState.Instructions->content[(MachineState.Instructions->size - 1) * sizeof(Instruction)]; + return ins; } void PrintSymbols(void) { diff --git a/src/scanner.c b/src/scanner.c index e6c9abd..d193f4c 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -54,7 +54,7 @@ List* GenerateTokenList(const char* source) { token = CreateToken(Line, PunctuationClass); token->Value.Punctuation = NewLine; - AddListItem(token, sizeof(Token), tokens); + AddListItem(token, tokens); AdvanceScanner(); Line++; break; @@ -69,28 +69,28 @@ List* GenerateTokenList(const char* source) { break; case '.': //directive like ".org" or ".db" token = ParseDirective(); - if (token) AddListItem(token, sizeof(Token), tokens); + if (token) AddListItem(token, tokens); break; case '"': token = ParseString(); - if (token) AddListItem(token, sizeof(Token), tokens); + if (token) AddListItem(token, tokens); break; default: if (isdigit(c)) { - AddListItem(ParseNumber(), sizeof(Token), tokens); + AddListItem(ParseNumber(), tokens); break; } if (IsPunctuation(c)) { AdvanceScanner(); - AddListItem(ParsePunctuation(c), sizeof(Token), tokens); + AddListItem(ParsePunctuation(c), tokens); break; } token = ParseIdentifier(); - if (token) AddListItem(token, sizeof(Token), tokens); + if (token) AddListItem(token, tokens); break; } @@ -111,7 +111,7 @@ List* GenerateTokenList(const char* source) { token->EndOfFile = 1; - AddListItem(token, sizeof(Token), tokens); + AddListItem(token, tokens); return tokens; }