123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "vm/Arrays.h"
- /*typedef struct {
- int length;
- DataType type;
- int references;
- int next;
- int previous;
- void* data;
- } Array;
- typedef struct {
- int capacity;
- int usedStart;
- int freeStart;
- Array* data;
- } Arrays;*/
- void asInit(Arrays* as) {
- as->capacity = 0;
- as->usedStart = -1;
- as->freeStart = -1;
- as->data = NULL;
- }
- void asDelete(Arrays* as) {
- for(int i = 0; i < as->capacity; i++) {
- free(as->data[i].data);
- }
- free(as->data);
- }
- static void aInitArray(Array* a, int previous, int next) {
- a->length = 0;
- a->type = DT_VOID;
- a->references = 0;
- a->next = next;
- a->previous = previous;
- a->data = NULL;
- }
- static void asInitArrays(Arrays* as, int start) {
- int end = as->capacity - 1;
- aInitArray(as->data + start, -1, start + 1);
- for(int i = start + 1; i < end; i++) {
- aInitArray(as->data + i, i - 1, i + 1);
- }
- aInitArray(as->data + end, end - 1, -1);
- as->freeStart = start;
- }
- static void asEnsureCapacity(Arrays* as) {
- if(as->freeStart != -1) {
- return;
- }
- if(as->data == NULL) {
- as->capacity = 4;
- as->data = malloc(sizeof(Array) * as->capacity);
- asInitArrays(as, 0);
- } else {
- int start = as->capacity;
- as->capacity *= 2;
- as->data = realloc(as->data, sizeof(Array) * as->capacity);
- asInitArrays(as, start);
- }
- }
- int asAllocate(Arrays* as, DataType type, int length) {
- asEnsureCapacity(as);
- int index = as->freeStart;
- Array* array = as->data + index;
- as->freeStart = array->next;
- if(array->next == -1) {
- as->data[array->next].previous = -1;
- }
- array->next = as->usedStart;
- if(as->usedStart != -1) {
- as->data[as->usedStart].previous = index;
- }
- as->usedStart = index;
- array->length = length;
- array->type = type;
- array->data = malloc(sizeof(dtGetSize(type)) * length);
- return index;
- }
- Array* asGet(Arrays* as, int p) {
- if(p < 0 || p >= as->capacity) {
- return NULL;
- }
- return as->data + p;
- }
- void aAddReference(Array* a) {
- a->references++;
- }
- void aRemoveReference(Array* a) {
- if(--a->references > 0) {
- return;
- }
- printf("Please remove me");
- }
- void asPrintDebug(Arrays* as) {
- (void)as;
- }
|