From 6a6c51d35920f1446d11776f6c0a4b6cb61ff912 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 3 Jul 2019 17:27:58 -0700 Subject: [PATCH] ircd::allocator: Distill out GNU malloc featurette from allocator.cc unit. --- include/ircd/allocator.h | 5 +- ircd/Makefile.am | 1 + ircd/allocator.cc | 227 ++------------------------------------- ircd/allocator_gnu.cc | 164 ++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 217 deletions(-) create mode 100644 ircd/allocator_gnu.cc diff --git a/include/ircd/allocator.h b/include/ircd/allocator.h index 3852c8170..6f46e6fcb 100644 --- a/include/ircd/allocator.h +++ b/include/ircd/allocator.h @@ -36,7 +36,7 @@ namespace ircd::allocator profile operator+(const profile &, const profile &); profile operator-(const profile &, const profile &); - bool trim(const size_t &pad = 0); // malloc_trim(3) + bool trim(const size_t &pad = 0) noexcept; // malloc_trim(3) string_view info(const mutable_buffer &); }; @@ -111,6 +111,9 @@ struct ircd::allocator::scope using realloc_closure = std::function; using free_closure = std::function; + static void hook_init() noexcept; + static void hook_fini() noexcept; + static scope *current; scope *theirs; alloc_closure user_alloc; diff --git a/ircd/Makefile.am b/ircd/Makefile.am index 2444357c3..d61e4cc6e 100644 --- a/ircd/Makefile.am +++ b/ircd/Makefile.am @@ -116,6 +116,7 @@ libircd_la_SOURCES =# libircd_la_SOURCES += assert.cc libircd_la_SOURCES += info.cc libircd_la_SOURCES += allocator.cc +libircd_la_SOURCES += allocator_gnu.cc libircd_la_SOURCES += vg.cc libircd_la_SOURCES += exception.cc libircd_la_SOURCES += util.cc diff --git a/ircd/allocator.cc b/ircd/allocator.cc index 223df4bb8..a639a17d9 100644 --- a/ircd/allocator.cc +++ b/ircd/allocator.cc @@ -8,8 +8,6 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. -#include user_free) - scope::current->user_free(ptr); - else - ::free(ptr); } -void * -ircd::allocator::realloc_hook(void *const ptr, - size_t size, - const void *const caller) -{ - // Once we've hooked we put back their hook before calling the user - // so they can passthru to the function without hooking themselves. - uninstall_realloc_hook(); - const unwind rehook_ours - { - install_realloc_hook - }; - - assert(scope::current); - return scope::current->user_realloc? - scope::current->user_realloc(ptr, size): - ::realloc(ptr, size); -} - -void * -ircd::allocator::malloc_hook(size_t size, - const void *const caller) -{ - // Once we've hooked we put back their hook before calling the user - // so they can passthru to the function without hooking themselves. - uninstall_malloc_hook(); - const unwind rehook_ours - { - install_malloc_hook - }; - - assert(scope::current); - return scope::current->user_alloc? - scope::current->user_alloc(size): - ::malloc(size); -} - -#if defined(__GNU_LIBRARY__) && defined(HAVE_MALLOC_H) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" void -ircd::allocator::install_malloc_hook() -{ - assert(!their_malloc_hook); - their_malloc_hook = __malloc_hook; - __malloc_hook = malloc_hook; -} -#pragma GCC diagnostic pop -#else -void -ircd::allocator::install_malloc_hook() +__attribute__((weak)) +ircd::allocator::scope::hook_fini() +noexcept { } -#endif - -#if defined(__GNU_LIBRARY__) && defined(HAVE_MALLOC_H) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -void -ircd::allocator::uninstall_malloc_hook() -{ - __malloc_hook = their_malloc_hook; -} -#pragma GCC diagnostic pop -#else -void -ircd::allocator::uninstall_malloc_hook() -{ -} -#endif - -#if defined(__GNU_LIBRARY__) && defined(HAVE_MALLOC_H) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -void -ircd::allocator::install_realloc_hook() -{ - assert(!their_realloc_hook); - their_realloc_hook = __realloc_hook; - __realloc_hook = realloc_hook; -} -#pragma GCC diagnostic pop -#else -void -ircd::allocator::install_realloc_hook() -{ -} -#endif - -#if defined(__GNU_LIBRARY__) && defined(HAVE_MALLOC_H) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -void -ircd::allocator::uninstall_realloc_hook() -{ - __realloc_hook = their_realloc_hook; -} -#else -void -ircd::allocator::uninstall_realloc_hook() -{ -} -#endif - -#if defined(__GNU_LIBRARY__) && defined(HAVE_MALLOC_H) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -void -ircd::allocator::install_free_hook() -{ - assert(!their_free_hook); - their_free_hook = __free_hook; - __free_hook = free_hook; -} -#pragma GCC diagnostic pop -#else -void -ircd::allocator::install_free_hook() -{ -} -#endif - -#if defined(__GNU_LIBRARY__) && defined(HAVE_MALLOC_H) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -void -ircd::allocator::uninstall_free_hook() -{ - __free_hook = their_free_hook; -} -#pragma GCC diagnostic pop -#else -void -ircd::allocator::uninstall_free_hook() -{ -} -#endif // // allocator::profile diff --git a/ircd/allocator_gnu.cc b/ircd/allocator_gnu.cc new file mode 100644 index 000000000..856c2e43a --- /dev/null +++ b/ircd/allocator_gnu.cc @@ -0,0 +1,164 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#include