[util] rgn_bootstrap_new

This commit is contained in:
Milo Turner 2020-02-25 12:25:26 -05:00
parent 3936c26feb
commit a31e039e09
3 changed files with 15 additions and 4 deletions

View File

@ -15,19 +15,17 @@
struct ax_ctxt* ax_new(void)
{
// "bootstrap" a new region
struct rgn rgn0;
rgn_init(&rgn0, SMALL);
struct rgn* rgn = rmemdup_typed(&rgn0, struct rgn, &rgn0, 1);
struct rgn* rgn = rgn_bootstrap_new(MEDIUM);
// use it to make a new context
struct ax_ctxt* ax = ralloc_typed(rgn, struct ax_ctxt, 1);
ax__ctxt_init(ax, rgn);
rgn_pin(rgn, ax, (void*) ax__ctxt_cleanup);
return ax;
}
void ax_free(struct ax_ctxt* ax)
{
if (ax != NULL) {
ax__ctxt_cleanup(ax);
rgn_cleanup(ax->init_rgn);
}
}

View File

@ -115,6 +115,15 @@ void rgn_pin(struct rgn* rgn, void* obj, void (*dtor)(void*))
rgn->pinned = pin;
}
struct rgn* rgn_bootstrap_new(size_t pgsize)
{
struct rgn tmp;
rgn_init(&tmp, pgsize);
struct rgn* ptr = ralloc_typed(&tmp, struct rgn, 1);
*ptr = tmp;
return ptr;
}
/*
void rgn_print_stats(struct rgn* rgn, FILE* out)
{

View File

@ -49,6 +49,10 @@ void rgn_pin(struct rgn* rgn, void* obj, void (*dtor)(void*));
* Derived utilities
* -------------------------------------------------------------------------- */
// new "self-bootstrapped" region -- the return region pointer belongs to
// itself, and will be invalidated when this region is cleared/cleaned up.
struct rgn* rgn_bootstrap_new(size_t pgsize);
#define ralloc_typed(_rgn, T, _n) \
((T*) ralloc(_rgn, sizeof(T) * (_n)))