From c4830c0af857c076c86068acd5dc6d8240d837f3 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 22 Sep 2023 10:17:26 -0400 Subject: [PATCH] wip: fix up pointers in `u3n_v2_reclaim` --- pkg/noun/v2/nock.c | 51 ++++++++++++++++++++++++++++++++++++++++++++-- pkg/noun/v2/nock.h | 5 +++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/noun/v2/nock.c b/pkg/noun/v2/nock.c index 66c10288ef..e6ff116ce1 100644 --- a/pkg/noun/v2/nock.c +++ b/pkg/noun/v2/nock.c @@ -20,8 +20,55 @@ u3n_v2_reclaim(void) u3R = (u3a_road*) u3R_v2; // clear the bytecode cache - u3n_v3_free(); - u3R->byc.har_p = u3h_v3_new(); + u3n_v2_free(); + u3R->byc.har_p = u3h_v2_new(); +} + +/* _cn_v2_prog_free(): free memory retained by program pog_u +*/ +static void +_cn_v2_prog_free(u3n_v2_prog* pog_u) +{ + // fix up pointers for loom portability + pog_u->byc_u.ops_y = (c3_y*) ((void*) pog_u) + sizeof(u3n_v2_prog); + pog_u->lit_u.non = (u3_noun*) (pog_u->byc_u.ops_y + pog_u->byc_u.len_w); + pog_u->mem_u.sot_u = (u3n_v2_memo*) (pog_u->lit_u.non + pog_u->lit_u.len_w); + pog_u->cal_u.sit_u = (u3j_v2_site*) (pog_u->mem_u.sot_u + pog_u->mem_u.len_w); + pog_u->reg_u.rit_u = (u3j_v2_rite*) (pog_u->cal_u.sit_u + pog_u->cal_u.len_w); + + c3_w dex_w; + for (dex_w = 0; dex_w < pog_u->lit_u.len_w; ++dex_w) { + u3a_v2_lose(pog_u->lit_u.non[dex_w]); + } + for (dex_w = 0; dex_w < pog_u->mem_u.len_w; ++dex_w) { + u3a_v2_lose(pog_u->mem_u.sot_u[dex_w].key); + } + for (dex_w = 0; dex_w < pog_u->cal_u.len_w; ++dex_w) { + u3j_v2_site_lose(&(pog_u->cal_u.sit_u[dex_w])); + } + for (dex_w = 0; dex_w < pog_u->reg_u.len_w; ++dex_w) { + u3j_v2_rite_lose(&(pog_u->reg_u.rit_u[dex_w])); + } + u3a_v2_free(pog_u); +} + +/* _n_v2_feb(): u3h_v2_walk helper for u3n_v2_free + */ +static void +_n_v2_feb(u3_noun kev) +{ + u3a_v2_cell *cel_u = (u3a_v2_cell*) u3a_v2_to_ptr(kev); + _cn_v2_prog_free(u3to(u3n_v2_prog, cel_u->tel)); +} + +/* u3n_v2_free(): free bytecode cache + */ +void +u3n_v2_free() +{ + u3p(u3h_v2_root) har_p = u3R_v2->byc.har_p; + u3h_v2_walk(har_p, _n_v2_feb); + u3h_v2_free(har_p); } /* u3n_v2_mig_rewrite_compact(): rewrite the bytecode cache for compaction. diff --git a/pkg/noun/v2/nock.h b/pkg/noun/v2/nock.h index 390c40e645..e7d44aee08 100644 --- a/pkg/noun/v2/nock.h +++ b/pkg/noun/v2/nock.h @@ -51,6 +51,11 @@ void u3n_v2_reclaim(void); + /* u3n_v2_free(): free bytecode cache. + */ + void + u3n_v2_free(void); + /* u3n_v2_mig_rewrite_compact(): rewrite bytecode cache for compaction. */ void