From 60a9104f6b737976e30271ac1c1a432cd9a5c657 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 9 Feb 2022 17:25:28 +0000 Subject: Encode ref count in bit mask too --- apl9.h | 7 ++++--- main.c | 4 ++-- memory.c | 10 +++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apl9.h b/apl9.h index 8135f39..1208cac 100644 --- a/apl9.h +++ b/apl9.h @@ -75,7 +75,7 @@ typedef struct Mail Mail; struct Mixed { - int type; + u8int type; union { vlong i; double f; @@ -87,10 +87,12 @@ struct Mixed #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)) struct Array { @@ -109,10 +111,9 @@ struct Array * 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: unused + * 41-63: 23 bits for reference counting (max refs = 8388608) * NOTE: should only be modified through the macros defined above */ - ulong refs; }; struct Statement diff --git a/main.c b/main.c index 9acd568..c9a0454 100644 --- a/main.c +++ b/main.c @@ -55,10 +55,10 @@ restart: if(result && !result->shy) print("%S\n", ppdatum(result)); freedatum(result); -/* + print("Unfreed arrays: %d\n", arrayalloccounts); print("Unfreed datums: %d\n", datumalloccounts); -*/ + } threadexitsall(nil); } diff --git a/memory.c b/memory.c index 8bca068..f1ff3a3 100644 --- a/memory.c +++ b/memory.c @@ -57,13 +57,13 @@ freearray(Array *a) { if(a == nil) return; - if(a->refs == 0){ + if(GetRefs(a) == 0){ print("NEGATIVE REF COUNT (array)! %p\n", a); threadexitsall(nil); } - a->refs--; - if(a->refs == 0){ + SetRefs(a, GetRefs(a)-1); + if(GetRefs(a) == 0){ if(GetType(a) == AtypeArray) for(int i = 0; i < GetSize(a); i++) freearray(a->arraydata[i]); @@ -82,9 +82,9 @@ allocarray(arrayDataType t, int rank, int size) SetType(a, t); SetSize(a, size); SetStrand(a, 0); + SetRefs(a, 1); a->shape = emalloc(sizeof(*a->shape) * rank); a->rawdata = emalloc(datasizes[t] * size); - a->refs = 1; arrayalloccounts++; return a; } @@ -92,7 +92,7 @@ allocarray(arrayDataType t, int rank, int size) void incarrayref(Array *a) { - a->refs++; + SetRefs(a, GetRefs(a)+1); } Datum * -- cgit v1.2.3