About to test garbage collector.

This commit is contained in:
C. Guy Yarvin 2014-10-08 19:20:57 -07:00
parent b64ee70c24
commit 014b032f8e
16 changed files with 273 additions and 50 deletions

145
g/a.c
View File

@ -474,7 +474,7 @@ _me_gain_use(u3_noun dog)
c3_w* dog_w = u3_co_to_ptr(dog);
u3_cs_box* box_u = u3_co_botox(dog_w);
if ( 0xffffffff == box_u->use_w ) {
if ( 0x7fffffff == box_u->use_w ) {
u3_cm_bail(c3__fail);
}
else {
@ -915,27 +915,140 @@ u3_ca_use(u3_noun som)
}
}
/* u3_ca_audit(): anticipate zero usecounts.
/* u3_ca_mark_ptr(): mark a pointer for gc. Produce size.
*/
c3_o
u3_ca_audit(u3_noun som)
c3_w
u3_ca_mark_ptr(void* ptr_v)
{
if ( u3_so(u3_co_is_cat(som)) ) {
return u3_yes;
if ( u3_so(u3_co_is_north(u3R)) ) {
c3_assert((ptr_v >= (void*)u3R->rut_w) && (ptr_v < (void*)u3R->hat_w));
}
if ( u3_so(u3_co_is_senior(u3R, som)) ) {
return u3_yes;
else {
c3_assert((ptr_v >= (void*)u3R->hat_w) && (ptr_v < (void*)u3R->rut_w));
}
if ( 0 == u3_ca_use(som) ) {
u3_cm_p("som", som);
printf("zero: %x/%x\r\n", u3_cr_mug(som), som);
abort();
return u3_no;
{
u3_cs_box* box_u = u3_co_botox(ptr_v);
c3_ws use_ws = (c3_ws)box_u->use_w;
c3_w siz_w;
c3_assert(use_ws != 0);
if ( use_ws < 0 ) {
use_ws -= 1;
siz_w = 0;
}
else {
use_ws = -1;
siz_w = box_u->siz_w;
return 0;
}
box_u->use_w = (c3_w)use_ws;
return siz_w;
}
else if ( u3_so(u3du(som)) && (1 == u3_ca_use(som)) ) {
return u3_and(u3_ca_audit(u3h(som)), u3_ca_audit(u3t(som)));
}
/* u3_ca_mark_noun(): mark a noun for gc. Produce size.
*/
c3_w
u3_ca_mark_noun(u3_noun som)
{
c3_w siz_w = 0;
while ( 1 ) {
if ( u3_so(u3_co_is_senior(u3R, som)) ) {
return siz_w;
}
else {
c3_w* dog_w = u3_co_to_ptr(som);
c3_w new_w = u3_ca_mark_ptr(dog_w);
if ( 0 == new_w ) {
return siz_w;
}
else {
siz_w += new_w;
siz_w += u3_ca_mark_noun(u3h(som));
som = u3t(som);
}
}
}
}
/* u3_ca_sweep(): sweep a fully marked road.
*/
void
u3_ca_sweep(c3_c* cap_c)
{
c3_w neg_w, pos_w, leq_w, tot_w, caf_w;
/* Measure allocated memory by counting the free list.
*/
{
c3_w end_w;
c3_w fre_w = 0;
c3_w i_w;
end_w = u3_so(u3_co_is_north(u3R))
? (u3R->hat_w - u3R->rut_w)
: (u3R->rut_w - u3R->hat_w);
for ( i_w = 0; i_w < u3_cc_fbox_no; i_w++ ) {
u3_cs_fbox* fre_u = u3R->all.fre_u[i_w];
while ( fre_u ) {
fre_w += fre_u->box_u.siz_w;
fre_u = fre_u->nex_u;
}
}
neg_w = (end_w - fre_w);
}
/* Sweep through the arena, repairing and counting leaks.
*/
pos_w = leq_w = 0;
{
c3_w* box_w = u3_so(u3_co_is_north(u3R)) ? u3R->rut_w : u3R->hat_w;
while ( box_w < (u3_so(u3_co_is_north(u3R)) ? u3R->hat_w : u3R->rut_w) ) {
u3_cs_box* box_u = (void *)box_w;
c3_ws use_ws = (c3_ws)box_u->use_w;
if ( use_ws > 0 ) {
leq_w += box_u->siz_w;
box_u->use_w = 0;
_box_attach(box_u);
}
else if ( use_ws < 0 ) {
pos_w += box_u->siz_w;
box_u->use_w = (c3_w)(0 - use_ws);
}
}
}
tot_w = u3_so(u3_co_is_north(u3R))
? u3R->mat_w - u3R->rut_w
: u3R->rut_w - u3R->mat_w;
caf_w = u3_so(u3_co_is_north(u3R))
? u3R->mat_w - u3R->cap_w
: u3R->cap_w - u3R->mat_w;
c3_assert(pos_w == neg_w);
tot_w *= 4;
caf_w *= 4;
pos_w *= 4;
leq_w *= 4;
fprintf(stderr, "available: %d.%03dMB\r\n", (tot_w / 1024), (tot_w % 1024));
fprintf(stderr, "allocated: %d.%03dMB\r\n", (pos_w / 1024), (pos_w % 1024));
if ( leq_w ) {
fprintf(stderr, "leaked: %d.%03dMB\r\n", (leq_w / 1024), (leq_w % 1024));
}
if ( caf_w ) {
fprintf(stderr, "stashed: %d.%03dMB\r\n", (caf_w / 1024), (caf_w % 1024));
}
else return u3_yes;
}
/* u3_ca_slab(): create a length-bounded proto-atom.

10
g/e.c
View File

@ -698,6 +698,16 @@ u3_ce_init(c3_o chk_o)
}
}
/* u3_ce_grab(): take out the trash.
*/
void
u3_ce_grab(c3_c* cap_c)
{
fprintf(stderr, "garbage: collecting...\r\n");
u3_cv_mark();
u3_ca_sweep(cap_c);
}
/* u3_ce_boot(): start the u3 system.
*/
void

68
g/h.c
View File

@ -474,7 +474,6 @@ u3_ch_gut(u3_ch_root* har_u, u3_noun key)
}
}
/* _ch_free_buck(): free bucket
*/
static void
@ -543,3 +542,70 @@ u3_ch_free(u3_ch_root* har_u)
u3_ca_free(har_u);
}
/* _ch_mark_buck(): mark bucket for gc.
*/
static void
_ch_mark_buck(u3_ch_buck* hab_u)
{
c3_w i_w;
for ( i_w = 0; i_w < hab_u->len_w; i_w++ ) {
u3_ca_mark_noun(hab_u->kev[i_w]);
}
u3_ca_mark_ptr(hab_u);
}
/* _ch_mark_node(): mark node for gc.
*/
static void
_ch_mark_node(u3_ch_node* han_u, c3_w lef_w)
{
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 ( u3_so(u3_ch_slot_is_noun(sot_w)) ) {
u3_noun kev = u3_ch_slot_to_noun(sot_w);
u3_ca_mark_noun(kev);
}
else {
void* hav_v = u3_ch_slot_to_node(sot_w);
if ( 0 == lef_w ) {
_ch_mark_buck(hav_v);
} else {
_ch_mark_node(hav_v, lef_w);
}
}
}
u3_ca_mark_ptr(han_u);
}
/* u3_ch_mark(): mark hashtable for gc.
*/
void
u3_ch_mark(u3_ch_root* har_u)
{
c3_w i_w;
for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];
if ( u3_so(u3_ch_slot_is_noun(sot_w)) ) {
u3_noun kev = u3_ch_slot_to_noun(sot_w);
u3_ca_mark_noun(kev);
}
else if ( u3_so(u3_ch_slot_is_node(sot_w)) ) {
u3_ch_node* han_u = u3_ch_slot_to_node(sot_w);
_ch_mark_node(han_u, 25);
}
}
u3_ca_mark_ptr(har_u);
}

2
g/j.c
View File

@ -646,7 +646,7 @@ u3_cj_mine(u3_noun clu,
fak_u.axe_l = axe_l;
jax_l = _cj_insert(&fak_u);
#if 1
#if 0
fprintf(stderr, "mine: dummy jet %d/%s\r\n", jax_l, fak_u.cos_c);
#endif
}

33
g/v.c
View File

@ -451,3 +451,36 @@ u3_cv_louse(c3_m how_m)
}
#endif
}
/* _cv_mark_ova(): mark queued ovum.
*/
static void
_cv_mark_ova(u3_cs_cart* egg_u)
{
while ( egg_u ) {
u3_ca_mark_noun(egg_u->vir);
egg_u = egg_u->nex_u;
}
}
/* u3_cv_mark(): mark arvo kernel.
*/
void
u3_cv_mark(void)
{
u3_cs_arvo* arv_u = &(u3H->arv_u);
u3_ca_mark_noun(arv_u->yot);
u3_ca_mark_noun(arv_u->now);
u3_ca_mark_noun(arv_u->wen);
u3_ca_mark_noun(arv_u->sen);
u3_ca_mark_noun(arv_u->own);
u3_ca_mark_noun(arv_u->roe);
u3_ca_mark_noun(arv_u->key);
u3_ca_mark_noun(arv_u->ken);
u3_ca_mark_noun(arv_u->roc);
_cv_mark_ova(arv_u->ova.geg_u);
}

View File

@ -45,11 +45,6 @@
u3_noun
u3_ca_take(u3_noun som);
/* u3_ca_audit(): investigate object for bad refcounts.
*/
c3_o
u3_ca_audit(u3_noun som);
/* u3_ca_lose(): lose a reference.
*/
void
@ -60,15 +55,20 @@
c3_w
u3_ca_use(u3_noun som);
/* u3_ca_mark(): mark for gc, returning allocated words.
/* u3_ca_mark_ptr(): mark a pointer for gc. Produce size.
*/
c3_w
u3_ca_mark(u3_noun som);
u3_ca_mark_ptr(void* ptr_v);
/* u3_ca_sweep(): sweep after gc, freeing, matching live count.
/* u3_ca_mark_noun(): mark a noun for gc. Produce size.
*/
c3_w
u3_ca_sweep(c3_w liv_w);
u3_ca_mark_noun(u3_noun som);
/* u3_ca_sweep(): sweep a fully marked road.
*/
void
u3_ca_sweep(c3_c* cap_c);
/* u3_ca_sane(): check allocator sanity.
*/

View File

@ -23,3 +23,9 @@
*/
void
u3_ce_init(c3_o chk_o);
/* u3_ce_grab(): garbage-collect memory.
*/
void
u3_ce_grab(c3_c* cap_c);

View File

@ -39,3 +39,7 @@
void
u3_ch_free(u3_ch_root* har_u);
/* u3_ch_mark(): mark hashtable for gc.
*/
void
u3_ch_mark(u3_ch_root* har_u);

View File

@ -58,11 +58,6 @@
c3_i
u3_cm_error(c3_c* str_c);
/* u3_cm_grab(): garbage-collect memory. Asserts u3R == u3H.
*/
void
u3_cm_grab(void);
/* u3_cm_check(): checkpoint memory to file. Asserts u3R == u3H.
*/
void

View File

@ -115,3 +115,8 @@
*/
void
u3_cv_louse(c3_m how_m);
/* u3_cv_mark(): mark arvo kernel.
*/
void
u3_cv_mark(void);

View File

@ -6,7 +6,7 @@
**/
/* u3_cart: ovum carton.
*/
struct _u3_arvo;
struct _u3_cs_arvo;
typedef struct _u3_cs_cart {
u3_noun vir; // effects of ovum
@ -14,7 +14,7 @@
u3_bean cit; // cart committed?
c3_d ent_d; // entry in raft queue?
struct _u3_cs_cart* nex_u; // next in queue
} u3_cart;
} u3_cs_cart;
/* u3_cs_arvo: modern arvo structure.
*/
@ -41,4 +41,3 @@
} ova;
};
} u3_cs_arvo;

View File

@ -80,3 +80,5 @@
? ( ((u3_cs_cell *)u3_co_to_ptr(som))->tel )\
: u3_cm_bail(c3__exit) )
/* u3_cs_hold, u3_cs_move: iterators for memory control.
*/

View File

@ -63,7 +63,7 @@
** preserve the results of the inner one, not just use them for
** temporary purposes, it has to copy them.
**
** This is a trivial cost in some cases, a prohibitive case in
** This is a trivial cost in some cases, a prohibitive cost in
** others. The upside, of course, is that all garbage accrued
** in the inner computation is discarded at zero cost.
**

View File

@ -10,16 +10,6 @@
{
u3_noun von = u3_ci_molt(u3k(van), u3_cv_sam, u3k(sut), 0);
u3_noun gat = u3_cj_hook(u3k(von), "gain");
u3_noun dun = u3_cqfu_dunq(van, "type", sut);
u3_noun pro;
if ( u3_ne(u3_ca_audit(gen)) ){
printf("bad gene!\r\n");
}
u3_ct_push(u3nc(c3__mean, dun));
pro = u3_cn_kick_on(u3_ci_molt(gat, u3_cv_sam, u3k(gen), 0));
u3_ct_drop();
return pro;
return u3_cn_kick_on(u3_ci_molt(gat, u3_cv_sam, u3k(gen), 0));
}

View File

@ -349,7 +349,7 @@ u3_lo_open(void)
void
u3_lo_shut(u3_bean inn)
{
// u3_lo_grab("lo_shut a", u3_none);
// u3_ce_grab("lo_shut a");
// process actions
//

View File

@ -1619,7 +1619,7 @@ _raft_punk(u3_noun ovo)
static void
_raft_comm(c3_d bid_d)
{
u3_cart* egg_u;
u3_cs_cart* egg_u;
u3_lo_open();
@ -1695,7 +1695,7 @@ u3_raft_work(void)
}
}
else {
u3_cart* egg_u;
u3_cs_cart* egg_u;
u3_noun ova;
u3_noun vir;
u3_noun nex;