From a82186dab48c234c507693b166acd7c13433864a Mon Sep 17 00:00:00 2001 From: glenda Date: Sat, 22 Oct 2022 16:45:32 +0000 Subject: Atomic reference counting --- quadnames.c | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) (limited to 'quadnames.c') diff --git a/quadnames.c b/quadnames.c index 29e973d..814376e 100644 --- a/quadnames.c +++ b/quadnames.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -210,7 +211,7 @@ void setquadrawio(Datum *new) { if(new->tag == ArrayTag && GetType(new->array) == AtypeRune){ - for(int i = 0; i < GetSize(new->array); i++) + for(int i = 0; i < new->array->size; i++) print("%C", new->array->runedata[i]); return; } @@ -361,7 +362,7 @@ quadem(Array *codes) if(GetType(codes) != AtypeInt) throwerror(nil, EDomain); Array *res; - if(GetSize(codes) == 1){ + if(codes->size == 1){ Rune *msg = errorstr(codes->intdata[0]); if(runestrlen(msg) == 0){ msg = runesmprint("ERROR NUMBER %lld", codes->intdata[0]); @@ -371,7 +372,7 @@ quadem(Array *codes) res = mkrunearray(msg); }else{ res = duparrayshape(codes, AtypeArray); - for(int i = 0; i < GetSize(codes); i++){ + for(int i = 0; i < codes->size; i++){ Array *code = arrayitem(codes, i); res->arraydata[i] = quadem(code); freearray(code); @@ -386,7 +387,7 @@ quadsignal1(Array *code) { if(GetType(code) != AtypeInt) throwerror(nil, EDomain); - if(GetSize(code) != 1) + if(code->size != 1) throwerror(nil, ELength); throwerror(nil, code->intdata[0]); return nil; @@ -397,7 +398,7 @@ quadsignal2(Array *msg, Array *code) { if(GetType(code) != AtypeInt || GetType(msg) != AtypeRune) throwerror(nil, EDomain); - if(GetSize(code) != 1) + if(code->size != 1) throwerror(nil, ELength); if(GetRank(msg) > 1) throwerror(nil, ERank); @@ -452,11 +453,11 @@ quaducs(Array *a) Array *res = nil; if(GetType(a) == AtypeInt){ res = duparrayshape(a, AtypeRune); - for(int i = 0; i < GetSize(res); i++) + for(int i = 0; i < res->size; i++) res->runedata[i] = a->intdata[i]; }else if(GetType(a) == AtypeRune){ res = duparrayshape(a, AtypeInt); - for(int i = 0; i < GetSize(res); i++) + for(int i = 0; i < res->size; i++) res->intdata[i] = a->runedata[i]; }else throwerror(nil, EDomain); @@ -468,7 +469,7 @@ Array * quaddl(Array *a) { /* TODO: return amount of seconds slept */ - if(GetSize(a) != 1) + if(a->size != 1) throwerror(nil, ELength); if(GetType(a) != AtypeInt && GetType(a) != AtypeFloat) throwerror(nil, EDomain); @@ -511,7 +512,7 @@ quadthreads2(Array *thread, Array *property) Array * quadserial(Array *mode, Array *a) { - if(GetType(mode) != AtypeInt || GetSize(mode) != 1) + if(GetType(mode) != AtypeInt || mode->size != 1) throwerror(nil, EDomain); int m = mode->intdata[0]; if(m != 0 && m != 1) @@ -521,7 +522,7 @@ quadserial(Array *mode, Array *a) Array *result; if(m == 0){ /* serialize */ Array *header = allocarray(AtypeInt, 1, 2+GetRank(a)); - header->shape[0] = GetSize(header); + header->shape[0] = header->size; header->intdata[0] = GetType(a); header->intdata[1] = GetRank(a); for(int i = 0; i < GetRank(a); i++) @@ -531,7 +532,7 @@ quadserial(Array *mode, Array *a) if(GetType(a) == AtypeArray) /* nested */ body = rundfn(L"⊃,⌿0⎕SERIAL¨,⍵", nil, nil, nil, a); else{ - int len = datasizes[GetType(a)] * GetSize(a); + int len = datasizes[GetType(a)] * a->size; body = allocarray(AtypeInt, 1, len); body->shape[0] = len; for(int i = 0; i < len; i++) @@ -554,13 +555,13 @@ quadserial(Array *mode, Array *a) if(type == AtypeArray){ /* nested */ int skips[512]; int depth = 0; - Array *starts = allocarray(AtypeInt, 1, GetSize(a)); - starts->shape[0] = GetSize(starts); - for(int i = 0; i < GetSize(starts); i++) + Array *starts = allocarray(AtypeInt, 1, a->size); + starts->shape[0] = starts->size; + for(int i = 0; i < starts->size; i++) starts->intdata[i] = 0; int offset = 2+rank; skips[0] = 0; - while(offset < GetSize(a)){ + while(offset < a->size){ if(depth == 0 && skips[0] == 0) starts->intdata[offset] = 1; int type = a->intdata[offset]; @@ -611,7 +612,7 @@ quadopen(Array *file, Array *omode) { if(GetType(file) != AtypeRune || GetRank(file) > 1) throwerror(L"Invalid file name", EDomain); - if(GetType(omode) != AtypeInt || GetSize(omode) != 1) + if(GetType(omode) != AtypeInt || omode->size != 1) throwerror(L"Invalid mode", EDomain); Rune *tmp = pparray(file); @@ -629,7 +630,7 @@ quadcreate(Array *file, Array *arg) { if(GetType(file) != AtypeRune || GetRank(file) > 1) throwerror(L"Invalid file name", EDomain); - if(GetType(arg) != AtypeInt || GetRank(arg) != 1 || GetSize(arg) != 2) + if(GetType(arg) != AtypeInt || GetRank(arg) != 1 || arg->size != 2) throwerror(L"Invalid mode+perm", EDomain); Rune *tmp = pparray(file); @@ -644,7 +645,7 @@ quadcreate(Array *file, Array *arg) Array * quadclose(Array *fd) { - if(GetSize(fd) != 1 || GetType(fd) != AtypeInt) + if(fd->size != 1 || GetType(fd) != AtypeInt) throwerror(nil, EDomain); int ret = close(fd->intdata[0]); return mkscalarint(ret); @@ -654,9 +655,9 @@ quadclose(Array *fd) Array * quadread(Array *fd, Array *nbytes) { - if(GetType(fd) != AtypeInt || GetSize(fd) != 1) + if(GetType(fd) != AtypeInt || fd->size != 1) throwerror(L"Invalid fd", EDomain); - if(GetType(nbytes) != AtypeInt || GetSize(nbytes) != 1 || nbytes->intdata[0] < 1) + if(GetType(nbytes) != AtypeInt || nbytes->size != 1 || nbytes->intdata[0] < 1) throwerror(L"Invalid byte coint", EDomain); u8int *buf = emalloc(nbytes->intdata[0]); @@ -679,14 +680,14 @@ quadread(Array *fd, Array *nbytes) Array * quadwrite(Array *fd, Array *data) { - if(GetType(fd) != AtypeInt || GetSize(fd) != 1) + if(GetType(fd) != AtypeInt || fd->size != 1) throwerror(L"Invalid fd", EDomain); if((GetType(data) != AtypeInt && GetType(data) != AtypeFloat) || GetRank(data) > 1) throwerror(L"Data must be a scalar or vector of bytes!", EDomain); - u8int *raw = emalloc(GetSize(data)); - for(int i = 0; i < GetSize(data); i++){ + u8int *raw = emalloc(data->size); + for(int i = 0; i < data->size; i++){ u8int v; switch(GetType(data)){ case AtypeInt: v = data->intdata[i]; break; @@ -695,7 +696,7 @@ quadwrite(Array *fd, Array *data) } raw[i] = v; } - long ret = write(fd->intdata[0], raw, GetSize(data)); + long ret = write(fd->intdata[0], raw, data->size); free(raw); return mkscalarint(ret); } @@ -707,10 +708,10 @@ quadpipe(Array *name) Array *result; int p[2]; - if(GetSize(name) > 0 && (GetType(name) != AtypeRune || GetRank(name) > 1)) + if(name->size > 0 && (GetType(name) != AtypeRune || GetRank(name) > 1)) throwerror(L"Invalid pipe name", EDomain); - if(GetSize(name) == 0){ /* Unnamed pipe */ + if(name->size == 0){ /* Unnamed pipe */ pipe(p); result = allocarray(AtypeInt, 1, 2); result->shape[0] = 2; @@ -744,7 +745,7 @@ quadpipe(Array *name) Array * quadfd2path(Array *fd) { - if(GetType(fd) != AtypeInt || GetSize(fd) != 1) + if(GetType(fd) != AtypeInt || fd->size != 1) throwerror(L"Invalid fd", EDomain); char *buf = emalloc(1024); int ret = fd2path(fd->intdata[0], buf, 1024); -- cgit v1.2.3