1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "vm/Allocator.h"
- /*void aInit(Allocator* a) {
- a->capacity = 0;
- a->usedStart = -1;
- a->freeStart = -1;
- a->data = NULL;
- }
- void aDelete(Allocator* a) {
- for(int i = a->usedStart; i != -1; i = a->data[i].next) {
- printf("end Dealloc %d\n", a->data[i].length);
- free(a->data[i].data);
- }
- free(a->data);
- }
- static void aInitArray(Array* a, int next) {
- a->length = 0;
- a->next = next;
- a->marked = false;
- a->data = NULL;
- }
- static void aInitArrays(Allocator* a, int start) {
- for(int i = start; i < a->capacity - 1; i++) {
- aInitArray(a->data + i, i + 1);
- }
- aInitArray(a->data + (a->capacity - 1), -1);
- a->freeStart = start;
- }
- int aAllocate(Allocator* a, int length) {
- if(a->freeStart == -1) {
- if(a->data == NULL) {
- a->capacity = 4;
- a->data = malloc(sizeof(Array) * a->capacity);
- aInitArrays(a, 0);
- } else {
- int start = a->capacity;
- a->capacity *= 2;
- a->data = realloc(a->data, sizeof(Array) * a->capacity);
- aInitArrays(a, start);
- }
- }
- int index = a->freeStart;
- Array* array = a->data + index;
- a->freeStart = array->next;
- array->length = length;
- array->next = a->usedStart;
- a->usedStart = index;
- array->data = malloc(sizeof(Object) * length);
- for(int i = 0; i < length; i++) {
- array->data[i].type = OT_NULL;
- }
- return index;
- }
- void aClearMarker(Allocator* a) {
- for(int i = a->usedStart; i != -1; i = a->data[i].next) {
- a->data[i].marked = false;
- }
- }
- void aRemoveUnmarked(Allocator* a) {
- int prev = -1;
- int i = a->usedStart;
- while(i != -1) {
- int next = a->data[i].next;
- if(!a->data[i].marked) {
- free(a->data[i].data);
- if(prev == -1) {
- a->usedStart = next;
- } else {
- a->data[prev].next = next;
- }
- a->data[i].next = a->freeStart;
- a->freeStart = i;
- a->data[i].length = 0;
- a->data[i].data = NULL;
- } else {
- prev = i;
- }
- i = next;
- }
- }*/
|