diff --git a/pkg/urbit/include/vere/vere.h b/pkg/urbit/include/vere/vere.h index 870e75c64d..9abfe5ad6e 100644 --- a/pkg/urbit/include/vere/vere.h +++ b/pkg/urbit/include/vere/vere.h @@ -683,6 +683,28 @@ c3_d u3_time_gap_ms(u3_noun now, u3_noun wen); + /** ward: common structure lifecycle + **/ + /* u3_fact_init(): initialize completed event. + */ + u3_fact* + u3_fact_init(c3_d eve_d, c3_l mug_l, u3_noun job); + + /* u3_fact_free(): dispose completed event. + */ + void + u3_fact_free(u3_fact *tac_u); + + /* u3_gift_init(): initialize effect list. + */ + u3_gift* + u3_gift_init(c3_d eve_d, u3_noun act); + + /* u3_gift_free(): dispose effect list. + */ + void + u3_gift_free(u3_gift* gif_u); + /** New vere **/ /* u3_auto_init(): initialize all drivers. diff --git a/pkg/urbit/vere/disk.c b/pkg/urbit/vere/disk.c index 31e8243635..4628b62638 100644 --- a/pkg/urbit/vere/disk.c +++ b/pkg/urbit/vere/disk.c @@ -99,11 +99,10 @@ _disk_commit_done(struct _cd_save* req_u) { u3_fact* tac_u = log_u->put_u.ext_u; - + while ( tac_u && (tac_u->eve_d <= log_u->dun_d) ) { log_u->put_u.ext_u = tac_u->nex_u; - u3z(tac_u->job); - c3_free(tac_u); + u3_fact_free(tac_u); tac_u = log_u->put_u.ext_u; } } @@ -257,21 +256,18 @@ u3_disk_plan(u3_disk* log_u, u3_fact* tac_u) void u3_disk_boot_plan(u3_disk* log_u, u3_noun job) { - u3_fact* tac_u = c3_malloc(sizeof(*tac_u)); - tac_u->mug_l = 0; //u3r_mug(job); XX - tac_u->eve_d = ++log_u->sen_d; - tac_u->nex_u = 0; - tac_u->job = job; + // NB, boot mugs are 0 + // + u3_fact* tac_u = u3_fact_init(++log_u->sen_d, 0, job); + tac_u->bug_l = 0; // XX if ( !log_u->put_u.ent_u ) { c3_assert( !log_u->put_u.ext_u ); c3_assert( 1ULL == log_u->sen_d ); - tac_u->bug_l = 0; // XX log_u->put_u.ent_u = log_u->put_u.ext_u = tac_u; } else { - tac_u->bug_l = log_u->put_u.ent_u->mug_l; // XX log_u->put_u.ent_u->nex_u = tac_u; log_u->put_u.ent_u = tac_u; } @@ -313,26 +309,30 @@ _disk_read_one_cb(void* vod_p, c3_d eve_d, size_t val_i, void* val_p) { struct _cd_read* red_u = vod_p; u3_disk* log_u = red_u->log_u; - u3_fact* tac_u = c3_calloc(sizeof(*tac_u)); - tac_u->eve_d = eve_d; + u3_fact* tac_u; { // XX u3m_soft? // u3_noun dat = u3ke_cue(u3i_bytes(val_i, val_p)); u3_noun mug, job; + c3_l bug_l; + if ( (c3n == u3r_cell(dat, &mug, &job)) - || (c3n == u3r_safe_word(mug, &tac_u->bug_l)) ) // XX + || (c3n == u3r_safe_word(mug, &bug_l)) ) // XX { // failure here triggers cleanup in _disk_read_start_cb() // - c3_free(tac_u); u3z(dat); return c3n; } - tac_u->job = u3k(job); + // NB: mug is unknown due to log format + // + tac_u = u3_fact_init(eve_d, 0, u3k(job)); + tac_u->bug_l = bug_l; + u3z(dat); } diff --git a/pkg/urbit/vere/lord.c b/pkg/urbit/vere/lord.c index ffa1fcc883..1f0a63d3bf 100644 --- a/pkg/urbit/vere/lord.c +++ b/pkg/urbit/vere/lord.c @@ -409,17 +409,13 @@ _lord_work_done(u3_lord* god_u, u3_noun job, u3_noun act) { - u3_fact* tac_u = c3_malloc(sizeof(*tac_u)); - tac_u->bug_l = god_u->mug_l; - tac_u->mug_l = god_u->mug_l = mug_l; - tac_u->eve_d = god_u->eve_d = eve_d; - tac_u->nex_u = 0; - tac_u->job = job; + u3_fact* tac_u = u3_fact_init(eve_d, mug_l, job); + tac_u->bug_l = god_u->mug_l; // XX - u3_gift* gif_u = c3_malloc(sizeof(*gif_u)); - gif_u->eve_d = eve_d; - gif_u->nex_u = 0; - gif_u->act = act; + god_u->mug_l = mug_l; + god_u->eve_d = eve_d; + + u3_gift* gif_u = u3_gift_init(eve_d, act); _lord_work_spin(god_u); diff --git a/pkg/urbit/vere/pier.c b/pkg/urbit/vere/pier.c index ded2d57b20..7e6ead75b1 100644 --- a/pkg/urbit/vere/pier.c +++ b/pkg/urbit/vere/pier.c @@ -131,12 +131,9 @@ _pier_gift_kick(u3_work* wok_u) #ifdef VERBOSE_PIER fprintf(stderr, "pier: (%" PRIu64 "): compute: release\r\n", gif_u->eve_d); #endif - u3_auto_kick(wok_u->car_u, gif_u->act); - // XX dispose properly - // - u3z(gif_u->act); - c3_free(gif_u); + u3_auto_kick(wok_u->car_u, gif_u->act); + u3_gift_free(gif_u); } } @@ -644,8 +641,7 @@ _pier_on_lord_play_done(void* vod_p, u3_info fon_u, c3_l mug_l) while ( tac_u ) { nex_u = tac_u->nex_u; - u3z(tac_u->job); - c3_free(tac_u); + u3_fact_free(tac_u); tac_u = nex_u; } } @@ -673,8 +669,7 @@ _pier_on_lord_play_bail(void* vod_p, u3_info fon_u, while ( tac_u->eve_d < eve_d ) { nex_u = tac_u->nex_u; las_l = tac_u->mug_l; - u3z(tac_u->job); - c3_free(tac_u); + u3_fact_free(tac_u); tac_u = nex_u; } diff --git a/pkg/urbit/vere/ward.c b/pkg/urbit/vere/ward.c new file mode 100644 index 0000000000..8b4850a7c8 --- /dev/null +++ b/pkg/urbit/vere/ward.c @@ -0,0 +1,71 @@ +/* vere/ward.c +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "all.h" +#include "vere/vere.h" + +// ward: lifecycle management for common structures +// +// should contain anything allocated in multiple modules, +// or allocated in one and freed in another +// + +/* u3_fact_init(): initialize completed event. +*/ +u3_fact* +u3_fact_init(c3_d eve_d, c3_l mug_l, u3_noun job) +{ + u3_fact *tac_u = c3_malloc(sizeof(*tac_u)); + tac_u->eve_d = eve_d; + tac_u->mug_l = mug_l; + tac_u->nex_u = 0; + tac_u->job = job; + + return tac_u; +} + +/* u3_fact_free(): dispose completed event. +*/ +void +u3_fact_free(u3_fact *tac_u) +{ + u3z(tac_u->job); + c3_free(tac_u); +} + +/* u3_gift_init(): initialize effect list. +*/ +u3_gift* +u3_gift_init(c3_d eve_d, u3_noun act) +{ + u3_gift *gif_u = c3_malloc(sizeof(*gif_u)); + gif_u->eve_d = eve_d; + gif_u->nex_u = 0; + gif_u->act = act; + + return gif_u; +} + +/* u3_gift_free(): dispose effect list. +*/ +void +u3_gift_free(u3_gift* gif_u) +{ + u3z(gif_u->act); + c3_free(gif_u); +}