Cleaned up the list implementation so as to NOT copy items into a new buffer.
This commit is contained in:
+5
-19
@@ -1,4 +1,5 @@
|
||||
#include "../includes/list.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
+8
-7
@@ -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) {
|
||||
|
||||
+7
-7
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user