#include #include #include #include #include "apl9.h" int alloccounts = 0; int debugmem; void * emalloc(ulong size) { void *res = malloc(size); if(res == nil){ print("Out of memory! :(\n"); exits("emalloc"); } return res; } void checkmem(char *where) { if(debugmem){ print("Checking memory at: %s\n", where); poolcheck(mainmem); } } void freearray(Array *a) { if(a == nil) return; a->refs--; if(a->refs == 0){ /* print("Freeing array: %S (%p)\n", pparray(a), a); */ if(a->type == AtypeArray) for(int i = 0; i < a->size; i++) freearray(a->arraydata[i]); free(a->shape); free(a); alloccounts--; }else if(a->refs > 0){ /* print("Not freeing (refs=%d): %S (%p)\n", a->refs, pparray(a), a); */ }else{ print("NEGATIVE REF COUNT! %p\n", a); exits(nil); } } Array * allocarray(arrayDataType t, int rank, int size) { Array *a = emalloc(sizeof(Array)); a->rank = rank; a->type = t; a->size = size; a->stranded = 0; a->shape = emalloc(sizeof(int) * rank); a->rawdata = emalloc(datasizes[t] * size); a->type = t; a->refs = 1; alloccounts++; return a; } void incarrayref(Array *a) { /* print("INCREF %d->%d %S\n", a->refs, a->refs+1, pparray(a)); */ a->refs++; if(a->type == AtypeArray) for(int i = 0; i < a->size; i++) incarrayref(a->arraydata[i]); }