Hooks: move the "extra" pointer into the hook_t itself.

This simplifies the mallctl call to install a hook, which should only take a
single argument.
This commit is contained in:
David Goldblatt 2018-04-26 15:46:08 -07:00 committed by David Goldblatt
parent cb0707c0fc
commit 126e9a84a5
3 changed files with 31 additions and 31 deletions

View File

@ -103,6 +103,7 @@ struct hooks_s {
hook_alloc alloc_hook;
hook_dalloc dalloc_hook;
hook_expand expand_hook;
void *extra;
};
/*
@ -142,7 +143,7 @@ struct hook_ralloc_args_s {
*/
bool hook_boot();
void *hook_install(tsdn_t *tsdn, hooks_t *hooks, void *extra);
void *hook_install(tsdn_t *tsdn, hooks_t *hooks);
/* Uninstalls the hook with the handle previously returned from hook_install. */
void hook_remove(tsdn_t *tsdn, void *opaque);

View File

@ -9,7 +9,6 @@
typedef struct hooks_internal_s hooks_internal_t;
struct hooks_internal_s {
hooks_t hooks;
void *extra;
bool in_use;
};
@ -27,7 +26,7 @@ hook_boot() {
}
static void *
hook_install_locked(hooks_t *to_install, void *extra) {
hook_install_locked(hooks_t *to_install) {
hooks_internal_t hooks_internal;
for (int i = 0; i < HOOKS_MAX; i++) {
bool success = seq_try_load_hooks(&hooks_internal, &hooks[i]);
@ -35,7 +34,6 @@ hook_install_locked(hooks_t *to_install, void *extra) {
assert(success);
if (!hooks_internal.in_use) {
hooks_internal.hooks = *to_install;
hooks_internal.extra = extra;
hooks_internal.in_use = true;
seq_store_hooks(&hooks[i], &hooks_internal);
atomic_store_u(&nhooks,
@ -48,9 +46,9 @@ hook_install_locked(hooks_t *to_install, void *extra) {
}
void *
hook_install(tsdn_t *tsdn, hooks_t *to_install, void *extra) {
hook_install(tsdn_t *tsdn, hooks_t *to_install) {
malloc_mutex_lock(tsdn, &hooks_mu);
void *ret = hook_install_locked(to_install, extra);
void *ret = hook_install_locked(to_install);
if (ret != NULL) {
tsd_global_slow_inc(tsdn);
}
@ -112,7 +110,7 @@ hook_invoke_alloc(hook_alloc_t type, void *result, uintptr_t result_raw,
FOR_EACH_HOOK_BEGIN(&hook)
hook_alloc h = hook.hooks.alloc_hook;
if (h != NULL) {
h(hook.extra, type, result, result_raw, args_raw);
h(hook.hooks.extra, type, result, result_raw, args_raw);
}
FOR_EACH_HOOK_END
}
@ -126,7 +124,7 @@ hook_invoke_dalloc(hook_dalloc_t type, void *address, uintptr_t args_raw[3]) {
FOR_EACH_HOOK_BEGIN(&hook)
hook_dalloc h = hook.hooks.dalloc_hook;
if (h != NULL) {
h(hook.extra, type, address, args_raw);
h(hook.hooks.extra, type, address, args_raw);
}
FOR_EACH_HOOK_END
}
@ -141,7 +139,7 @@ hook_invoke_expand(hook_expand_t type, void *address, size_t old_usize,
FOR_EACH_HOOK_BEGIN(&hook)
hook_expand h = hook.hooks.expand_hook;
if (h != NULL) {
h(hook.extra, type, address, old_usize, new_usize,
h(hook.hooks.extra, type, address, old_usize, new_usize,
result_raw, args_raw);
}
FOR_EACH_HOOK_END

View File

@ -81,8 +81,9 @@ test_expand_hook(void *extra, hook_expand_t type, void *address,
TEST_BEGIN(test_hooks_basic) {
/* Just verify that the record their arguments correctly. */
hooks_t hooks = {
&test_alloc_hook, &test_dalloc_hook, &test_expand_hook};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)111);
&test_alloc_hook, &test_dalloc_hook, &test_expand_hook,
(void *)111};
void *handle = hook_install(TSDN_NULL, &hooks);
uintptr_t args_raw[4] = {10, 20, 30, 40};
/* Alloc */
@ -124,15 +125,15 @@ TEST_END
TEST_BEGIN(test_hooks_null) {
/* Null hooks should be ignored, not crash. */
hooks_t hooks1 = {NULL, NULL, NULL};
hooks_t hooks2 = {&test_alloc_hook, NULL, NULL};
hooks_t hooks3 = {NULL, &test_dalloc_hook, NULL};
hooks_t hooks4 = {NULL, NULL, &test_expand_hook};
hooks_t hooks1 = {NULL, NULL, NULL, NULL};
hooks_t hooks2 = {&test_alloc_hook, NULL, NULL, NULL};
hooks_t hooks3 = {NULL, &test_dalloc_hook, NULL, NULL};
hooks_t hooks4 = {NULL, NULL, &test_expand_hook, NULL};
void *handle1 = hook_install(TSDN_NULL, &hooks1, NULL);
void *handle2 = hook_install(TSDN_NULL, &hooks2, NULL);
void *handle3 = hook_install(TSDN_NULL, &hooks3, NULL);
void *handle4 = hook_install(TSDN_NULL, &hooks4, NULL);
void *handle1 = hook_install(TSDN_NULL, &hooks1);
void *handle2 = hook_install(TSDN_NULL, &hooks2);
void *handle3 = hook_install(TSDN_NULL, &hooks3);
void *handle4 = hook_install(TSDN_NULL, &hooks4);
assert_ptr_ne(handle1, NULL, "Hook installation failed");
assert_ptr_ne(handle2, NULL, "Hook installation failed");
@ -161,8 +162,8 @@ TEST_BEGIN(test_hooks_null) {
TEST_END
TEST_BEGIN(test_hooks_remove) {
hooks_t hooks = {&test_alloc_hook, NULL, NULL};
void *handle = hook_install(TSDN_NULL, &hooks, NULL);
hooks_t hooks = {&test_alloc_hook, NULL, NULL, NULL};
void *handle = hook_install(TSDN_NULL, &hooks);
assert_ptr_ne(handle, NULL, "Hook installation failed");
call_count = 0;
uintptr_t args_raw[4] = {10, 20, 30, 40};
@ -179,8 +180,8 @@ TEST_END
TEST_BEGIN(test_hooks_alloc_simple) {
/* "Simple" in the sense that we're not in a realloc variant. */
hooks_t hooks = {&test_alloc_hook, NULL, NULL};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)123);
hooks_t hooks = {&test_alloc_hook, NULL, NULL, (void *)123};
void *handle = hook_install(TSDN_NULL, &hooks);
assert_ptr_ne(handle, NULL, "Hook installation failed");
/* Stop malloc from being optimized away. */
@ -290,8 +291,8 @@ TEST_END
TEST_BEGIN(test_hooks_dalloc_simple) {
/* "Simple" in the sense that we're not in a realloc variant. */
hooks_t hooks = {NULL, &test_dalloc_hook, NULL};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)123);
hooks_t hooks = {NULL, &test_dalloc_hook, NULL, (void *)123};
void *handle = hook_install(TSDN_NULL, &hooks);
assert_ptr_ne(handle, NULL, "Hook installation failed");
void *volatile ptr;
@ -337,8 +338,8 @@ TEST_END
TEST_BEGIN(test_hooks_expand_simple) {
/* "Simple" in the sense that we're not in a realloc variant. */
hooks_t hooks = {NULL, NULL, &test_expand_hook};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)123);
hooks_t hooks = {NULL, NULL, &test_expand_hook, (void *)123};
void *handle = hook_install(TSDN_NULL, &hooks);
assert_ptr_ne(handle, NULL, "Hook installation failed");
void *volatile ptr;
@ -365,8 +366,8 @@ TEST_END
TEST_BEGIN(test_hooks_realloc_as_malloc_or_free) {
hooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,
&test_expand_hook};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)123);
&test_expand_hook, (void *)123};
void *handle = hook_install(TSDN_NULL, &hooks);
assert_ptr_ne(handle, NULL, "Hook installation failed");
void *volatile ptr;
@ -416,8 +417,8 @@ static void
do_realloc_test(void *(*ralloc)(void *, size_t, int), int flags,
int expand_type, int dalloc_type) {
hooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,
&test_expand_hook};
void *handle = hook_install(TSDN_NULL, &hooks, (void *)123);
&test_expand_hook, (void *)123};
void *handle = hook_install(TSDN_NULL, &hooks);
assert_ptr_ne(handle, NULL, "Hook installation failed");
void *volatile ptr;