fix le memory leak

This commit is contained in:
2026-06-16 20:01:42 +10:00
parent 9539629cfb
commit 02b5a9cc54
6 changed files with 73 additions and 5 deletions

10
src/Free/Arg.c Normal file
View File

@@ -0,0 +1,10 @@
#include "../../include/ground.h"
void _GroundFreeArg(GroundArg* in) {
if (in->type == GroundArg_Value) {
Ground.Free.Value(&in->as.value);
} else {
// FIXME - double free for arg
// free(in->as.ref);
}
}

10
src/Free/Instruction.c Normal file
View File

@@ -0,0 +1,10 @@
#include "../../include/ground.h"
void _GroundFreeInstruction(GroundInstruction* in) {
for (size_t i = 0; i < in->args.len; i++) {
Ground.Free.Arg(&in->args.at[i]);
}
free(in->args.at);
in->args.len = 0;
in->args.capacity = 0;
}

10
src/Free/Program.c Normal file
View File

@@ -0,0 +1,10 @@
#include "../../include/ground.h"
void _GroundFreeProgram(GroundProgram* in) {
for (size_t i = 0; i < in->len; i++) {
Ground.Free.Instruction(&in->at[i]);
}
free(in->at);
in->len = 0;
in->capacity = 0;
}

26
src/Free/State.c Normal file
View File

@@ -0,0 +1,26 @@
#include "../../include/ground.h"
void _GroundFreeState(GroundState* in) {
{
GroundVariable *s, *tmp;
HASH_ITER(hh, in->variables, s, tmp) {
Ground.Free.Value(&s->value);
HASH_DEL(in->variables, s);
free(s);
}
}
{
GroundCatch *s, *tmp;
HASH_ITER(hh, in->catches, s, tmp) {
HASH_DEL(in->variables, s);
free(s);
}
}
{
GroundLabel *s, *tmp;
HASH_ITER(hh, in->labels, s, tmp) {
HASH_DEL(in->variables, s);
free(s);
}
}
}

View File

@@ -7,9 +7,10 @@
static inline void doLabels(GroundProgram* program, GroundState* state) { static inline void doLabels(GroundProgram* program, GroundState* state) {
for (size_t i = 0; i < program->len; i++) { for (size_t i = 0; i < program->len; i++) {
for (size_t j = 0; j < program->len; j++) { GroundInstruction* instruction = &program->at[i];
GroundArg* arg = &program->at[i].args.at[j]; if (instruction->type == GroundInstruction_CREATELABEL) {
if (arg->type == GroundArg_Label) { if (instruction->args.len > 0) {
GroundArg* arg = &instruction->args.at[0];
GroundLabel* label = NULL; GroundLabel* label = NULL;
HASH_FIND_STR(state->labels, arg->as.ref, label); HASH_FIND_STR(state->labels, arg->as.ref, label);
@@ -20,6 +21,8 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
Ground.Flags.error = true; Ground.Flags.error = true;
return; return;
} }
strncpy(label->name, arg->as.ref, 2047);
label->name[2047] = '\0';
label->lineNum = i; label->lineNum = i;
HASH_ADD_STR(state->labels, name, label); HASH_ADD_STR(state->labels, name, label);
} }
@@ -139,8 +142,7 @@ void _GroundInternalRun(GroundProgram* program, GroundState* state) {
return; return;
} }
} }
// TODO: implement Ground.Free.Instruction(&instruction);
// Ground.Free.Instruction(&instruction);
} }
} }

View File

@@ -38,6 +38,11 @@ void _GroundFreeFunction(GroundFunction* in);
void _GroundFreeStruct(GroundStruct* in); void _GroundFreeStruct(GroundStruct* in);
void _GroundFreeObject(GroundObject* in); void _GroundFreeObject(GroundObject* in);
void _GroundFreeArg(GroundArg* in);
void _GroundFreeInstruction(GroundInstruction* in);
void _GroundFreeProgram(GroundProgram* in);
void _GroundFreeState(GroundState* in);
GroundValue _GroundCopyValue(GroundValue* in); GroundValue _GroundCopyValue(GroundValue* in);
GroundList _GroundCopyList(GroundList* in); GroundList _GroundCopyList(GroundList* in);
@@ -133,6 +138,11 @@ struct _Ground Ground = {
.Function = _GroundFreeFunction, .Function = _GroundFreeFunction,
.Struct = _GroundFreeStruct, .Struct = _GroundFreeStruct,
.Object = _GroundFreeObject, .Object = _GroundFreeObject,
.Arg = _GroundFreeArg,
.Instruction = _GroundFreeInstruction,
.Program = _GroundFreeProgram,
.State = _GroundFreeState,
}, },
.Copy = { .Copy = {