diff --git a/include/libipc/mem/new.h b/include/libipc/mem/new.h index cc90f24..fcd8223 100644 --- a/include/libipc/mem/new.h +++ b/include/libipc/mem/new.h @@ -26,13 +26,13 @@ namespace mem { class LIBIPC_EXPORT block_collector { public: virtual ~block_collector() noexcept = default; - virtual void recycle(void *p, std::size_t bytes, std::size_t alignment) noexcept = 0; + virtual void recycle(void *p) noexcept = 0; }; #if defined(LIBIPC_CPP_17) -using recycle_t = void (*)(void *p, void *o, std::size_t bytes, std::size_t alignment) noexcept; +using recycle_t = void (*)(void *p, void *o) noexcept; #else -using recycle_t = void (*)(void *p, void *o, std::size_t bytes, std::size_t alignment); +using recycle_t = void (*)(void *p, void *o); #endif static constexpr std::size_t regular_head_size @@ -60,14 +60,12 @@ constexpr inline std::size_t regular_sizeof_impl(std::size_t s) noexcept { } /// \brief Calculates the appropriate memory block size based on the specific type. -template -constexpr inline std::size_t regular_sizeof() noexcept { - return regular_sizeof_impl(regular_head_size + sizeof(T)); +constexpr inline std::size_t regular_sizeof(std::size_t s) noexcept { + return regular_sizeof_impl(regular_head_size + s); } - -template <> -constexpr inline std::size_t regular_sizeof() noexcept { - return (std::numeric_limits::max)(); +template +constexpr inline std::size_t regular_sizeof() noexcept { + return regular_sizeof(S); } /// \brief Use block pools to handle memory less than 64K. @@ -78,7 +76,7 @@ public: return block_pool::allocate(); } - void deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/) noexcept { + void deallocate(void *p) noexcept { block_pool::deallocate(p); } }; @@ -91,8 +89,8 @@ public: return new_delete_resource::allocate(regular_head_size + bytes, alignment); } - void deallocate(void *p, std::size_t bytes, std::size_t alignment) noexcept { - new_delete_resource::deallocate(p, regular_head_size + bytes, alignment); + void deallocate(void *p) noexcept { + new_delete_resource::deallocate(p, regular_head_size); } }; @@ -103,8 +101,8 @@ class block_pool_resource : public block_resource_base; - void recycle(void *p, std::size_t bytes, std::size_t alignment) noexcept override { - base_t::deallocate(p, bytes, alignment); + void recycle(void *p) noexcept override { + base_t::deallocate(p); } public: @@ -115,19 +113,13 @@ public: template void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept { - void *p = base_t::allocate(bytes, alignment); - *static_cast(p) - = [](void *p, void *o, std::size_t bytes, std::size_t alignment) noexcept { - std::ignore = destroy(static_cast(o)); - get()->recycle(p, bytes, alignment); + void *b = base_t::allocate(bytes, alignment); + *static_cast(b) + = [](void *b, void *p) noexcept { + std::ignore = destroy(static_cast(p)); + get()->recycle(b); }; - return static_cast(p) + regular_head_size; - } - - void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept { - void *b = static_cast(p) - regular_head_size; - auto *r = static_cast(b); - (*r)(b, p, bytes, alignment); + return static_cast(b) + regular_head_size; } }; @@ -147,6 +139,11 @@ auto *get_regular_resource() noexcept { using block_poll_resource_t = block_pool_resource>; return dynamic_cast(block_poll_resource_t::get()); } +template ::value, bool> = true> +auto *get_regular_resource() noexcept { + using block_poll_resource_t = block_pool_resource<0, 0>; + return dynamic_cast(block_poll_resource_t::get()); +} namespace detail_new { @@ -171,27 +168,6 @@ struct do_allocate { } }; -template -struct do_deallocate { - template - static void apply(R *res, T *p) noexcept { -#if (LIBIPC_CC_MSVC > LIBIPC_CC_MSVC_2015) - res->deallocate(p, sizeof(T), alignof(T)); -#else - // `alignof` of vs2015 requires that type must be able to be instantiated. - res->deallocate(p, sizeof(T)); -#endif - } -}; - -template <> -struct do_deallocate { - template - static void apply(R *res, void *p) noexcept { - res->deallocate(p, 0); - } -}; - } // namespace detail_new /// \brief Creates an object based on the specified type and parameters with block pool resource. @@ -209,9 +185,9 @@ T *$new(A &&... args) noexcept { template void $delete(T *p) noexcept { if (p == nullptr) return; - auto *res = get_regular_resource(); - if (res == nullptr) return; - detail_new::do_deallocate::apply(res, p); + auto *b = reinterpret_cast(p) - regular_head_size; + auto *r = reinterpret_cast(b); + (*r)(b, p); } /// \brief The destruction policy used by std::unique_ptr.