From a82186dab48c234c507693b166acd7c13433864a Mon Sep 17 00:00:00 2001 From: glenda Date: Sat, 22 Oct 2022 16:45:32 +0000 Subject: Atomic reference counting --- apl9.h | 33 ++++----- array.c | 53 +++++++------- concurrency.c | 3 +- error.c | 3 +- eval.c | 29 ++++---- functions.c | 219 +++++++++++++++++++++++++++++----------------------------- hybrids.c | 35 +++++----- inverse.c | 1 + lexer.c | 1 + main.c | 3 +- memory.c | 42 +++-------- operators.c | 25 +++---- print.c | 9 +-- quadnames.c | 55 +++++++-------- symbol.c | 29 ++++---- 15 files changed, 264 insertions(+), 276 deletions(-) diff --git a/apl9.h b/apl9.h index bfb4d99..08c4d00 100644 --- a/apl9.h +++ b/apl9.h @@ -83,20 +83,16 @@ struct Mixed }; }; -#define GetSize(a) ((u32int)(a->info & 0x00000000FFFFFFFF)) -#define GetRank(a) ((u8int)((a->info & 0x0000000F00000000) >> 32)) -#define GetType(a) ((u8int)((a->info & 0x000000F000000000) >> 36)) -#define GetStrand(a) ((u8int)((a->info & 0x0000010000000000) >> 40)) -#define GetRefs(a) ((u32int)((a->info & 0xFFFFFE0000000000) >> 41)) -#define SetSize(a,v) (a->info ^= ((u64int)GetSize(a)^v)) -#define SetRank(a,v) (a->info ^= ((u64int)(GetRank(a)^v) << 32)) -#define SetType(a,v) (a->info ^= ((u64int)(GetType(a)^v) << 36)) -#define SetStrand(a,v) (a->info ^= ((u64int)(GetStrand(a)^v) << 40)) -#define SetRefs(a,v) (a->info ^= ((u64int)(GetRefs(a)^v) << 41)) +#define GetRank(a) ((u8int)((a->info & 0x000000000000000F) >> 0)) +#define GetType(a) ((u8int)((a->info & 0x000000000000000F) >> 4)) +#define GetStrand(a) ((u8int)((a->info & 0x0000000000000010) >> 8)) +#define SetRank(a,v) (a->info ^= ((u64int)(GetRank(a)^v) << 0)) +#define SetType(a,v) (a->info ^= ((u64int)(GetType(a)^v) << 4)) +#define SetStrand(a,v) (a->info ^= ((u64int)(GetStrand(a)^v) << 8)) struct Array { - u32int *shape; + u64int *shape; union { char *rawdata; vlong *intdata; @@ -106,13 +102,14 @@ struct Array Array **arraydata; Array *prototype; /* only in use when size == 0 and type == AtypeArray */ }; + uvlong size; + Ref; /* Reference count */ u64int info; /* info is a bitmap of information, divided as follows: - * 0-31: 32 bits for size (max size = 4294967296) - * 32-35: 4 bits for the rank (max rank 15) - * 36-39: 4 bits for the type (16 options) - * 40: 1 bit for stranding - * 41-63: 23 bits for reference counting (max refs = 8388608) + * 0-3: 4 bits for the rank (max rank 15) + * 4-7: 4 bits for the type (16 options) + * 8: 1 bit for stranding + * 9-63: unused * NOTE: should only be modified through the macros defined above */ }; @@ -163,7 +160,6 @@ struct Datum { datumTag tag; int shy; - int refs; union { Array *array; Statement stmt; @@ -173,6 +169,7 @@ struct Datum Rune *name; Statement names; }; + Ref; /* reference count*/ }; struct Symbol @@ -314,10 +311,8 @@ void *erealloc(void *, ulong); void checkmem(char *); Array *allocarray(int, int, int); void freearray(Array *); -void incarrayref(Array *); Datum *allocdatum(int, int); void freedatum(Datum *); -void incdatumref(Datum *); void freefunction(Function); void freeoperator(Operator); void freestatement(Statement); diff --git a/array.c b/array.c index 554c60b..3643ee6 100644 --- a/array.c +++ b/array.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -42,8 +43,8 @@ Array * mkrunearray(Rune *str) { Array *a = allocarray(AtypeRune, 1, runestrlen(str)); - a->shape[0] = GetSize(a); - for(int i = 0; i < GetSize(a); i++) + a->shape[0] = a->size; + for(int i = 0; i < a->size; i++) a->runedata[i] = str[i]; return a; } @@ -52,17 +53,17 @@ Array * duparray(Array *a) { Array *b = duparrayshape(a, GetType(a)); - memcpy(b->rawdata, a->rawdata, datasizes[GetType(a)]*GetSize(a)); + memcpy(b->rawdata, a->rawdata, datasizes[GetType(a)]*a->size); if(GetType(b) == AtypeArray) - for(int i = 0; i < GetSize(b); i++) - incarrayref(b->arraydata[i]); + for(int i = 0; i < b->size; i++) + incref(b->arraydata[i]); return b; } Array * duparrayshape(Array *a, int type) { - Array *b = allocarray(type, GetRank(a), GetSize(a)); + Array *b = allocarray(type, GetRank(a), a->size); memcpy(b->shape, a->shape, sizeof(*a->shape) * GetRank(a)); SetStrand(b, GetStrand(a)); return b; @@ -106,7 +107,7 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb) }else if(GetRank(a) != 0 && GetRank(b) == 0){ *aa = fnSame(a); *bb = extend(b, a); - }else if(GetSize(a) == GetSize(b) && GetRank(a) == GetRank(b)){ + }else if(a->size == b->size && GetRank(a) == GetRank(b)){ /* Check that each dimension matches */ for(int i = 0; i < GetRank(a); i++){ if(a->shape[i] != b->shape[i]) @@ -114,7 +115,7 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb) } *aa = fnSame(a); *bb = fnSame(b); - }else if(GetSize(a) == 1 && GetSize(b) == 1){ + }else if(a->size == 1 && b->size == 1){ Array *shape; if(GetRank(a) > GetRank(b)) shape = fnShape(a); @@ -123,12 +124,12 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb) *aa = fnReshape(shape, a); *bb = fnReshape(shape, b); freearray(shape); - }else if(GetSize(a) == 1 && GetSize(b) != 1){ + }else if(a->size == 1 && b->size != 1){ Array *shape = fnShape(b); *aa = fnReshape(shape, a); *bb = fnSame(b); freearray(shape); - }else if(GetSize(a) != 1 && GetSize(b) == 1){ + }else if(a->size != 1 && b->size == 1){ Array *shape = fnShape(a); *aa = fnSame(a); *bb = fnReshape(shape, b); @@ -142,7 +143,7 @@ Array * inttofloatarray(Array *a) { Array *b = duparrayshape(a, AtypeFloat); - for(int i = 0; i < GetSize(a); i++) + for(int i = 0; i < a->size; i++) b->floatdata[i] = a->intdata[i]; return b; } @@ -195,7 +196,7 @@ arrayitem(Array *a, int index) break; case AtypeArray: res = a->arraydata[index]; - incarrayref(res); + incref(res); break; default: throwerror(L"Unhandled case in arrayitem 2", ENotImplemented); @@ -207,7 +208,7 @@ Array * simplifyarray(Array *a) { /* simplify an array if possible. */ - if((GetType(a) != AtypeArray && GetType(a) != AtypeMixed) || GetSize(a) == 0) + if((GetType(a) != AtypeArray && GetType(a) != AtypeMixed) || a->size == 0) return fnSame(a); int nested = GetType(a) == AtypeArray; int type = nested ? GetType(a->arraydata[0]) : a->mixeddata[0].type; @@ -216,7 +217,7 @@ simplifyarray(Array *a) int canmix = 1; int i; - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ int t = nested ? GetType(a->arraydata[i]) : a->mixeddata[i].type; canfloat = canfloat && (t == AtypeFloat || t == AtypeInt); sametype = sametype && (t == type); @@ -227,11 +228,11 @@ simplifyarray(Array *a) if(sametype && type != AtypeArray){ Array *b = duparrayshape(a, type); - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ if(nested){ memcpy(b->rawdata + i * datasizes[type], a->arraydata[i]->rawdata, datasizes[type]); if(GetType(b) == AtypeArray) - incarrayref(b->arraydata[i]); + incref(b->arraydata[i]); }else{ switch(GetType(b)){ case AtypeInt: b->intdata[i] = a->mixeddata[i].i; break; @@ -250,7 +251,7 @@ simplifyarray(Array *a) return b; }else if(canfloat){ Array *b = duparrayshape(a, AtypeFloat); - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ if(nested){ if(GetType(a->arraydata[i]) == AtypeFloat) b->floatdata[i] = a->arraydata[i]->floatdata[0]; @@ -266,7 +267,7 @@ simplifyarray(Array *a) return b; }else if(canmix && nested){ Array *b = duparrayshape(a, AtypeMixed); - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ b->mixeddata[i].type = GetType(a->arraydata[i]); switch(b->mixeddata[i].type){ case AtypeInt: b->mixeddata[i].i = a->arraydata[i]->intdata[0]; break; @@ -305,7 +306,7 @@ comparearray(Array *a, Array *b, int checkshapes) } } - for(i = 0; i < GetSize(a) && i < GetSize(b); i++){ + for(i = 0; i < a->size && i < b->size; i++){ int sub = 0; switch(GetType(a)){ case AtypeInt: @@ -328,9 +329,9 @@ comparearray(Array *a, Array *b, int checkshapes) return sub; } - if(i < GetSize(a)) + if(i < a->size) return 1; - else if(i < GetSize(b)) + else if(i < b->size) return -1; else return 0; @@ -350,10 +351,10 @@ fillelement(Array *a) return fill; } case AtypeArray:{ - if(GetSize(a) == 0) + if(a->size == 0) return fnSame(a->prototype); Array *b = duparrayshape(a, GetType(a)); - for(int i = 0; i < GetSize(b); i++){ + for(int i = 0; i < b->size; i++){ Array *fill = fillelement(a->arraydata[i]); Array *shape = fnShape(a->arraydata[i]); b->arraydata[i] = fnReshape(shape, fill); @@ -375,9 +376,9 @@ arrayspaceused(Array *a) uvlong size = 0; size += sizeof(*a); size += sizeof(*a->shape) * GetRank(a); - size += datasizes[GetType(a)] * GetSize(a); + size += datasizes[GetType(a)] * a->size; - for(int i = 0; i < GetSize(a) && GetType(a) == AtypeArray; i++) + for(int i = 0; i < a->size && GetType(a) == AtypeArray; i++) size += arrayspaceused(a->arraydata[i]); return size; } @@ -389,7 +390,7 @@ arraydepth(Array *a, int *uniform) int max = -1; int subuniform; *uniform = 1; - for(int i = 0; i < GetSize(a); i++){ + for(int i = 0; i < a->size; i++){ int subdepth = arraydepth(a->arraydata[i], &subuniform); if((subdepth != max && max != -1) || subuniform == 0) *uniform = 0; diff --git a/concurrency.c b/concurrency.c index b45ee08..1c73099 100644 --- a/concurrency.c +++ b/concurrency.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "apl9.h" @@ -161,7 +162,7 @@ Retry: /* Check if the new message is OK according to 'match' */ prev = m; Array *matchres = runfunc(match, nil, m->contents); - if(GetRank(matchres) != 1 || GetSize(matchres) != 2){ + if(GetRank(matchres) != 1 || matchres->size != 2){ freearray(matchres); goto Retry; } diff --git a/error.c b/error.c index 4d04696..a0e2a1b 100644 --- a/error.c +++ b/error.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -21,7 +22,7 @@ newerrorguard(Array *codes, Statement *guard) eg->frame = dupdfnframe(fr); } - for(int i = 0; i < GetSize(codes); i++) + for(int i = 0; i < codes->size; i++) eg->code |= (1<intdata[i]); return eg; diff --git a/eval.c b/eval.c index cd9b95a..0752d94 100644 --- a/eval.c +++ b/eval.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -113,9 +114,9 @@ retry: int guardOK = 1; if(stmt->toks[0]->tag != ArrayTag) guardOK = 0; - else if(!stmt->errorguard && GetSize(stmt->toks[0]->array) != 1) + else if(!stmt->errorguard && stmt->toks[0]->array->size != 1) guardOK = 0; - else if(stmt->errorguard && GetSize(stmt->toks[0]->array) < 1) + else if(stmt->errorguard && stmt->toks[0]->array->size < 1) guardOK = 0; else if(GetType(stmt->toks[0]->array) != AtypeInt) guardOK = 0; @@ -174,7 +175,7 @@ lookup(Datum *var) val = symbol->getfn(); else{ val = symbol->value; - incdatumref(val); + incref(val); } val->shy = 0; traceprint("VAR %S = %S\n", var->name, ppdatum(val)); @@ -230,7 +231,7 @@ parens(Datum *left, Datum *right) USED(right); traceprint("PARENS: %S\n", ppdatums(left->stmt.toks, left->stmt.ntoks)); Datum *result = eval(&left->stmt, 1); - incdatumref(result); + incref(result); if(result->tag == ArrayTag) SetStrand(result->array, 0); result->shy = 0; @@ -246,7 +247,7 @@ nameis(Datum *left, Datum *right) result->names.ntoks = 1; result->names.toks = emalloc(sizeof(Datum*)); result->names.toks[0] = left; - incdatumref(left); + incref(left); return result; } @@ -254,7 +255,7 @@ Datum * namesis(Datum *left, Datum *right) { if(left->tag == RParTag){ - incdatumref(right); + incref(right); return right; } @@ -264,7 +265,7 @@ namesis(Datum *left, Datum *right) result->names.toks = emalloc(sizeof(Datum*) * result->names.ntoks); for(int i = 0; i < result->names.ntoks; i++){ result->names.toks[i] = left->stmt.toks[i]; - incdatumref(result->names.toks[i]); + incref(result->names.toks[i]); } return result; } @@ -286,7 +287,7 @@ assign(Datum *left, Datum *right) symbol = getsym(symbol->name, 1); freedatum(symbol->value); symbol->value = right; - incdatumref(right); + incref(right); if(symbol->value->tag == ArrayTag) SetStrand(symbol->value->array, 0); } @@ -307,7 +308,7 @@ assign(Datum *left, Datum *right) locations[nlocs-1]->names.ntoks = 1; locations[nlocs-1]->names.toks = emalloc(sizeof(Datum*)); locations[nlocs-1]->names.toks[0] = loc; - incdatumref(loc); + incref(loc); }else if(loc->tag == LParTag){ i++; nlocs++; @@ -317,12 +318,12 @@ assign(Datum *left, Datum *right) locations[nlocs-1]->names.toks = emalloc(sizeof(Datum*) * loc->stmt.ntoks); for(int j = 0; j < loc->stmt.ntoks; j++){ locations[nlocs-1]->names.toks[j] = loc->stmt.toks[j]; - incdatumref(loc->stmt.toks[j]); + incref(loc->stmt.toks[j]); } } } - if(GetRank(right->array) == 1 && GetSize(right->array) != nlocs) + if(GetRank(right->array) == 1 && right->array->size != nlocs) throwerror(nil, ELength); for(int i = 0; i < nlocs; i++){ @@ -341,7 +342,7 @@ assign(Datum *left, Datum *right) } } Datum *result = right; - incdatumref(right); + incref(right); result->shy = 1; return result; } @@ -359,7 +360,7 @@ monadop(Datum *left, Datum *right) result->func.operator.code = right->hybrid; } result->func.operator.left = left; - incdatumref(left); + incref(left); result->func.left = nil; result->func.scope = right->operator.scope; return result; @@ -372,7 +373,7 @@ dyadop(Datum *left, Datum *right) Datum *result = allocdatum(MonadicOpTag, 0); result->operator = dupoperator(left->operator); result->operator.right = right; - incdatumref(right); + incref(right); return result; } diff --git a/functions.c b/functions.c index 102590d..3112e06 100644 --- a/functions.c +++ b/functions.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -153,7 +154,7 @@ runfunc(Function f, Array *left, Array *right) if(dfnres->tag != ArrayTag || result == nil) result = mkscalarint(0); /* Very stupid */ else - incarrayref(result); + incref(result); freedatum(dfnres); }else if(f.type == FunctypePrim){ if(left){ @@ -360,11 +361,11 @@ fnFactorial(Array *right) Array *result = nil; if(GetType(right) == AtypeArray){ result = duparrayshape(right, AtypeArray); - for(int i = 0; i < GetSize(right); i++) + for(int i = 0; i < right->size; i++) result->arraydata[i] = fnFactorial(right->arraydata[i]); }else if(GetType(right) == AtypeInt){ result = duparrayshape(right, AtypeFloat); - for(int i = 0; i < GetSize(result); i++){ + for(int i = 0; i < result->size; i++){ double x = 1; vlong n = right->intdata[i]; if(n < 0) @@ -386,21 +387,21 @@ fnRoll(Array *right) Array *result = nil; if(GetType(right) == AtypeArray){ result = duparrayshape(right, AtypeArray); - for(int i = 0; i < GetSize(right); i++) + for(int i = 0; i < right->size; i++) result->arraydata[i] = fnRoll(right->arraydata[i]); }else if(GetType(right) == AtypeInt){ - if(GetSize(right) == 0) + if(right->size == 0) return duparrayshape(right, AtypeInt); int io = globalIO(); vlong lowest = right->intdata[0]; - for(int i = 1; i < GetSize(right); i++) + for(int i = 1; i < right->size; i++) if(right->intdata[i] < lowest) lowest = right->intdata[i]; if(lowest < 0) throwerror(nil, EDomain); else if(lowest == 0){ result = duparrayshape(right, AtypeFloat); - for(int i = 0; i < GetSize(right); i++){ + for(int i = 0; i < right->size; i++){ vlong max = right->intdata[i]; if(max == 0) do{ @@ -411,7 +412,7 @@ fnRoll(Array *right) } }else{ result = duparrayshape(right, AtypeInt); - for(int i = 0; i < GetSize(right); i++) + for(int i = 0; i < right->size; i++) result->intdata[i] = io + lrand() % right->intdata[i]; } }else @@ -441,12 +442,12 @@ fnFloor(Array *right) break; case AtypeFloat: res = duparrayshape(right, AtypeInt); - for(int i = 0; i < GetSize(right); i++) + for(int i = 0; i < right->size; i++) res->intdata[i] = floor(right->floatdata[i]); break; case AtypeArray: res = duparrayshape(right, AtypeArray); - for(int i = 0; i < GetSize(right); i++) + for(int i = 0; i < right->size; i++) res->arraydata[i] = fnFloor(right->arraydata[i]); break; default: @@ -458,7 +459,7 @@ fnFloor(Array *right) Array * fnSame(Array *right) { - incarrayref(right); + incref(right); return right; } @@ -471,7 +472,7 @@ fnUniqueMask(Array *right) res->intdata[0] = 1; return res; } - if(GetSize(right) == 0){ + if(right->size == 0){ Array *res = allocarray(AtypeInt, 1, 0); res->shape[0] = 0; return res; @@ -501,12 +502,12 @@ fnMix(Array *right) { if(GetType(right) != AtypeArray) return fnSame(right); - if(GetSize(right) == 0) + if(right->size == 0) return rundfn(L"((⍴⍵),⍴⊃⍵)⍴⊃⍵", nil, nil, nil, right); int commonrank = 0; int i,j; - for(i = 0; i < GetSize(right); i++) + for(i = 0; i < right->size; i++) if(GetRank(right->arraydata[i]) > commonrank) commonrank = GetRank(right->arraydata[i]); @@ -515,7 +516,7 @@ fnMix(Array *right) for(i = 0; i < commonrank; i++) commonshape->intdata[i] = 0; - for(i = 0; i < GetSize(right); i++){ + for(i = 0; i < right->size; i++){ Array *a = right->arraydata[i]; for(j = 0; j < GetRank(a); j++){ if(a->shape[GetRank(a)-1-j] > commonshape->intdata[commonrank-1-j]) @@ -527,7 +528,7 @@ fnMix(Array *right) int commonsize = 1; for(i = 0; i < GetRank(right); i++) size *= right->shape[i]; - for(i = 0; i < GetSize(commonshape); i++){ + for(i = 0; i < commonshape->size; i++){ size *= commonshape->intdata[i]; commonsize *= commonshape->intdata[i]; } @@ -535,7 +536,7 @@ fnMix(Array *right) Array *result = allocarray(AtypeArray, GetRank(right) + commonrank, size); for(i = 0; i < GetRank(right); i++) result->shape[i] = right->shape[i]; - for(j = 0; j < GetSize(commonshape); j++) + for(j = 0; j < commonshape->size; j++) result->shape[i+j] = commonshape->intdata[j]; int *index = emalloc(sizeof(int) * commonrank); @@ -548,10 +549,10 @@ fnMix(Array *right) result->arraydata[i*commonsize] = a->arraydata[0]; else result->arraydata[i*commonsize] = a; - incarrayref(result->arraydata[i*commonsize]); + incref(result->arraydata[i*commonsize]); for(j = 1; j < commonsize; j++){ result->arraydata[i*commonsize+j] = fill; - incarrayref(fill); + incref(fill); } }else{ for(j = 0; j < commonrank; j++) @@ -561,19 +562,19 @@ fnMix(Array *right) int nfill = commonshape->intdata[commonrank-1-k] - a->shape[GetRank(a)-1-k]; while(nfill--){ result->arraydata[i*commonsize+offset] = fill; - incarrayref(fill); + incref(fill); offset++; } index[commonrank-1-k] = 0; index[commonrank-2-k]++; } - if(j < GetSize(a)){ + if(j < a->size){ result->arraydata[i*commonsize+offset] = arrayitem(a, j); offset++; index[commonrank-1]++; }else if(offset < commonsize){ result->arraydata[i*commonsize+offset] = fill; - incarrayref(fill); + incref(fill); offset++; } } @@ -590,7 +591,7 @@ fnSplit(Array *right) { Rune *code = L"0≡≢⍴⍵: ⍵ ⋄ 1≡≢⍴⍵: ⊂⍵ ⋄ (⊂⍵)⌷⍨¨⍳¯1↓⍴⍵"; Array *result = rundfn(code, nil, nil, nil, right); - if(GetSize(result) == 0 && GetType(result) == AtypeArray) + if(result->size == 0 && GetType(result) == AtypeArray) result->prototype = rundfn(L"⊂(⊃⌽⍴⍵)↑⎕proto ⍵", nil, nil, nil, right); return result; } @@ -598,7 +599,7 @@ fnSplit(Array *right) Array * fnEnclose(Array *right) { - incarrayref(right); + incref(right); if(simplescalar(right)) return right; else{ @@ -611,7 +612,7 @@ fnEnclose(Array *right) Array * fnDisclose(Array *right) { - if(GetSize(right) == 0){ + if(right->size == 0){ Array *fill = fillelement(right); Array *res = fnDisclose(fill); freearray(fill); @@ -691,7 +692,7 @@ fnIndexGenerator(Array *right) for(vlong i = 0; i < n; i++) result->intdata[i] = i + io; }else if(GetRank(right) == 1){ - if(GetSize(right) == 0) + if(right->size == 0) return rundfn(L"⊂⍬", nil, nil, nil, nil); else result = rundfn(L"⊃⍣(0<≢⍵),⌾⌿⍳¨⍵", nil, nil, nil, right); @@ -709,13 +710,13 @@ fnWhere(Array *right) Array * fnEnlist(Array *right) { - if(GetSize(right) == 0) + if(right->size == 0) return fnSame(right); if(GetType(right) != AtypeArray) return fnRavel(right); else{ Array *res = fnEnlist(right->arraydata[0]); - for(int i = 1; i < GetSize(right); i++){ + for(int i = 1; i < right->size; i++){ Array *old = res; Array *tmp = fnEnlist(right->arraydata[i]); res = fnCatenateFirst(res, tmp); @@ -731,7 +732,7 @@ fnUnique(Array *right) { if(GetRank(right) == 0) return fnRavel(right); - if(GetSize(right) == 0) + if(right->size == 0) return fnSame(right); return rundfn(L"(≠⍵)⌿⍵", nil, nil, nil, right); @@ -743,7 +744,7 @@ fnNot(Array *right) if(GetType(right) != AtypeInt) throwerror(nil, EDomain); Array *res = duparray(right); - for(int i = 0; i < GetSize(res); i++){ + for(int i = 0; i < res->size; i++){ if(res->intdata[i] == 0) res->intdata[i] = 1; else if(res->intdata[i] == 1) @@ -762,7 +763,7 @@ fnRavel(Array *right) Array *res = duparray(right); SetRank(res, 1); res->shape = erealloc(res->shape, sizeof(*res->shape) * 1); - res->shape[0] = GetSize(res); + res->shape[0] = res->size; return res; } @@ -773,7 +774,7 @@ fnTable(Array *right) SetRank(res, 2); res->shape = erealloc(res->shape, sizeof(*res->shape) * 2); res->shape[0] = GetRank(right) ? res->shape[0] : 1; - res->shape[1] = GetSize(right) / res->shape[0]; + res->shape[1] = right->size / res->shape[0]; return res; } @@ -818,7 +819,7 @@ fnReverseFirst(Array *right) Array *res = duparray(right); int cells = res->shape[0]; - int elems = GetSize(res) / cells; + int elems = res->size / cells; for(int i = 0; i < cells; i++) memcpy( res->rawdata + i * elems * datasizes[GetType(res)], @@ -845,7 +846,7 @@ fnTranspose(Array *right) accTo *= right->shape[i]; } - for(from = 0; from < GetSize(right); from++){ + for(from = 0; from < right->size; from++){ to = 0; int tmp = from; for(int i = GetRank(right)-1; i >= 0; i--){ @@ -904,7 +905,7 @@ fnFormat(Array *right) if(right->shape[GetRank(right)-1] == 0) size = 0; else - size = maxwidth * GetSize(right) / right->shape[GetRank(right)-1]; + size = maxwidth * right->size / right->shape[GetRank(right)-1]; Array *result = allocarray(AtypeRune, GetRank(right), size); result->shape[GetRank(result)-1] = maxwidth; @@ -949,7 +950,7 @@ Array *name(Array *left, Array *right){\ Array *res;\ if(nested){\ res = duparrayshape(left, AtypeArray);\ - for(int i = 0; i < GetSize(left); i++){\ + for(int i = 0; i < left->size; i++){\ Array *l = arrayitem(left, i);\ Array *r = arrayitem(right, i);\ res->arraydata[i] = name(l,r);\ @@ -958,7 +959,7 @@ Array *name(Array *left, Array *right){\ }\ }else{\ res = duparray(left);\ - for(int i = 0; i < GetSize(left); i++)\ + for(int i = 0; i < left->size; i++)\ switch(GetType(left)){\ default: throwerror(nil, EDomain); break;\ cases\ @@ -1096,7 +1097,7 @@ fnDeal(Array *left, Array *right) { if(GetType(left) != AtypeInt || GetType(right) != AtypeInt) throwerror(nil, EDomain); - if(GetSize(left) != 1 || GetSize(right) != 1) + if(left->size != 1 || right->size != 1) throwerror(nil, ELength); vlong x = left->intdata[0]; vlong y = right->intdata[0]; @@ -1171,7 +1172,7 @@ Array * fnLeft(Array *left, Array *right) { USED(right); - incarrayref(left); + incref(left); return left; } @@ -1179,7 +1180,7 @@ Array * fnRight(Array *left, Array *right) { USED(left); - incarrayref(right); + incref(right); return right; } @@ -1316,27 +1317,27 @@ fnTake(Array *left, Array *right) if(GetRank(right) == 0){ right = duparray(right); - SetRank(right, GetSize(left)); + SetRank(right, left->size); right->shape = erealloc(right->shape, sizeof(*right->shape) * GetRank(right)); for(i = 0; i < GetRank(right); i++) right->shape[i] = 1; }else right = fnSame(right); - if(GetSize(left) > GetRank(right)) + if(left->size > GetRank(right)) throwerror(nil, ELength); - else if(GetSize(left) == GetRank(right)) + else if(left->size == GetRank(right)) left = fnSame(left); else{ Array *old = left; left = fnShape(right); - for(i = 0; i < GetSize(old); i++) + for(i = 0; i < old->size; i++) left->intdata[i] = old->intdata[i]; } - int *shape = emalloc(sizeof(int) * GetSize(left)); + int *shape = emalloc(sizeof(int) * left->size); int size = 1; - for(i = 0; i < GetSize(left); i++){ + for(i = 0; i < left->size; i++){ int s = left->intdata[i]; shape[i] = s < 0 ? -s : s; size *= shape[i]; @@ -1350,17 +1351,17 @@ fnTake(Array *left, Array *right) if(size == 0 && GetType(result) == AtypeArray) result->prototype = fnSame(fill); - int *index = emallocz(sizeof(int) * GetSize(left), 1); + int *index = emallocz(sizeof(int) * left->size, 1); int fromindex; for(i = 0; i < size; i++){ - for(int j = GetSize(left)-1; index[j] == shape[j]; j--){ + for(int j = left->size-1; index[j] == shape[j]; j--){ index[j] = 0; index[j-1]++; } int inside = 1; fromindex = 0; - for(int j = 0; j < GetSize(left) && inside; j++){ + for(int j = 0; j < left->size && inside; j++){ vlong n = left->intdata[j]; vlong m = index[j]; if(n > 0 && m >= right->shape[j]) @@ -1385,8 +1386,8 @@ fnTake(Array *left, Array *right) memcpy(result->rawdata + i*datasizes[GetType(result)], fill->rawdata, datasizes[GetType(result)]); if(GetType(result) == AtypeArray) - incarrayref(result->arraydata[i]); - index[GetSize(left)-1]++; + incref(result->arraydata[i]); + index[left->size-1]++; } free(shape); @@ -1408,27 +1409,27 @@ fnDrop(Array *left, Array *right) if(GetRank(right) == 0){ right = duparray(right); - SetRank(right, GetSize(left)); + SetRank(right, left->size); right->shape = erealloc(right->shape, sizeof(*right->shape) * GetRank(right)); for(i = 0; i < GetRank(right); i++) right->shape[i] = 1; }else right = fnSame(right); - if(GetSize(left) > GetRank(right)) + if(left->size > GetRank(right)) throwerror(nil, ELength); - else if(GetSize(left) == GetRank(right)) + else if(left->size == GetRank(right)) left = duparray(left); else{ Array *old = left; left = allocarray(AtypeInt, 1, GetRank(right)); - left->shape[0] = GetSize(left); - for(i = 0; i < GetSize(old); i++) + left->shape[0] = left->size; + for(i = 0; i < old->size; i++) left->intdata[i] = old->intdata[i]; - for(; i < GetSize(left); i++) + for(; i < left->size; i++) left->intdata[i] = 0; } - for(int i = 0; i < GetSize(left); i++){ + for(int i = 0; i < left->size; i++){ vlong n = left->intdata[i]; vlong m = right->shape[i]; if(n > m || (-n) > m) @@ -1449,25 +1450,25 @@ fnPick(Array *left, Array *right) { if(GetRank(left) > 1) throwerror(nil, ERank); - if(GetSize(left) == 0) + if(left->size == 0) return fnSame(right); int io = globalIO(); Array *result = fnSame(right); - for(int i = 0; i < GetSize(left); i++){ + for(int i = 0; i < left->size; i++){ Array *ix = arrayitem(left, i); if(GetRank(ix) > 1) throwerror(nil, ERank); - if(GetSize(ix) != GetRank(result)) + if(ix->size != GetRank(result)) throwerror(nil, ERank); int index = 0; - for(int j = 0; j < GetSize(ix); j++){ + for(int j = 0; j < ix->size; j++){ int add = ix->intdata[j] - io; - for(int k = j+1; k < GetSize(ix); k++) + for(int k = j+1; k < ix->size; k++) add *= result->shape[k]; index += add; } - if(index >= GetSize(result)) + if(index >= result->size) throwerror(nil, EIndex); Array *tmp = result; result = arrayitem(result, index); @@ -1485,7 +1486,7 @@ fnPartition(Array *left, Array *right) throwerror(nil, ERank); if(GetType(left) != AtypeInt) throwerror(nil, EDomain); - if(GetSize(left) != right->shape[0]) + if(left->size != right->shape[0]) throwerror(nil, ELength); if(right->shape[0] == 0) return fnSame(right); /* TODO figure out if this is correct */ @@ -1498,8 +1499,8 @@ fnPartition(Array *left, Array *right) int start = -1; int io = globalIO(); int active = 0; - for(i = 0; i <= GetSize(left); i++){ - vlong n = i == GetSize(left) ? 0 : left->intdata[i]; + for(i = 0; i <= left->size; i++){ + vlong n = i == left->size ? 0 : left->intdata[i]; if(n < 0) throwerror(nil, EDomain); if(active && (n == 0 || n > prev)){ @@ -1538,15 +1539,15 @@ fnIndex(Array *left, Array *right) throwerror(nil, ERank); if(GetType(left) != AtypeArray && GetType(left) != AtypeInt) throwerror(nil, EDomain); - if(GetSize(left) > GetRank(right)) + if(left->size > GetRank(right)) throwerror(nil, ELength); /* extend left index vector to full format */ Array *oldleft = left; left = allocarray(AtypeArray, 1, GetRank(right)); left->shape[0] = GetRank(right); - for(i = 0; i < GetSize(left); i++){ - if(i >= GetSize(oldleft)){ + for(i = 0; i < left->size; i++){ + if(i >= oldleft->size){ Array *n = mkscalarint(right->shape[i]); left->arraydata[i] = fnIndexGenerator(n); freearray(n); @@ -1558,12 +1559,12 @@ fnIndex(Array *left, Array *right) Array *sub = oldleft->arraydata[i]; if(GetType(sub) != AtypeInt) throwerror(nil, EDomain); - for(int j = 0; j < GetSize(sub); j++){ + for(int j = 0; j < sub->size; j++){ if(sub->intdata[j] < io || sub->intdata[j] >= io + right->shape[i]) throwerror(nil, EIndex); } left->arraydata[i] = oldleft->arraydata[i]; - incarrayref(left->arraydata[i]); + incref(left->arraydata[i]); } } @@ -1572,7 +1573,7 @@ fnIndex(Array *left, Array *right) int rank = 0; int size = 1; - for(i = 0; i < GetSize(left); i++){ + for(i = 0; i < left->size; i++){ if(GetType(left) == AtypeArray){ Array *tmp = shape; Array *newShape = fnShape(left->arraydata[i]); @@ -1582,7 +1583,7 @@ fnIndex(Array *left, Array *right) } } - for(i = 0; i < GetSize(shape); i++){ + for(i = 0; i < shape->size; i++){ size *= shape->intdata[i]; rank++; } @@ -1592,14 +1593,14 @@ fnIndex(Array *left, Array *right) result->shape[i] = shape->intdata[i]; freearray(shape); int *leftindex = emallocz(sizeof(int) * GetRank(right), 1); - for(i = 0; i < GetSize(result); i++){ - for(int j = GetSize(left)-1; leftindex[j] == GetSize(left->arraydata[j]); j--){ + for(i = 0; i < result->size; i++){ + for(int j = left->size-1; leftindex[j] == left->arraydata[j]->size; j--){ leftindex[j] = 0; leftindex[j-1]++; } int from = 0; - int cellsize = GetSize(right); - for(int j = 0; j < GetSize(left); j++){ + int cellsize = right->size; + for(int j = 0; j < left->size; j++){ cellsize = cellsize / right->shape[j]; from += cellsize * (left->arraydata[j]->intdata[leftindex[j]] - io); } @@ -1608,8 +1609,8 @@ fnIndex(Array *left, Array *right) right->rawdata + from * datasizes[GetType(result)], datasizes[GetType(result)]); if(GetType(result) == AtypeArray) - incarrayref(result->arraydata[i]); - leftindex[GetSize(left)-1]++; + incref(result->arraydata[i]); + leftindex[left->size-1]++; } free(leftindex); freearray(left); @@ -1631,7 +1632,7 @@ fnIntervalIndex(Array *left, Array *right) Array * fnMembership(Array *left, Array *right) { - if(GetSize(right) == 0) + if(right->size == 0) return rundfn(L"(⍴⍵)⍴0", nil, nil, nil, left); if(GetRank(right) == 0) @@ -1651,15 +1652,15 @@ fnFind(Array *left, Array *right) } if(GetRank(left) < GetRank(right)){ - Array *newleft = allocarray(GetType(left), GetRank(right), GetSize(left)); + Array *newleft = allocarray(GetType(left), GetRank(right), left->size); for(i = 0; i < GetRank(right) - GetRank(left); i++) newleft->shape[i] = 1; for(j = 0; j < GetRank(left); j++) newleft->shape[i+j] = left->shape[j]; - memcpy(newleft->rawdata, left->rawdata, datasizes[GetType(left)] * GetSize(left)); + memcpy(newleft->rawdata, left->rawdata, datasizes[GetType(left)] * left->size); if(GetType(left) == AtypeArray) - for(i = 0; i < GetSize(left); i++) - incarrayref(newleft->arraydata[i]); + for(i = 0; i < left->size; i++) + incref(newleft->arraydata[i]); left = newleft; }else left = fnSame(left); @@ -1776,10 +1777,10 @@ fnCatenateFirst(Array *left, Array *right) /* TODO reduce duplicated code between copies from left and right */ /* Copy data from the left array */ - for(i = 0, j = 0; i < GetSize(leftarr); i++, j++){ + for(i = 0, j = 0; i < leftarr->size; i++, j++){ if(type == AtypeArray && GetType(leftarr) == AtypeArray){ result->arraydata[j] = leftarr->arraydata[i]; - incarrayref(result->arraydata[j]); + incref(result->arraydata[j]); }else if(type == AtypeArray && GetType(leftarr) != AtypeArray){ result->arraydata[j] = arrayitem(leftarr, i); }else{ @@ -1791,10 +1792,10 @@ fnCatenateFirst(Array *left, Array *right) } /* Copy data from the right array */ - for(i = 0; i < GetSize(rightarr); i++, j++){ + for(i = 0; i < rightarr->size; i++, j++){ if(type == AtypeArray && GetType(rightarr) == AtypeArray){ result->arraydata[j] = rightarr->arraydata[i]; - incarrayref(result->arraydata[j]); + incref(result->arraydata[j]); }else if(type == AtypeArray && GetType(rightarr) != AtypeArray){ result->arraydata[j] = arrayitem(rightarr, i); }else{ @@ -1808,7 +1809,7 @@ fnCatenateFirst(Array *left, Array *right) freearray(leftarr); freearray(rightarr); - if(GetSize(result) == 0 && GetType(result) == AtypeArray) + if(result->size == 0 && GetType(result) == AtypeArray) result->prototype = fillelement(left); return result; } @@ -1819,22 +1820,22 @@ fnReshape(Array *left, Array *right) vlong size = 1; int i; char *p; - for(i = 0; i < GetSize(left); i++) + for(i = 0; i < left->size; i++) size *= left->intdata[i]; - if(GetSize(left) == 0) + if(left->size == 0) return arrayitem(right, 0); - Array *res = allocarray(GetType(right), GetSize(left), size); + Array *res = allocarray(GetType(right), left->size, size); if(size == 0 && GetType(res) == AtypeArray) res->prototype = fillelement(right); - for(i = 0; i < GetSize(left); i++) + for(i = 0; i < left->size; i++) res->shape[i] = left->intdata[i]; for(i = 0, p = res->rawdata; i < size; i++, p += datasizes[GetType(res)]) - memcpy(p, right->rawdata + (datasizes[GetType(res)] * (i % GetSize(right))), datasizes[GetType(res)]); + memcpy(p, right->rawdata + (datasizes[GetType(res)] * (i % right->size)), datasizes[GetType(res)]); if(GetType(res) == AtypeArray) - for(i = 0; i < GetSize(res); i++) - incarrayref(res->arraydata[i]); + for(i = 0; i < res->size; i++) + incref(res->arraydata[i]); return res; } @@ -1850,16 +1851,16 @@ fnRotateFirst(Array *left, Array *right) if(GetType(left) != AtypeInt) throwerror(nil, EDomain); - if(GetRank(right) == 0 || GetSize(right) < 2) + if(GetRank(right) == 0 || right->size < 2) return fnSame(right); int i, j; - if(GetSize(left) == 1 && GetRank(right) != 0){ + if(left->size == 1 && GetRank(right) != 0){ vlong v = left->intdata[0]; - left = allocarray(AtypeInt, GetRank(right)-1, GetSize(right) / right->shape[0]); + left = allocarray(AtypeInt, GetRank(right)-1, right->size / right->shape[0]); for(i = 0; i < GetRank(left); i++) left->shape[i] = right->shape[i+1]; - for(i = 0; i < GetSize(left); i++) + for(i = 0; i < left->size; i++) left->intdata[i] = v; }else left = fnSame(left); @@ -1871,16 +1872,16 @@ fnRotateFirst(Array *left, Array *right) throwerror(nil, ELength); int n = right->shape[0]; - for(i = 0; i < GetSize(left); i++) + for(i = 0; i < left->size; i++) while(left->intdata[i] < 0) left->intdata[i] += n; Array *result = duparray(right); for(i = 0; i < n; i++){ - for(j = 0; j < GetSize(left); j++){ + for(j = 0; j < left->size; j++){ vlong rot = left->intdata[j]; - vlong from = j + ((i+rot)%n)*GetSize(left); - vlong to = j + i*GetSize(left); + vlong from = j + ((i+rot)%n)*left->size; + vlong to = j + i*left->size; memcpy(result->rawdata + to*datasizes[GetType(right)], right->rawdata + from*datasizes[GetType(right)], datasizes[GetType(right)]); @@ -1904,7 +1905,7 @@ fnSelfReference2(Array *left, Array *right) Array * fnSend(Array *left, Array *right) { - if(GetSize(right) != 1) + if(right->size != 1) throwerror(nil, ELength); if(GetType(right) != AtypeInt) throwerror(nil, EDomain); @@ -1956,10 +1957,10 @@ indexOfHelper(Array *left, Array *right, int interval) result->shape[i] = right->shape[i]; /* Reshape Y to allow easy picking of sub-arrays */ Array *rightshape = allocarray(AtypeInt, 1, GetRank(right) - rank + 1); - rightshape->shape[0] = GetSize(rightshape); + rightshape->shape[0] = rightshape->size; rightshape->intdata[0] = size; - for(i = 0; i < GetSize(rightshape) - 1; i++) - rightshape->intdata[GetSize(rightshape)-1-i] = right->shape[GetRank(right)-1-i]; + for(i = 0; i < rightshape->size - 1; i++) + rightshape->intdata[rightshape->size-1-i] = right->shape[GetRank(right)-1-i]; right = fnReshape(rightshape, right); Array **lefts = emalloc(sizeof(Array *) * n); diff --git a/hybrids.c b/hybrids.c index 243faa4..36b0a4d 100644 --- a/hybrids.c +++ b/hybrids.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -42,31 +43,31 @@ fnReplicateFirst(Array *left, Array *right) throwerror(nil, ERank); /* Reshape right if scalar */ - if(GetSize(right) == 1){ + if(right->size == 1){ Array *shape = GetRank(right) == 0 ? mkscalarint(1) : fnShape(right); - shape->intdata[0] = GetSize(left); + shape->intdata[0] = left->size; right = fnReshape(shape, right); freearray(shape); }else right = fnSame(right); int nsize = right->shape[0]; - int cellsize = nsize == 0 ? 1 : GetSize(right) / nsize; + int cellsize = nsize == 0 ? 1 : right->size / nsize; int i; - if(GetSize(left) == 1){ + if(left->size == 1){ Array *shape = mkscalarint(nsize); left = fnReshape(shape, left); }else left = fnSame(left); - if(GetSize(left) != nsize){ + if(left->size != nsize){ freearray(left); freearray(right); throwerror(nil, ELength); } nsize = 0; - for(i = 0; i < GetSize(left); i++){ + for(i = 0; i < left->size; i++){ vlong n = left->intdata[i]; nsize += n > 0 ? n : -n; } @@ -95,7 +96,7 @@ fnReplicateFirst(Array *left, Array *right) } if(GetType(result) == AtypeArray) for(int j = 0; j < npos*cellsize; j++) - incarrayref(result->arraydata[to*cellsize+j]); + incref(result->arraydata[to*cellsize+j]); } freearray(fill); freearray(left); @@ -116,7 +117,7 @@ fnExpandFirst(Array *left, Array *right) int npos = 0; int nsize = 0; int i; - for(i = 0; i < GetSize(left); i++){ + for(i = 0; i < left->size; i++){ if(left->intdata[i] > 0) npos++; else if(left->intdata[i] == 0) @@ -129,7 +130,7 @@ fnExpandFirst(Array *left, Array *right) if(right->shape[0] != 1 && right->shape[0] != npos) throwerror(nil, ELength); - vlong cellsize = right->shape[0] == 0 ? 1 : GetSize(right) / right->shape[0]; + vlong cellsize = right->shape[0] == 0 ? 1 : right->size / right->shape[0]; Array *result = allocarray(GetType(right), GetRank(right), nsize * cellsize); result->shape[0] = nsize; for(i = 1; i < GetRank(result); i++) @@ -153,7 +154,7 @@ fnExpandFirst(Array *left, Array *right) } if(GetType(result) == AtypeArray) for(int j = 0; j < npos*cellsize; j++) - incarrayref(result->arraydata[to*cellsize+j]); + incref(result->arraydata[to*cellsize+j]); if(right->shape[0] != 1 && !neg) from++; } @@ -194,7 +195,7 @@ opReduceFirst(Datum *lefto, Array *left, Array *right) if(left){ if(GetType(left) != AtypeInt) throwerror(nil, EDomain); - if(GetSize(left) != 1) + if(left->size != 1) throwerror(nil, ELength); vlong winsize = left->intdata[0]; if(winsize > right->shape[0]) @@ -215,7 +216,7 @@ opReduceFirst(Datum *lefto, Array *left, Array *right) if(n == 0) throwerror(L"Can't figure out identity element", ENotImplemented); - Array *result = allocarray(AtypeArray, GetRank(right) - 1, GetSize(right) / n); + Array *result = allocarray(AtypeArray, GetRank(right) - 1, right->size / n); for(int i = 0; i < GetRank(right)-1; i++) result->shape[i] = right->shape[i+1]; @@ -224,11 +225,11 @@ opReduceFirst(Datum *lefto, Array *left, Array *right) Array *tmp = mkscalarint(n); index->arraydata[0] = fnIndexGenerator(tmp); freearray(tmp); - for(int i = 1; i < GetSize(index); i++) + for(int i = 1; i < index->size; i++) index->arraydata[i] = mkscalarint(io); - for(int i = 0; i < GetSize(result); i++){ - for(int j = GetSize(index) - 1; index->arraydata[j]->intdata[0] == io + right->shape[j]; j--){ + for(int i = 0; i < result->size; i++){ + for(int j = index->size - 1; index->arraydata[j]->intdata[0] == io + right->shape[j]; j--){ index->arraydata[j]->intdata[0] = io; index->arraydata[j-1]->intdata[0]++; } @@ -242,7 +243,7 @@ opReduceFirst(Datum *lefto, Array *left, Array *right) freearray(argR); } freearray(vector); - index->arraydata[GetSize(index)-1]->intdata[0]++; + index->arraydata[index->size-1]->intdata[0]++; } freearray(index); @@ -257,7 +258,7 @@ opScanFirst(Datum *lefto, Array *left, Array *right) Array *result = duparrayshape(right, AtypeArray); int n = result->shape[0]; - int m = GetSize(result) / n; + int m = result->size / n; for(int i = 0; i < n; i++){ Array *len = mkscalarint(i + 1); Array *index = fnIndexGenerator(len); diff --git a/inverse.c b/inverse.c index c20e78d..20628e4 100644 --- a/inverse.c +++ b/inverse.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" diff --git a/lexer.c b/lexer.c index f684e51..198bd42 100644 --- a/lexer.c +++ b/lexer.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" diff --git a/main.c b/main.c index d921810..034fdff 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "apl9.h" @@ -78,7 +79,7 @@ evalline(Rune *line, Biobuf *bio, int toplevel) Datum *result = eval(stmts, toplevel); if(result) - incdatumref(result); + incref(result); Statement *s = stmts; while(s != nil){ diff --git a/memory.c b/memory.c index 5dd879b..9f333e6 100644 --- a/memory.c +++ b/memory.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "apl9.h" @@ -56,18 +57,13 @@ freearray(Array *a) { if(a == nil) return; - if(GetRefs(a) == 0){ - print("NEGATIVE REF COUNT (array)! %p\n", a); - exits(nil); - } - SetRefs(a, GetRefs(a)-1); - if(GetRefs(a) == 0){ + if(decref(a) == 0){ if(GetType(a) == AtypeArray){ - for(int i = 0; i < GetSize(a); i++) + for(int i = 0; i < a->size; i++) freearray(a->arraydata[i]); } - if(GetType(a) == AtypeArray && GetSize(a) == 0) + if(GetType(a) == AtypeArray && a->size == 0) freearray(a->prototype); else free(a->rawdata); @@ -83,26 +79,20 @@ allocarray(arrayDataType t, int rank, int size) Array *a = emalloc(sizeof(Array)); SetRank(a, rank); SetType(a, t); - SetSize(a, size); SetStrand(a, 0); - SetRefs(a, 1); + a->ref = 1; + a->size = size; a->shape = emalloc(sizeof(*a->shape) * rank); a->rawdata = emalloc(datasizes[t] * size); arrayalloccounts++; return a; } -void -incarrayref(Array *a) -{ - SetRefs(a, GetRefs(a)+1); -} - Datum * allocdatum(int tag, int shy) { Datum *d = emallocz(sizeof(Datum), 1); - d->refs = 1; + d->ref = 1; d->tag = tag; d->shy = shy; datumalloccounts++; @@ -115,8 +105,7 @@ freedatum(Datum *d) if(d == nil) return; - d->refs--; - if(d->refs == 0){ + if(decref(d)){ /* print("FREE DATUM: %S\n", ppdatum(d)); */ switch(d->tag){ case ArrayTag: @@ -151,18 +140,9 @@ freedatum(Datum *d) } free(d); datumalloccounts--; - }else if(d->refs < 0){ - print("NEGATIVE REF COUNT (datum)! %p\n", d); - exits(nil); } } -void -incdatumref(Datum *d) -{ - d->refs++; -} - void freefunction(Function f) { @@ -225,7 +205,7 @@ dupfunction(Function f) { Function g = f; if(g.left) - incarrayref(g.left); + incref(g.left); switch(f.type){ case FunctypeDfn: @@ -261,9 +241,9 @@ dupoperator(Operator o) Operator p = o; if(p.left) - incdatumref(p.left); + incref(p.left); if(p.right) - incdatumref(p.right); + incref(p.right); switch(o.type){ case OperatortypeDop: p.dop = runestrdup(o.dop); diff --git a/operators.c b/operators.c index 20c8611..8f644ad 100644 --- a/operators.c +++ b/operators.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -52,10 +53,10 @@ opEach(Datum *lefto, Array *left, Array *right) /* TODO handle empty arrays by applying the function to their prototype */ - Array *result = allocarray(AtypeArray, GetRank(rightarr), GetSize(rightarr)); + Array *result = allocarray(AtypeArray, GetRank(rightarr), rightarr->size); for(i = 0; i < GetRank(rightarr); i++) result->shape[i] = rightarr->shape[i]; - for(i = 0; i < GetSize(rightarr); i++){ + for(i = 0; i < rightarr->size; i++){ Array *elem1 = leftarr ? arrayitem(leftarr, i) : nil; Array *elem2 = arrayitem(rightarr, i); result->arraydata[i] = runfunc(lefto->func, elem1, elem2); @@ -71,7 +72,7 @@ Array * opSwitch(Datum *lefto, Array *left, Array *right) { if(lefto->tag == ArrayTag){ - incarrayref(lefto->array); + incref(lefto->array); return lefto->array; }else if(lefto->tag == FunctionTag){ if(left) @@ -117,9 +118,9 @@ opOuterProduct(Datum *lefto, Array *left, Array *right) for(i = 0; i < rank; i++) result->shape[i] = shape[i]; - for(int leftindex = 0; leftindex < GetSize(left); leftindex++){ - for(int rightindex = 0; rightindex < GetSize(right); rightindex++){ - i = leftindex * GetSize(right) + rightindex; + for(int leftindex = 0; leftindex < left->size; leftindex++){ + for(int rightindex = 0; rightindex < right->size; rightindex++){ + i = leftindex * right->size + rightindex; Array *leftitem = arrayitem(left, leftindex); Array *rightitem = arrayitem(right, rightindex); result->arraydata[i] = runfunc(lefto->func, leftitem, rightitem); @@ -151,17 +152,17 @@ opReceive(Datum *lefto, Array *left, Array *right) throwerror(nil, ESyntax); if(GetType(right) != AtypeInt && GetType(right) != AtypeFloat) throwerror(nil, EDomain); - if(GetSize(right) != 1 && GetSize(right) != 0) + if(right->size != 1 && right->size != 0) throwerror(nil, ELength); int timeout = 0; - if(GetSize(right) == 0) + if(right->size == 0) timeout = -1; else if(GetType(right) == AtypeInt) timeout = right->intdata[0]*1000; else if(GetType(right) == AtypeFloat) timeout = right->floatdata[0]*1000; - if(GetSize(right) == 1 && timeout < 0) + if(right->size == 1 && timeout < 0) throwerror(L"Timeout must be non-negative", EDomain); return messagerecv(lefto->func, timeout); @@ -180,7 +181,7 @@ opPower(Datum *lefto, Datum *righto, Array *left, Array *right) else code = L"next←⍶⍵ ⋄ next⍹⍵:⍵ ⋄ ∇next"; }else if(righto->tag == ArrayTag){ - if(GetType(righto->array) != AtypeInt || GetRank(righto->array) != 0 || GetSize(righto->array) != 1) + if(GetType(righto->array) != AtypeInt || GetRank(righto->array) != 0 || righto->array->size != 1) throwerror(L"right operand to ⍣", EDomain); vlong times = righto->array->intdata[0]; if(times < 0){ @@ -254,10 +255,10 @@ opAtop(Datum *lefto, Datum *righto, Array *left, Array *right) throwerror(nil, ERank); if(GetType(ranks) != AtypeInt) throwerror(nil, EDomain); - if(GetSize(ranks) < 1 || GetSize(ranks) > 3) + if(ranks->size < 1 || ranks->size > 3) throwerror(nil, ELength); int p,q,r; - switch(GetSize(ranks)){ + switch(ranks->size){ case 1: p = q = r = ranks->intdata[0]; break; case 2: q = ranks->intdata[0]; diff --git a/print.c b/print.c index 55da3b3..3143fcc 100644 --- a/print.c +++ b/print.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -51,7 +52,7 @@ ppdatums(Datum **ds, int n) Rune * pparray(Array *a) { - Rune **elemstrs = emalloc(sizeof(Rune *) * GetSize(a)); + Rune **elemstrs = emalloc(sizeof(Rune *) * a->size); int rowcount = 1; if(GetRank(a) > 0){ for(int i = 0; i < GetRank(a)-1; i++) @@ -61,7 +62,7 @@ pparray(Array *a) int boxing = !simplearray(a); char *align = GetType(a) == AtypeArray ? "-" : ""; - for(int i = 0; i < GetSize(a); i++){ + for(int i = 0; i < a->size; i++){ if(GetType(a) == AtypeArray){ Rune *arrstr = pparray(a->arraydata[i]); elemstrs[i] = runesmprint("%S", arrstr); @@ -95,7 +96,7 @@ pparray(Array *a) int lastdim = GetRank(a) ? a->shape[GetRank(a)-1] : 1; int *widths = emallocz(sizeof(int) * lastdim, 1); int *heights = emallocz(sizeof(int) * rowcount, 1); - for(int i = 0; i < GetSize(a); i++){ + for(int i = 0; i < a->size; i++){ int w,h; strdims(elemstrs[i], &w, &h); if(w > widths[i%lastdim]) @@ -151,7 +152,7 @@ pparray(Array *a) res = printborder(res, widths, lastdim, 0); int j = 1; int blanks = 0; - for(int dim = 0; dim < GetRank(a) - 1 && i+1 != GetSize(a); dim++){ + for(int dim = 0; dim < GetRank(a) - 1 && i+1 != a->size; dim++){ j *= a->shape[GetRank(a)-dim-2]; if(i%j == 0) blanks++; 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); diff --git a/symbol.c b/symbol.c index ad06203..61629ee 100644 --- a/symbol.c +++ b/symbol.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "apl9.h" @@ -64,7 +65,7 @@ dupsymbol(Symbol *s) memcpy(new, s, sizeof(Symbol)); new->name = runestrdup(s->name); if(new->value) - incdatumref(new->value); + incref(new->value); return new; } @@ -137,18 +138,18 @@ pushdfnframe(Rune *code, Symtab *scope, Datum *lefto, Datum *righto, Array *left new->lefto = lefto; new->righto = righto; if(lefto) - incdatumref(lefto); + incref(lefto); if(righto) - incdatumref(righto); + incref(righto); if(left){ new->left = allocdatum(ArrayTag, 0); new->left->array = left; - incarrayref(left); + incref(left); }else new->left = nil; new->right = right; if(right) - incarrayref(right); + incref(right); new->prev = td->currentdfn; new->errorguards = nil; @@ -164,13 +165,13 @@ dupdfnframe(DfnFrame *f) new->code = runestrdup(f->code); new->symtab = dupsymtab(f->symtab); if(f->lefto) - incdatumref(f->lefto); + incref(f->lefto); if(f->righto) - incdatumref(f->righto); + incref(f->righto); if(f->left) - incdatumref(f->left); + incref(f->left); if(f->right) - incarrayref(f->right); + incref(f->right); return new; } @@ -252,7 +253,7 @@ getalpha(void) throwerror(nil, ESyntax); else{ res = dfn->left; - incdatumref(res); + incref(res); } return res; } @@ -267,7 +268,7 @@ getomega(void) else{ res = allocdatum(ArrayTag, 0); res->array = dfn->right; - incarrayref(res->array); + incref(res->array); } return res; } @@ -281,7 +282,7 @@ getalphao(void) throwerror(nil, ESyntax); else{ res = dfn->lefto; - incdatumref(res); + incref(res); } return res; } @@ -295,7 +296,7 @@ getomegao(void) throwerror(nil, ESyntax); else{ res = dfn->righto; - incdatumref(res); + incref(res); } return res; } @@ -308,7 +309,7 @@ setalpha(Datum *new) throwerror(nil, ESyntax); if(dfn->left == nil){ dfn->left = new; - incdatumref(new); + incref(new); } } -- cgit v1.2.3