From a3d74c6af2b3c9e426d8dfcca7b5912c48e50880 Mon Sep 17 00:00:00 2001 From: dzaima Date: Thu, 16 Mar 2023 22:27:44 +0200 Subject: [PATCH] MUT_APPEND --- src/builtins/fold.c | 4 ++-- src/h.h | 4 +++- src/opt/mm_buddy.h | 4 +--- src/opt/mm_malloc.h | 1 + src/utils/mut.h | 18 +++++++++++++----- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/builtins/fold.c b/src/builtins/fold.c index 2af1b820..e03aa201 100644 --- a/src/builtins/fold.c +++ b/src/builtins/fold.c @@ -338,7 +338,7 @@ B fold_rows(Md1D* fd, B x) { usz *sh = SH(x); usz n = sh[0]; usz m = sh[1]; usz b = (block + m - 1) / m; // Normal block length usz b_max = b + b/4; // Last block max length - MAKE_MUT(r, n); + MAKE_MUT(r, n); MUT_APPEND_INIT(r); BSS2A slice = TI(x,slice); for (usz i=0, im=0; i b_max) { incG(x); l = b; } @@ -348,7 +348,7 @@ B fold_rows(Md1D* fd, B x) { ssh[0] = l; ssh[1] = m; B sr = insert_c1(fd, C1(transp, taga(sl))); - mut_copy(r, i, sr, 0, l); + MUT_APPEND(r, sr, 0, l); decG(sr); i += l; im += sia; } diff --git a/src/h.h b/src/h.h index 335cd0eb..77f8d085 100644 --- a/src/h.h +++ b/src/h.h @@ -236,7 +236,7 @@ typedef union B { #if defined(OBJ_TRACK) #define OBJ_COUNTER 1 #endif -#if DEBUG && !defined(VERIFY_TAIL) && MM!=2 +#if DEBUG && !defined(VERIFY_TAIL) && MM==1 #define VERIFY_TAIL 64 #endif #if ALLOC_STAT || VERIFY_TAIL @@ -322,6 +322,7 @@ typedef struct Arr { #define UD assert(false); extern bool cbqn_noAlloc; NOINLINE void cbqn_NOGC_start(); // function to allow breakpointing + #define NOGC_CHECK if (cbqn_noAlloc && !gc_depth) err("allocating during noalloc"); #define NOGC_S cbqn_NOGC_start() #define NOGC_E cbqn_noAlloc=false #else @@ -331,6 +332,7 @@ typedef struct Arr { #define UD __builtin_unreachable(); #define NOGC_S #define NOGC_E + #define NOGC_CHECK #endif #if WARN_SLOW void warn_slow1(char* s, B x); diff --git a/src/opt/mm_buddy.h b/src/opt/mm_buddy.h index 48a0cd17..d13f63ea 100644 --- a/src/opt/mm_buddy.h +++ b/src/opt/mm_buddy.h @@ -23,10 +23,8 @@ extern EmptyValue* mm_buckets[64]; #if !ALLOC_NOINLINE || ALLOC_IMPL || ALLOC_IMPL_MMX ALLOC_FN void* mm_alloc(u64 sz, u8 type) { assert(sz>=16); + NOGC_CHECK; preAlloc(sz, type); - #if DEBUG - if (cbqn_noAlloc && !gc_depth) err("allocating during noAlloc"); - #endif #if VERIFY_TAIL i64 logAlloc = LOG2(sz + VERIFY_TAIL); #else diff --git a/src/opt/mm_malloc.h b/src/opt/mm_malloc.h index edadcf0a..e4c58f0f 100644 --- a/src/opt/mm_malloc.h +++ b/src/opt/mm_malloc.h @@ -25,6 +25,7 @@ static void mm_visitP(void* x) { } bool gc_maybeGC(bool); void gc_forceGC(bool); +#define gc_depth 0 void mm_forHeap(V2v f); void mm_dumpHeap(FILE* f); diff --git a/src/utils/mut.h b/src/utils/mut.h index 52bf684c..f22d9340 100644 --- a/src/utils/mut.h +++ b/src/utils/mut.h @@ -46,27 +46,27 @@ void mut_to(Mut* m, u8 n); #define MAKE_MUT(N, IA) Mut N##_val; N##_val.fns = &mutFns[el_MAX]; N##_val.ia = (IA); Mut* N = &N##_val; static B mut_fv(Mut* m) { assert(m->fns->elType!=el_MAX); - NOGC_E; + NOGC_E; assert(m->ia == m->val->ia); Arr* a = m->val; a->sh = &a->ia; SPRNK(a, 1); return taga(a); } static B mut_fc(Mut* m, B x) { assert(m->fns->elType!=el_MAX); // doesn't consume x - NOGC_E; + NOGC_E; assert(m->ia == m->val->ia); Arr* a = m->val; arr_shCopy(a, x); return taga(a); } static B mut_fcd(Mut* m, B x) { assert(m->fns->elType!=el_MAX); // consumes x - NOGC_E; + NOGC_E; assert(m->ia == m->val->ia); Arr* a = m->val; arr_shCopy(a, x); decG(x); return taga(a); } static Arr* mut_fp(Mut* m) { assert(m->fns->elType!=el_MAX); - NOGC_E; + NOGC_E; assert(m->ia == m->val->ia); return m->val; } @@ -93,7 +93,15 @@ static void mut_fill(Mut* m, usz ms, B x, usz l) { m->fns->m_fill(m, ms, x, l); // doesn't consume; expects x to be an array, each position must be written to precisely once static void mut_copy(Mut* m, usz ms, B x, usz xs, usz l) { assert(isArr(x)); m->fns->m_copy(m, ms, x, xs, l); } - +// MUT_APPEND_INIT must be called immediately after MAKE_MUT or MAKE_MUT_INIT +// after that, the only valid operation on the Mut will be MUT_APPEND +// using this append system will no longer prevent allocations from being done during the lifetime of the Mut +#define MUT_APPEND_INIT(N) ux N##_ci = 0; NOGC_E; +#define MUT_APPEND(N, X, XS, L) ({ ux l_ = (L); NOGC_CHECK; \ + mut_copy(N, N##_ci, X, XS, l_); \ + N##_ci+= l_; \ + if (PTY(N->val) == t_harr) { NOGC_E; N->val->ia = N##_ci; } \ +}) #define MUTG_INIT(N) MutFns N##_mutfns = *N->fns; void* N##_mutarr = N->a // these methods function as the non-G-postfixed ones, except that