Add extent_destroy_t and use it during arena destruction.

Add the extent_destroy_t extent destruction hook to extent_hooks_t, and
use it during arena destruction.  This hook explicitly communicates to
the callee that the extent must be destroyed or tracked for later reuse,
lest it be permanently leaked.  Prior to this change, retained extents
could unintentionally be leaked if extent retention was enabled.

This resolves #560.
This commit is contained in:
Jason Evans
2017-04-27 15:51:35 -07:00
parent b9ab04a191
commit c86c8f4ffb
10 changed files with 120 additions and 17 deletions

View File

@@ -8,6 +8,8 @@ static void *extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr,
unsigned arena_ind);
static bool extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, bool committed, unsigned arena_ind);
static void extent_destroy_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, bool committed, unsigned arena_ind);
static bool extent_commit_hook(extent_hooks_t *extent_hooks, void *addr,
size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr,
@@ -27,6 +29,7 @@ static extent_hooks_t *default_hooks;
static extent_hooks_t hooks = {
extent_alloc_hook,
extent_dalloc_hook,
extent_destroy_hook,
extent_commit_hook,
extent_decommit_hook,
extent_purge_lazy_hook,
@@ -38,6 +41,7 @@ static extent_hooks_t hooks = {
/* Control whether hook functions pass calls through to default hooks. */
static bool try_alloc = true;
static bool try_dalloc = true;
static bool try_destroy = true;
static bool try_commit = true;
static bool try_decommit = true;
static bool try_purge_lazy = true;
@@ -48,6 +52,7 @@ static bool try_merge = true;
/* Set to false prior to operations, then introspect after operations. */
static bool called_alloc;
static bool called_dalloc;
static bool called_destroy;
static bool called_commit;
static bool called_decommit;
static bool called_purge_lazy;
@@ -58,6 +63,7 @@ static bool called_merge;
/* Set to false prior to operations, then introspect after operations. */
static bool did_alloc;
static bool did_dalloc;
static bool did_destroy;
static bool did_commit;
static bool did_decommit;
static bool did_purge_lazy;
@@ -115,6 +121,24 @@ extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
return err;
}
static void
extent_destroy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
bool committed, unsigned arena_ind) {
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
"arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
"true" : "false", arena_ind);
assert_ptr_eq(extent_hooks, &hooks,
"extent_hooks should be same as pointer used to set hooks");
assert_ptr_eq(extent_hooks->destroy, extent_destroy_hook,
"Wrong hook function");
called_destroy = true;
if (!try_destroy) {
return;
}
default_hooks->destroy(default_hooks, addr, size, committed, 0);
did_destroy = true;
}
static bool
extent_commit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
size_t offset, size_t length, unsigned arena_ind) {

View File

@@ -278,6 +278,7 @@ static extent_hooks_t hooks_orig;
static extent_hooks_t hooks_unmap = {
extent_alloc_hook,
extent_dalloc_unmap, /* dalloc */
extent_destroy_hook,
extent_commit_hook,
extent_decommit_hook,
extent_purge_lazy_hook,

View File

@@ -5,6 +5,7 @@
static extent_hooks_t hooks_null = {
extent_alloc_hook,
NULL, /* dalloc */
NULL, /* destroy */
NULL, /* commit */
NULL, /* decommit */
NULL, /* purge_lazy */
@@ -16,6 +17,7 @@ static extent_hooks_t hooks_null = {
static extent_hooks_t hooks_not_null = {
extent_alloc_hook,
extent_dalloc_hook,
extent_destroy_hook,
NULL, /* commit */
extent_decommit_hook,
extent_purge_lazy_hook,
@@ -59,6 +61,7 @@ TEST_BEGIN(test_base_hooks_null) {
extent_hooks_prep();
try_dalloc = false;
try_destroy = true;
try_decommit = false;
try_purge_lazy = false;
try_purge_forced = false;
@@ -98,6 +101,7 @@ TEST_BEGIN(test_base_hooks_not_null) {
extent_hooks_prep();
try_dalloc = false;
try_destroy = true;
try_decommit = false;
try_purge_lazy = false;
try_purge_forced = false;
@@ -194,15 +198,17 @@ TEST_BEGIN(test_base_hooks_not_null) {
}
}
called_dalloc = called_decommit = called_purge_lazy =
called_dalloc = called_destroy = called_decommit = called_purge_lazy =
called_purge_forced = false;
base_delete(base);
assert_true(called_dalloc, "Expected dalloc call");
assert_true(!called_destroy, "Unexpected destroy call");
assert_true(called_decommit, "Expected decommit call");
assert_true(called_purge_lazy, "Expected purge_lazy call");
assert_true(called_purge_forced, "Expected purge_forced call");
try_dalloc = true;
try_destroy = true;
try_decommit = true;
try_purge_lazy = true;
try_purge_forced = true;