Use reference counting for identifiers
This commit is contained in:
@@ -25,17 +25,19 @@ GroundFunction _GroundCopyFunction(GroundFunction* in) {
|
||||
|
||||
*newFunction.closure = Ground.Copy.State(in->closure);
|
||||
|
||||
newFunction.args.at = malloc(sizeof(char*) * in->args.capacity);
|
||||
newFunction.args.at = malloc(sizeof(GroundFunctionArg) * in->args.capacity);
|
||||
if (newFunction.args.at == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Copy.Function()");
|
||||
Ground.Flags.error = true;
|
||||
return newFunction;
|
||||
}
|
||||
for (size_t i = 0; i < in->args.count; i++) {
|
||||
size_t len = strlen(in->args.at[i].as.id);
|
||||
newFunction.args.at = malloc(sizeof(char) * (len + 1));
|
||||
if (newFunction.args.at == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Copy.Function()");
|
||||
Ground.Flags.error = true;
|
||||
return newFunction;
|
||||
}
|
||||
|
||||
strcpy(newFunction.args.at[i].as.id, in->args.at[i].as.id);
|
||||
newFunction.args.at->as.id = Ground.Copy.Identifier(in->args.at[i].as.id);
|
||||
}
|
||||
|
||||
return newFunction;
|
||||
|
||||
6
src/Copy/Identifier.c
Normal file
6
src/Copy/Identifier.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "../../include/ground.h"
|
||||
|
||||
GroundIdentifier* _GroundCopyIdentifier(GroundIdentifier* identifier) {
|
||||
identifier->referenceCount++;
|
||||
return identifier;
|
||||
}
|
||||
@@ -4,7 +4,6 @@ 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);
|
||||
Ground.Free.Identifier(in->as.ref);
|
||||
}
|
||||
}
|
||||
|
||||
9
src/Free/Identifier.c
Normal file
9
src/Free/Identifier.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "../../include/ground.h"
|
||||
|
||||
void _GroundFreeIdentifier(GroundIdentifier* identifier) {
|
||||
identifier->referenceCount--;
|
||||
if (identifier->referenceCount == 0) {
|
||||
free(identifier->string);
|
||||
free(identifier);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "../../include/ground.h"
|
||||
#include <string.h>
|
||||
|
||||
void _GroundFunctionAppendArg(GroundFunction* function, const char* argName) {
|
||||
void _GroundFunctionAppendArg(GroundFunction* function, GroundIdentifier* argName) {
|
||||
if (function->isNativeFunction) {
|
||||
Ground.Log.Error("Cannot add arg to native function in Ground.Function.appendArg()");
|
||||
Ground.Flags.error = true;
|
||||
@@ -19,14 +18,6 @@ void _GroundFunctionAppendArg(GroundFunction* function, const char* argName) {
|
||||
function->args.capacity *= 2;
|
||||
}
|
||||
|
||||
size_t len = strlen(argName);
|
||||
function->args.at[function->args.count].as.id = malloc(sizeof(char) * (len + 1));
|
||||
if (function->args.at[function->args.count].as.id == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Function.appendArg()");
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
strcpy(function->args.at[function->args.count].as.id, argName);
|
||||
function->args.at[function->args.count].as.id = Ground.Copy.Identifier(argName);
|
||||
function->args.count++;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
if (instruction->args.len > 0) {
|
||||
GroundArg* arg = &instruction->args.at[0];
|
||||
GroundLabel* label = NULL;
|
||||
HASH_FIND_STR(state->labels, arg->as.ref, label);
|
||||
HASH_FIND_STR(state->labels, arg->as.ref->string, label);
|
||||
|
||||
if (label == NULL) {
|
||||
label = malloc(sizeof(GroundLabel));
|
||||
@@ -21,7 +21,7 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
strncpy(label->name, arg->as.ref, 2047);
|
||||
strncpy(label->name, arg->as.ref->string, 2047);
|
||||
label->name[2047] = '\0';
|
||||
label->lineNum = i;
|
||||
HASH_ADD_STR(state->labels, name, label);
|
||||
@@ -34,10 +34,10 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
GroundInstruction* instruction = &program->at[i];
|
||||
if (instruction->type == GroundInstruction_JUMP) {
|
||||
GroundArg* arg = &instruction->args.at[0];
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref);
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction JUMP at %zu) in Ground.Internal.Run()", arg->as.ref, i);
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction JUMP at %zu) in Ground.Internal.Run()", arg->as.ref->string, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
@@ -47,10 +47,10 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
|
||||
} else if (instruction->type == GroundInstruction_IF) {
|
||||
GroundArg* arg = &instruction->args.at[1];
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref);
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction IF at %zu) in Ground.Internal.Run()", arg->as.ref, i);
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction IF at %zu) in Ground.Internal.Run()", arg->as.ref->string, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
@@ -60,10 +60,10 @@ static inline void doLabels(GroundProgram* program, GroundState* state) {
|
||||
|
||||
} else if (instruction->type == GroundInstruction_CATCH) {
|
||||
GroundArg* arg = &instruction->args.at[1];
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref);
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref->string);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction CATCH at %zu) in Ground.Internal.Run()", arg->as.ref, i);
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction CATCH at %zu) in Ground.Internal.Run()", arg->as.ref->string, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
@@ -92,7 +92,7 @@ static inline size_t doOffsets(GroundProgram* program, GroundState* state) {
|
||||
) continue;
|
||||
|
||||
GroundVariable* item = NULL;
|
||||
HASH_FIND_STR(state->variables, arg->as.ref, item);
|
||||
HASH_FIND_STR(state->variables, arg->as.ref->string, item);
|
||||
|
||||
if (item == NULL) {
|
||||
item = malloc(sizeof(GroundVariable));
|
||||
@@ -102,7 +102,7 @@ static inline size_t doOffsets(GroundProgram* program, GroundState* state) {
|
||||
return 0;
|
||||
}
|
||||
item->value = Ground.New.Value.Int(0);
|
||||
strncpy(item->name, arg->as.ref, 2047);
|
||||
strncpy(item->name, arg->as.ref->string, 2047);
|
||||
item->_offset = size++;
|
||||
HASH_ADD_STR(state->variables, name, item);
|
||||
}
|
||||
|
||||
@@ -1,25 +1,8 @@
|
||||
#include "../../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundArg _GroundNewArgDirectRef(const char* ref) {
|
||||
size_t len = strlen(ref);
|
||||
char* copy = malloc(len + 1);
|
||||
|
||||
if (copy == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Arg.DirectRef()");
|
||||
Ground.Flags.error = true;
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = ""
|
||||
};
|
||||
}
|
||||
|
||||
strcpy(copy, ref);
|
||||
|
||||
GroundArg _GroundNewArgDirectRef(GroundIdentifier* ref) {
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = copy
|
||||
.as.ref = Ground.Copy.Identifier(ref)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,25 +1,8 @@
|
||||
#include "../../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundArg _GroundNewArgFunctionRef(const char* ref) {
|
||||
size_t len = strlen(ref);
|
||||
char* copy = malloc(len + 1);
|
||||
|
||||
if (copy == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Arg.FunctionRef()");
|
||||
Ground.Flags.error = true;
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = ""
|
||||
};
|
||||
}
|
||||
|
||||
strcpy(copy, ref);
|
||||
|
||||
GroundArg _GroundNewArgFunctionRef(GroundIdentifier* ref) {
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_FunctionRef,
|
||||
.as.ref = copy
|
||||
.as.ref = Ground.Copy.Identifier(ref)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,26 +1,9 @@
|
||||
#include "../../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundArg _GroundNewArgLabelRef(const char* ref) {
|
||||
size_t len = strlen(ref);
|
||||
char* copy = malloc(len + 1);
|
||||
|
||||
if (copy == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Arg.LabelRef()");
|
||||
Ground.Flags.error = true;
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = ""
|
||||
};
|
||||
}
|
||||
|
||||
strcpy(copy, ref);
|
||||
|
||||
GroundArg _GroundNewArgLabelRef(GroundIdentifier* ref) {
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_Label,
|
||||
.as.ref = copy
|
||||
.as.ref = Ground.Copy.Identifier(ref)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +1,8 @@
|
||||
#include "../../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundArg _GroundNewArgLineRef(const char* ref) {
|
||||
size_t len = strlen(ref);
|
||||
char* copy = malloc(len + 1);
|
||||
|
||||
if (copy == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Arg.LineRef()");
|
||||
Ground.Flags.error = true;
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = ""
|
||||
};
|
||||
}
|
||||
|
||||
strcpy(copy, ref);
|
||||
|
||||
GroundArg _GroundNewArgLineRef(GroundIdentifier* ref) {
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_LineRef,
|
||||
.as.ref = copy
|
||||
.as.ref = Ground.Copy.Identifier(ref)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,26 +1,9 @@
|
||||
#include "../../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundArg _GroundNewArgTypeRef(const char* ref) {
|
||||
size_t len = strlen(ref);
|
||||
char* copy = malloc(len + 1);
|
||||
|
||||
if (copy == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Arg.TypeRef()");
|
||||
Ground.Flags.error = true;
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = ""
|
||||
};
|
||||
}
|
||||
|
||||
strcpy(copy, ref);
|
||||
|
||||
GroundArg _GroundNewArgTypeRef(GroundIdentifier* ref) {
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_TypeRef,
|
||||
.as.ref = copy
|
||||
.as.ref = Ground.Copy.Identifier(ref)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,9 @@
|
||||
#include "../../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GroundArg _GroundNewArgValueRef(const char* ref) {
|
||||
size_t len = strlen(ref);
|
||||
char* copy = malloc(len + 1);
|
||||
|
||||
if (copy == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Arg.ValueRef()");
|
||||
Ground.Flags.error = true;
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_DirectRef,
|
||||
.as.ref = ""
|
||||
};
|
||||
}
|
||||
|
||||
strcpy(copy, ref);
|
||||
|
||||
GroundArg _GroundNewArgValueRef(GroundIdentifier* ref) {
|
||||
return (GroundArg) {
|
||||
.type = GroundArg_ValueRef,
|
||||
.as.ref = copy
|
||||
.as.ref = Ground.Copy.Identifier(ref)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
22
src/New/Identifier.c
Normal file
22
src/New/Identifier.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "../../include/ground.h"
|
||||
|
||||
GroundIdentifier* _GroundNewIdentifier(const char* id) {
|
||||
GroundIdentifier* identifier = malloc(sizeof(GroundIdentifier));
|
||||
if (identifier == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Identifier");
|
||||
Ground.Flags.error = true;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
identifier->string = malloc(sizeof(char) * (strlen(id) + 1));
|
||||
if (identifier->string == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.New.Identifier");
|
||||
Ground.Flags.error = true;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(identifier->string, id);
|
||||
identifier->referenceCount = 1;
|
||||
|
||||
return identifier;
|
||||
}
|
||||
@@ -11,12 +11,12 @@ GroundValue _GroundNewValueStruct(GroundStruct in);
|
||||
GroundValue _GroundNewValueObject(GroundObject in);
|
||||
|
||||
GroundArg _GroundNewArgValue(GroundValue value);
|
||||
GroundArg _GroundNewArgValueRef(const char* ref);
|
||||
GroundArg _GroundNewArgDirectRef(const char* ref);
|
||||
GroundArg _GroundNewArgLineRef(const char* ref);
|
||||
GroundArg _GroundNewArgLabelRef(const char* ref);
|
||||
GroundArg _GroundNewArgFunctionRef(const char* ref);
|
||||
GroundArg _GroundNewArgTypeRef(const char* ref);
|
||||
GroundArg _GroundNewArgValueRef(GroundIdentifier* ref);
|
||||
GroundArg _GroundNewArgDirectRef(GroundIdentifier* ref);
|
||||
GroundArg _GroundNewArgLineRef(GroundIdentifier* ref);
|
||||
GroundArg _GroundNewArgLabelRef(GroundIdentifier* ref);
|
||||
GroundArg _GroundNewArgFunctionRef(GroundIdentifier* ref);
|
||||
GroundArg _GroundNewArgTypeRef(GroundIdentifier* ref);
|
||||
|
||||
GroundList _GroundNewList();
|
||||
GroundString _GroundNewString(const char* in);
|
||||
@@ -30,6 +30,7 @@ GroundInstruction _GroundNewInstruction(enum GroundInstructionType type);
|
||||
GroundProgram _GroundNewProgram();
|
||||
GroundType _GroundNewType(enum GroundTypeType type, ...);
|
||||
|
||||
GroundIdentifier* _GroundNewIdentifier(const char* id);
|
||||
|
||||
void _GroundFreeValue(GroundValue* in);
|
||||
void _GroundFreeList(GroundList* in);
|
||||
@@ -43,6 +44,8 @@ void _GroundFreeInstruction(GroundInstruction* in);
|
||||
void _GroundFreeProgram(GroundProgram* in);
|
||||
void _GroundFreeState(GroundState* in);
|
||||
|
||||
void _GroundFreeIdentifier(GroundIdentifier* identifier);
|
||||
|
||||
|
||||
GroundValue _GroundCopyValue(GroundValue* in);
|
||||
GroundList _GroundCopyList(GroundList* in);
|
||||
@@ -56,6 +59,8 @@ GroundInstruction _GroundCopyInstruction(GroundInstruction* in);
|
||||
GroundProgram _GroundCopyProgram(GroundProgram* in);
|
||||
GroundState _GroundCopyState(GroundState* in);
|
||||
|
||||
GroundIdentifier* _GroundCopyIdentifier(GroundIdentifier* identifier);
|
||||
|
||||
|
||||
void _GroundListAppend(GroundList* list, GroundValue value);
|
||||
|
||||
@@ -129,6 +134,8 @@ struct _Ground Ground = {
|
||||
.Instruction = _GroundNewInstruction,
|
||||
.Program = _GroundNewProgram,
|
||||
.Type = _GroundNewType,
|
||||
|
||||
.Identifier = _GroundNewIdentifier,
|
||||
},
|
||||
|
||||
.Free = {
|
||||
@@ -143,6 +150,8 @@ struct _Ground Ground = {
|
||||
.Instruction = _GroundFreeInstruction,
|
||||
.Program = _GroundFreeProgram,
|
||||
.State = _GroundFreeState,
|
||||
|
||||
.Identifier = _GroundFreeIdentifier,
|
||||
},
|
||||
|
||||
.Copy = {
|
||||
@@ -158,6 +167,7 @@ struct _Ground Ground = {
|
||||
.Program = _GroundCopyProgram,
|
||||
.State = _GroundCopyState,
|
||||
|
||||
.Identifier = _GroundCopyIdentifier,
|
||||
},
|
||||
|
||||
.List = {
|
||||
|
||||
Reference in New Issue
Block a user