mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Automatically free container elements if needed
This commit is contained in:
2
makefile
2
makefile
@@ -26,7 +26,7 @@ repl: source
|
||||
|
||||
#various kinds of available tests
|
||||
.PHONY: tests
|
||||
tests: clean test-cases
|
||||
tests: clean test-cases test-integrations
|
||||
|
||||
.PHONY: test-all
|
||||
test-all: clean test-cases test-integrations test-benchmarks
|
||||
|
||||
@@ -5,13 +5,18 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) {
|
||||
//TODO: slip in a call to free the complex values here
|
||||
|
||||
if (capacity == 0) {
|
||||
free(paramArray);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//if some values will be removed, free them first
|
||||
if (paramArray != NULL && paramArray->count > capacity) {
|
||||
for (unsigned int i = capacity; i < paramArray->count; i++) {
|
||||
Toy_freeValue(paramArray->data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity;
|
||||
|
||||
Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array));
|
||||
|
||||
@@ -19,9 +19,12 @@ Toy_Stack* Toy_allocateStack() {
|
||||
}
|
||||
|
||||
void Toy_freeStack(Toy_Stack* stack) {
|
||||
//TODO: slip in a call to free the complex values here
|
||||
|
||||
if (stack != NULL) {
|
||||
//if some values will be removed, free them first
|
||||
for (unsigned int i = 0; i < stack->count; i++) {
|
||||
Toy_freeValue(stack->data[i]);
|
||||
}
|
||||
|
||||
free(stack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,9 +89,15 @@ Toy_Table* Toy_allocateTable() {
|
||||
}
|
||||
|
||||
void Toy_freeTable(Toy_Table* table) {
|
||||
//TODO: slip in a call to free the complex values here
|
||||
if (table != NULL) {
|
||||
//if some values will be removed, free them first
|
||||
for (unsigned int i = 0; i < table->capacity; i++) {
|
||||
Toy_freeValue(table->data[i].key);
|
||||
Toy_freeValue(table->data[i].value);
|
||||
}
|
||||
|
||||
free(table);
|
||||
}
|
||||
}
|
||||
|
||||
void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value) {
|
||||
|
||||
Reference in New Issue
Block a user