Cleaned up the list implementation so as to NOT copy items into a new buffer.

This commit is contained in:
2022-10-07 20:46:55 +00:00
parent bdb4d2b241
commit 4b08707e67
4 changed files with 21 additions and 34 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ typedef struct {
} List; } List;
List* CreateList(void); List* CreateList(void);
int AddListItem(const void *, size_t, List *); int AddListItem(void *, List *);
void DestroyList(List*); void DestroyList(List*);
#endif #endif
+5 -19
View File
@@ -1,4 +1,5 @@
#include "../includes/list.h" #include "../includes/list.h"
#include <stdlib.h>
List* CreateList() { List* CreateList() {
List *new = malloc(sizeof(List)); List *new = malloc(sizeof(List));
@@ -8,7 +9,7 @@ List* CreateList() {
return NULL; return NULL;
} }
new->content = malloc(sizeof(void*) * LISTDEFAULTSIZE); new->content = calloc(LISTDEFAULTSIZE, sizeof(void*));
if (!new->content) { if (!new->content) {
fprintf(stderr, "Failed to malloc() memory for List contents.\n"); fprintf(stderr, "Failed to malloc() memory for List contents.\n");
@@ -22,10 +23,8 @@ List* CreateList() {
return new; return new;
} }
int AddListItem(const void *value, size_t size, List* list) { int AddListItem(void *item, List* list) {
if (!list) return -1; if (!list || !item) return -1;
if (!value) return -1;
if (size == 0) return -1;
if (list->capacity < list->size + 1) { if (list->capacity < list->size + 1) {
void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2); 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->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->content[list->size] = item;
list->size++; list->size++;
@@ -57,10 +47,6 @@ int AddListItem(const void *value, size_t size, List* list) {
void DestroyList(List* list) { void DestroyList(List* list) {
if (!list) return; if (!list) return;
for(int i = 0; i < list->size; i++) {
free(list->content[i]);
}
free(list->content); free(list->content);
free(list); free(list);
} }
+8 -7
View File
@@ -42,12 +42,15 @@ IRState* ParseTokens(List* tokens) {
AdvanceParser(); //Consume the label token AdvanceParser(); //Consume the label token
if (!symbol) AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1; if (!symbol) symbol = AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable);
else symbol->Resolved = 1;
symbol->Resolved = 1;
AdvanceParser(); //Consume the NewLine AdvanceParser(); //Consume the NewLine
// Instruction* ins = HandleOperation(); Instruction* ins = HandleOperation();
if (!ins) printf("No op %s\n", symbol->Name);
// if (!ins) continue; // if (!ins) continue;
@@ -211,13 +214,11 @@ Instruction* HandleOperation() {
InsertInstruction: InsertInstruction:
AddListItem(ins, sizeof(Instruction), MachineState.Instructions); AddListItem(ins, MachineState.Instructions);
AdvanceParser(); //Consume the line break AdvanceParser(); //Consume the line break
free(ins); return ins;
return MachineState.Instructions->content[(MachineState.Instructions->size - 1) * sizeof(Instruction)];
} }
void PrintSymbols(void) { void PrintSymbols(void) {
+7 -7
View File
@@ -54,7 +54,7 @@ List* GenerateTokenList(const char* source) {
token = CreateToken(Line, PunctuationClass); token = CreateToken(Line, PunctuationClass);
token->Value.Punctuation = NewLine; token->Value.Punctuation = NewLine;
AddListItem(token, sizeof(Token), tokens); AddListItem(token, tokens);
AdvanceScanner(); AdvanceScanner();
Line++; Line++;
break; break;
@@ -69,28 +69,28 @@ List* GenerateTokenList(const char* source) {
break; break;
case '.': //directive like ".org" or ".db" case '.': //directive like ".org" or ".db"
token = ParseDirective(); token = ParseDirective();
if (token) AddListItem(token, sizeof(Token), tokens); if (token) AddListItem(token, tokens);
break; break;
case '"': case '"':
token = ParseString(); token = ParseString();
if (token) AddListItem(token, sizeof(Token), tokens); if (token) AddListItem(token, tokens);
break; break;
default: default:
if (isdigit(c)) { if (isdigit(c)) {
AddListItem(ParseNumber(), sizeof(Token), tokens); AddListItem(ParseNumber(), tokens);
break; break;
} }
if (IsPunctuation(c)) { if (IsPunctuation(c)) {
AdvanceScanner(); AdvanceScanner();
AddListItem(ParsePunctuation(c), sizeof(Token), tokens); AddListItem(ParsePunctuation(c), tokens);
break; break;
} }
token = ParseIdentifier(); token = ParseIdentifier();
if (token) AddListItem(token, sizeof(Token), tokens); if (token) AddListItem(token, tokens);
break; break;
} }
@@ -111,7 +111,7 @@ List* GenerateTokenList(const char* source) {
token->EndOfFile = 1; token->EndOfFile = 1;
AddListItem(token, sizeof(Token), tokens); AddListItem(token, tokens);
return tokens; return tokens;
} }