Skip to content

Commit

Permalink
wip: update v2 allocate/hashtable/nock
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-levan committed Sep 7, 2023
1 parent 0bbe610 commit aebed34
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 4 deletions.
31 changes: 29 additions & 2 deletions pkg/noun/v2/allocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ u3a_v2_reclaim(void)
u3R->cax.har_p = u3h_new();
}

u3_noun
u3a_v2_rewritten_noun(u3_noun som)
{
if ( c3y == u3a_is_cat(som) ) {
return som;
}
u3_post som_p = u3a_rewritten(u3a_v2_to_off(som));

/* If this is being called during a migration, one-bit pointer compression
needs to be temporarily enabled so the rewritten reference is compressed */
if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 1;

if ( c3y == u3a_is_pug(som) ) {
som_p = u3a_v2_to_pug(som_p);
}
else {
som_p = u3a_v2_to_pom(som_p);
}

/* likewise, pointer compression is disabled until migration is complete */
if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 0;

return som_p;
}

/* u3a_v2_rewrite_compact(): rewrite pointers in ad-hoc persistent road structures.
* XX need to version
*/
Expand Down Expand Up @@ -48,11 +75,11 @@ u3a_v2_rewrite_compact(void)
void
u3a_v2_rewrite_noun(u3_noun som)
{
if ( c3n == u3a_v2_is_cell(som) ) {
if ( c3n == u3a_is_cell(som) ) {
return;
}

if ( c3n == u3a_v2_rewrite_ptr(u3a_v2_to_ptr((som))) ) return;
if ( c3n == u3a_rewrite_ptr(u3a_v2_to_ptr((som))) ) return;

u3a_cell* cel = u3a_v2_to_ptr(som);

Expand Down
14 changes: 13 additions & 1 deletion pkg/noun/v2/allocate.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef U3_ALLOCATE_V2_H
#define U3_ALLOCATE_V2_H

#include "pkg/noun/allocate.h"

#include "error.h"
#include "pkg/noun/v2/manage.h"
#include "options.h"
Expand Down Expand Up @@ -37,7 +39,7 @@
/* u3a_v2_to_ptr(): convert noun [som] into generic pointer into loom.
*/
inline void *u3a_v2_to_ptr(c3_w som) {
return u3a_v2_into(u3a_v2_to_off(som));
return u3a_into(u3a_v2_to_off(som));
}

/* u3a_v2_to_wtr(): convert noun [som] into word pointer into loom.
Expand Down Expand Up @@ -81,4 +83,14 @@
void
u3a_v2_rewrite_noun(u3_noun som);

/* u3a_v2_rewritten(): rewrite a pointer for compaction.
*/
u3_post
u3a_v2_rewritten(u3_post som_p);

/* u3a_v2_rewritten(): rewritten noun pointer for compaction.
*/
u3_noun
u3a_v2_rewritten_noun(u3_noun som);

#endif /* ifndef U3_ALLOCATE_V2_H */
94 changes: 94 additions & 0 deletions pkg/noun/v2/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "pkg/noun/v2/hashtable.h"

#include "pkg/noun/allocate.h"
#include "pkg/noun/v2/allocate.h"
#include "imprison.h"
#include "retrieve.h"
Expand Down Expand Up @@ -170,3 +171,96 @@ u3h_v2_walk(u3p(u3h_root) har_p, void (*fun_f)(u3_noun))
{
u3h_v2_walk_with(har_p, _ch_walk_plain, fun_f);
}

/* _ch_rewrite_buck(): rewrite buck for compaction.
*/
void
_ch_rewrite_buck(u3h_buck* hab_u)
{
if ( c3n == u3a_rewrite_ptr(hab_u) ) return;
c3_w i_w;

for ( i_w = 0; i_w < hab_u->len_w; i_w++ ) {
u3_noun som = u3h_slot_to_noun(hab_u->sot_w[i_w]);
hab_u->sot_w[i_w] = u3h_noun_to_slot(u3a_v2_rewritten_noun(som));
u3a_v2_rewrite_noun(som);
}
}

/* _ch_rewrite_node(): rewrite node for compaction.
*/
void
_ch_rewrite_node(u3h_node* han_u, c3_w lef_w)
{
if ( c3n == u3a_rewrite_ptr(han_u) ) return;

c3_w len_w = _ch_popcount(han_u->map_w);
c3_w i_w;

lef_w -= 5;

for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w sot_w = han_u->sot_w[i_w];

if ( _(u3h_slot_is_noun(sot_w)) ) {
u3_noun kev = u3h_slot_to_noun(sot_w);
han_u->sot_w[i_w] = u3h_noun_to_slot(u3a_v2_rewritten_noun(kev));

u3a_v2_rewrite_noun(kev);
}
else {
void* hav_v = u3h_v2_slot_to_node(sot_w);
u3h_node* nod_u = u3to(u3h_node,u3a_v2_rewritten(u3of(u3h_node,hav_v)));

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 1;

han_u->sot_w[i_w] = u3h_v2_node_to_slot(nod_u);

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 0;

if ( 0 == lef_w ) {
_ch_rewrite_buck(hav_v);
} else {
_ch_rewrite_node(hav_v, lef_w);
}
}
}
}

/* u3h_v2_rewrite(): rewrite pointers during compaction.
*/
void
u3h_v2_rewrite(u3p(u3h_root) har_p)
{
u3h_root* har_u = u3to(u3h_root, har_p);
c3_w i_w;

if ( c3n == u3a_rewrite_ptr(har_u) ) return;

for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];

if ( _(u3h_slot_is_noun(sot_w)) ) {
u3_noun kev = u3h_slot_to_noun(sot_w);
har_u->sot_w[i_w] = u3h_noun_to_slot(u3a_v2_rewritten_noun(kev));

u3a_v2_rewrite_noun(kev);
}
else if ( _(u3h_slot_is_node(sot_w)) ) {
u3h_node* han_u = u3h_v2_slot_to_node(sot_w);
u3h_node* nod_u = u3to(u3h_node, u3a_rewritten(u3of(u3h_node,han_u)));

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 1;

har_u->sot_w[i_w] = u3h_v2_node_to_slot(nod_u);

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 0;

_ch_rewrite_node(han_u, 25);
}
}
}
5 changes: 5 additions & 0 deletions pkg/noun/v2/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
void
u3h_v2_free(u3p(u3h_root) har_p);

/* u3h_v2_rewrite(): rewrite hashtable for compaction.
*/
void
u3h_v2_rewrite(u3p(u3h_v2_root) har_p);

/* u3h_v2_walk_with(): traverse hashtable with key, value fn and data
* argument; RETAINS.
*/
Expand Down
33 changes: 32 additions & 1 deletion pkg/noun/v2/nock.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// @file

#include "pkg/noun/nock.h"
#include "pkg/noun/v2/nock.h"

#include "pkg/noun/v2/hashtable.h"

/* u3n_v2_reclaim(): clear ad-hoc persistent caches to reclaim memory.
** XX need to version
*/
void
u3n_v2_reclaim(void)
Expand All @@ -19,6 +19,37 @@ u3n_v2_reclaim(void)
u3R->byc.har_p = u3h_new();
}

/* _cn_prog_free(): free memory retained by program pog_u
** NB: copy of _cn_prog_free in pkg/noun/nock.c
*/
static void
_cn_prog_free(u3n_prog* pog_u)
{
c3_w dex_w;
for (dex_w = 0; dex_w < pog_u->lit_u.len_w; ++dex_w) {
u3z(pog_u->lit_u.non[dex_w]);
}
for (dex_w = 0; dex_w < pog_u->mem_u.len_w; ++dex_w) {
u3z(pog_u->mem_u.sot_u[dex_w].key);
}
for (dex_w = 0; dex_w < pog_u->cal_u.len_w; ++dex_w) {
u3j_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_rite_lose(&(pog_u->reg_u.rit_u[dex_w]));
}
u3a_free(pog_u);
}


/* _n_feb(): u3h_walk helper for u3n_free
*/
static void
_n_feb(u3_noun kev)
{
_cn_prog_free(u3to(u3n_prog, u3t(kev)));
}

/* u3n_v2_free(): free bytecode cache
*/
void
Expand Down

0 comments on commit aebed34

Please sign in to comment.