clox/memory.c

46 lines
920 B
C
Raw Permalink Normal View History

2019-06-02 14:58:15 -05:00
#include <stdlib.h>
#include "common.h"
#include "memory.h"
#include "vm.h"
#include "value.h"
void* reallocate(void* previous, size_t oldSize, size_t newSize) {
if (newSize == 0) {
free(previous);
return NULL;
}
return realloc(previous, newSize);
}
static void freeObject(Obj* object) {
switch (object->type) {
case OBJ_STRING: {
ObjString* string = (ObjString*) object;
FREE_ARRAY(char, string->chars, string->length + 1);
FREE(ObjString, object);
break;
}
case OBJ_ARRAY: {
ObjArray* array = (ObjArray*) object;
freeValueArray(array->array);
FREE(ObjArray, object);
break;
}
case OBJ_HASH: {
ObjHash* hash = (ObjHash*) object;
freeTable(hash->hashTable);
FREE(ObjHash, object);
}
}
}
void freeObjects() {
Obj* object = vm.objects;
while (object != NULL) {
Obj* next = object->next;
freeObject(object);
object = next;
}
}