From 02b5a9cc5416f4e71db1183248bd06fcecc81031 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Tue, 16 Jun 2026 20:01:42 +1000 Subject: [PATCH] fix le memory leak --- src/Free/Arg.c | 10 ++++++++++ src/Free/Instruction.c | 10 ++++++++++ src/Free/Program.c | 10 ++++++++++ src/Free/State.c | 26 ++++++++++++++++++++++++++ src/Internal/run.c | 12 +++++++----- src/libmain.c | 10 ++++++++++ 6 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/Free/Arg.c create mode 100644 src/Free/Instruction.c create mode 100644 src/Free/Program.c create mode 100644 src/Free/State.c diff --git a/src/Free/Arg.c b/src/Free/Arg.c new file mode 100644 index 0000000..89e210e --- /dev/null +++ b/src/Free/Arg.c @@ -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); + } +} diff --git a/src/Free/Instruction.c b/src/Free/Instruction.c new file mode 100644 index 0000000..10670e6 --- /dev/null +++ b/src/Free/Instruction.c @@ -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; +} diff --git a/src/Free/Program.c b/src/Free/Program.c new file mode 100644 index 0000000..d0a0167 --- /dev/null +++ b/src/Free/Program.c @@ -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; +} diff --git a/src/Free/State.c b/src/Free/State.c new file mode 100644 index 0000000..820aee3 --- /dev/null +++ b/src/Free/State.c @@ -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); + } + } +} diff --git a/src/Internal/run.c b/src/Internal/run.c index 743602b..cad53b6 100644 --- a/src/Internal/run.c +++ b/src/Internal/run.c @@ -7,9 +7,10 @@ static inline void doLabels(GroundProgram* program, GroundState* state) { for (size_t i = 0; i < program->len; i++) { - for (size_t j = 0; j < program->len; j++) { - GroundArg* arg = &program->at[i].args.at[j]; - if (arg->type == GroundArg_Label) { + GroundInstruction* instruction = &program->at[i]; + if (instruction->type == GroundInstruction_CREATELABEL) { + if (instruction->args.len > 0) { + GroundArg* arg = &instruction->args.at[0]; GroundLabel* label = NULL; 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; return; } + strncpy(label->name, arg->as.ref, 2047); + label->name[2047] = '\0'; label->lineNum = i; HASH_ADD_STR(state->labels, name, label); } @@ -139,8 +142,7 @@ void _GroundInternalRun(GroundProgram* program, GroundState* state) { return; } } - // TODO: implement - // Ground.Free.Instruction(&instruction); + Ground.Free.Instruction(&instruction); } } diff --git a/src/libmain.c b/src/libmain.c index bf40bf6..9e345fc 100644 --- a/src/libmain.c +++ b/src/libmain.c @@ -38,6 +38,11 @@ void _GroundFreeFunction(GroundFunction* in); void _GroundFreeStruct(GroundStruct* 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); GroundList _GroundCopyList(GroundList* in); @@ -133,6 +138,11 @@ struct _Ground Ground = { .Function = _GroundFreeFunction, .Struct = _GroundFreeStruct, .Object = _GroundFreeObject, + + .Arg = _GroundFreeArg, + .Instruction = _GroundFreeInstruction, + .Program = _GroundFreeProgram, + .State = _GroundFreeState, }, .Copy = {