/** * \file libpmr/new.h * \author mutouyun (orz@orzz.org) * \brief Global object creation function. * \date 2024-01-01 */ #pragma once #include #include #include #include "libimp/aligned.h" #include "libimp/uninitialized.h" #include "libimp/byte.h" #include "libimp/detect_plat.h" #include "libimp/export.h" #include "libpmr/def.h" #include "libpmr/block_pool.h" #include "libpmr/memory_resource.h" LIBPMR_NAMESPACE_BEG_ /// \brief Select the incremental level based on the size. constexpr inline std::size_t regular_level(std::size_t s) noexcept { return (s <= 128 ) ? 0 : (s <= 1024 ) ? 1 : (s <= 8192 ) ? 2 : (s <= 65536) ? 3 : 4; } /// \brief Calculates the appropriate memory block size based on the increment level and size. constexpr inline std::size_t regular_sizeof_impl(std::size_t l, std::size_t s) noexcept { return (l == 0) ? std::max(::LIBIMP::round_up(s, 8), regular_head_size) : (l == 1) ? ::LIBIMP::round_up(s, 128 ) : (l == 2) ? ::LIBIMP::round_up(s, 1024) : (l == 3) ? ::LIBIMP::round_up(s, 8192) : (std::numeric_limits::max)(); } /// \brief Calculates the appropriate memory block size based on the size. constexpr inline std::size_t regular_sizeof(std::size_t s) noexcept { return regular_sizeof_impl(regular_level(s), s); } /// \brief Calculates the appropriate memory block size based on the specific type. template constexpr inline std::size_t regular_sizeof() noexcept { return regular_sizeof(regular_head_size + sizeof(T)); } /// \brief Defines the memory block collector interface. class LIBIMP_EXPORT block_collector { public: virtual ~block_collector() noexcept = default; virtual void deallocate(void *p) noexcept = 0; }; /// \brief Gets all block pools of the thread cache. LIBIMP_EXPORT auto get_thread_block_pool_map() noexcept -> std::unordered_map &; /// \brief Defines block pool memory resource based on block pool. template class block_pool_resource; /// \brief Memory block collector of unknown size. /// \note This memory resource is only used to temporarily collect memory blocks /// that cannot find a suitable block pool memory resource. template <> class block_pool_resource<0, 0> : public block_pool<0, 0> , public block_collector { public: void deallocate(void *p) noexcept override { block_pool<0, 0>::deallocate(p); } }; /// \brief A block pool memory resource for a block of memory of a specific size. template class block_pool_resource : public block_pool , public block_collector { using base_t = block_pool; void deallocate(void *p) noexcept override { base_t::deallocate(p); } public: static block_pool_resource *get() noexcept; using base_t::base_t; void *allocate(std::size_t /*bytes*/, std::size_t /*alignment*/ = alignof(std::max_align_t)) noexcept { void *p = base_t::allocate(); p = ::LIBIMP::construct(p, BlockSize); return reinterpret_cast<::LIBIMP::byte *>(p) + regular_head_size; } void deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/ = alignof(std::max_align_t)) noexcept { p = reinterpret_cast<::LIBIMP::byte *>(p) - regular_head_size; auto r_size = *static_cast(p); if (r_size <= BlockSize) { base_t::deallocate(p); return; } // When the actual size exceeds the current memory block size, // try to find a suitable pool among all memory block pools for this thread. auto &map = get_thread_block_pool_map(); auto it = map.find(r_size); if ((it == map.end()) || (it->second == nullptr)) { block_pool_resource<0, 0> *bp = nullptr; LIBIMP_TRY { // If the corresponding memory resource cannot be found, // create a temporary general-purpose block pool to deallocate memory. it = map.emplace(r_size, bp = new block_pool_resource<0, 0>).first; } LIBIMP_CATCH(...) { // If the memory resource cannot be created, // store the pointer directly to avoid leakage. delete bp; base_t::deallocate(p); return; } } it->second->deallocate(p); } }; template auto block_pool_resource::get() noexcept -> block_pool_resource * { thread_local block_pool_resource *pi = nullptr; if (pi != nullptr) { return pi; } // Create a new block pool resource for this thread. auto &map = get_thread_block_pool_map(); auto it = map.find(BlockSize); if ((it != map.end()) && (it->second != nullptr)) { // If there are existing block pool resources in the thread cache, // a new block pool resource is constructed based on it and the cache is updated. auto *bp = static_cast *>( dynamic_cast *>(it->second)); if (bp == nullptr) { return nullptr; } thread_local block_pool_resource instance(std::move(*bp)); delete static_cast *>(bp); it->second = pi = &instance; return pi; } else { // If there are no existing block pool resources in the thread cache, // the thread local storage instance is constructed and the pointer is cached. thread_local block_pool_resource instance; LIBIMP_TRY { map.emplace(BlockSize, pi = &instance); return pi; } LIBIMP_CATCH(...) { return nullptr; } } } /// \brief Match the appropriate memory block resources /// according to the size of the specification. template class regular_resource : public new_delete_resource {}; /// \brief Different increment levels match different chunk sizes. /// 512 means that 512 consecutive memory blocks are allocated at a time, and the block size is N. template class regular_resource : public block_pool_resource {}; template class regular_resource : public block_pool_resource {}; template class regular_resource : public block_pool_resource {}; template class regular_resource : public block_pool_resource {}; /// \brief Creates an object based on the specified type and parameters with block pool resource. /// \note This function is thread-safe. template T *new$(A &&... args) noexcept { auto *mem_res = regular_resource()>::get(); if (mem_res == nullptr) return nullptr; return ::LIBIMP::construct(mem_res->allocate(sizeof(T), alignof(T)), std::forward(args)...); } /// \brief Destroys object previously allocated by the `new$` and releases obtained memory area. /// \note This function is thread-safe. If the pointer type passed in is different from `new$`, /// additional performance penalties may be incurred. template void delete$(T *p) noexcept { if (p == nullptr) return; ::LIBIMP::destroy(p); auto *mem_res = regular_resource()>::get(); if (mem_res == nullptr) return; #if defined(LIBIMP_CC_MSVC_2015) // `alignof` of vs2015 requires that type must be able to be instantiated. mem_res->deallocate(p, sizeof(T)); #else mem_res->deallocate(p, sizeof(T), alignof(T)); #endif } LIBPMR_NAMESPACE_END_