From bed452a874c5565b176bae530af76d3af60047ee Mon Sep 17 00:00:00 2001 From: couriersud Date: Mon, 8 Jun 2020 03:09:32 +0200 Subject: netlist: arena code maintenance. (nw) --- src/lib/netlist/nltypes.h | 7 +- src/lib/netlist/plib/palloc.h | 185 +++++++++++++++++++++++++++++----------- src/lib/netlist/plib/pmempool.h | 63 ++------------ 3 files changed, 141 insertions(+), 114 deletions(-) diff --git a/src/lib/netlist/nltypes.h b/src/lib/netlist/nltypes.h index 0d34186bdc1..42479e0636b 100644 --- a/src/lib/netlist/nltypes.h +++ b/src/lib/netlist/nltypes.h @@ -79,13 +79,8 @@ namespace netlist /// using device_arena = std::conditional::type; + using host_arena = plib::aligned_arena; - using host_arena = plib::aligned_arena; - -#if 0 - template - using host_unique_ptr = host_arena::unique_ptr; -#endif /// \brief Interface definition for netlist callbacks into calling code /// /// A class inheriting from netlist_callbacks_t has to be passed to the netlist_t diff --git a/src/lib/netlist/plib/palloc.h b/src/lib/netlist/plib/palloc.h index 3d52e3af737..a2abe820899 100644 --- a/src/lib/netlist/plib/palloc.h +++ b/src/lib/netlist/plib/palloc.h @@ -288,16 +288,17 @@ namespace plib { // Memory allocation //============================================================ - struct aligned_arena + template + struct arena_base { - static constexpr const bool has_static_deallocator = true; + static constexpr const bool has_static_deallocator = HSD; using size_type = std::size_t; template - using allocator_type = arena_allocator; + using allocator_type = arena_allocator; template - using deleter_type = arena_deleter; + using deleter_type = arena_deleter; template using unique_ptr = std::unique_ptr>; @@ -305,69 +306,71 @@ namespace plib { template using owned_ptr = plib::owned_ptr>; - static inline aligned_arena &instance() noexcept + static inline P &instance() noexcept { - static aligned_arena s_arena; + static P s_arena; return s_arena; } - static inline void *allocate( size_t alignment, size_t size ) + template + static inline std::enable_if_t> + make_unique(Args&&... args) { - m_stat_cur_alloc() += size; - if (m_stat_max_alloc() < m_stat_cur_alloc()) - m_stat_max_alloc() = m_stat_cur_alloc(); - - #if (PUSE_ALIGNED_ALLOCATION) - #if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER) - return _aligned_malloc(size, alignment); - #elif defined(__APPLE__) - void* p; - if (::posix_memalign(&p, alignment, size) != 0) { - p = nullptr; + auto *mem = P::allocate(alignof(T), sizeof(T)); + try + { + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + auto *mema = new (mem) T(std::forward(args)...); + return unique_ptr(mema, deleter_type()); + } + catch (...) + { + P::deallocate(mem, sizeof(T)); + throw; } - return p; - #else - return aligned_alloc(alignment, size); - #endif - #else - unused_var(alignment); - return ::operator new(size); - #endif } - static inline void deallocate( void *ptr, size_t size ) noexcept + template + inline std::enable_if_t> + make_unique(Args&&... args) { - //unused_var(size); - m_stat_cur_alloc() -= size; - #if (PUSE_ALIGNED_ALLOCATION) - // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) - ::free(ptr); - #else - ::operator delete(ptr); - #endif + auto *mem = static_cast

(this)->allocate(alignof(T), sizeof(T)); + try + { + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + auto *mema = new (mem) T(std::forward(args)...); + return unique_ptr(mema, deleter_type()); + } + catch (...) + { + static_cast

(this)->deallocate(mem, sizeof(T)); + throw; + } } template - static inline unique_ptr make_unique(Args&&... args) + static inline std::enable_if_t> + make_owned(Args&&... args) { - auto *mem = allocate(alignof(T), sizeof(T)); + auto *mem = P::allocate(alignof(T), sizeof(T)); try { // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) auto *mema = new (mem) T(std::forward(args)...); - return unique_ptr(mema, deleter_type()); + return owned_ptr(mema, true, deleter_type()); } catch (...) { - deallocate(mem, sizeof(T)); + P::deallocate(mem, sizeof(T)); throw; } } template - static inline owned_ptr make_owned(Args&&... args) + inline std::enable_if_t> + make_owned(Args&&... args) { - auto *mem = allocate(alignof(T), sizeof(T)); + auto *mem = static_cast

(this)->allocate(alignof(T), sizeof(T)); try { // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) @@ -376,40 +379,122 @@ namespace plib { } catch (...) { - deallocate(mem, sizeof(T)); + static_cast

(this)->deallocate(mem, sizeof(T)); throw; } } template - static T *alloc(Args&&... args) + static inline std::enable_if_t + alloc(Args&&... args) + { + auto *p = P::allocate(alignof(T), sizeof(T)); + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + return new(p) T(std::forward(args)...); + } + + template + inline std::enable_if_t + alloc(Args&&... args) { - auto *p = allocate(alignof(T), sizeof(T)); + auto *p = static_cast

(this)->allocate(alignof(T), sizeof(T)); // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) return new(p) T(std::forward(args)...); } template - static void free(T *ptr) noexcept + static inline void + free(T *ptr, std::enable_if_t = nullptr) noexcept { ptr->~T(); - aligned_arena::deallocate(ptr, sizeof(T)); + P::deallocate(ptr, sizeof(T)); } - bool operator ==(const aligned_arena &rhs) const noexcept + template + inline void + free(T *ptr, std::enable_if_t = nullptr) noexcept { - plib::unused_var(rhs); - return true; + ptr->~T(); + static_cast

(this)->deallocate(ptr, sizeof(T)); } size_type cur_alloc() const noexcept { return m_stat_cur_alloc(); } // NOLINT(readability-convert-member-functions-to-static) size_type max_alloc() const noexcept { return m_stat_max_alloc(); } // NOLINT(readability-convert-member-functions-to-static) - private: + protected: static size_t &m_stat_cur_alloc() noexcept { static size_t val = 0; return val; } static size_t &m_stat_max_alloc() noexcept { static size_t val = 0; return val; } }; + + struct aligned_arena : public arena_base + { + static inline void *allocate( size_t alignment, size_t size ) + { + m_stat_cur_alloc() += size; + if (m_stat_max_alloc() < m_stat_cur_alloc()) + m_stat_max_alloc() = m_stat_cur_alloc(); + + #if (PUSE_ALIGNED_ALLOCATION) + #if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER) + return _aligned_malloc(size, alignment); + #elif defined(__APPLE__) + void* p; + if (::posix_memalign(&p, alignment, size) != 0) { + p = nullptr; + } + return p; + #else + return aligned_alloc(alignment, size); + #endif + #else + unused_var(alignment); + return ::operator new(size); + #endif + } + + static inline void deallocate( void *ptr, size_t size ) noexcept + { + //unused_var(size); + m_stat_cur_alloc() -= size; + #if (PUSE_ALIGNED_ALLOCATION) + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) + ::free(ptr); + #else + ::operator delete(ptr); + #endif + } + + bool operator ==(const aligned_arena &rhs) const noexcept + { + plib::unused_var(rhs); + return true; + } + + }; + + struct std_arena : public arena_base + { + static inline void *allocate( size_t alignment, size_t size ) + { + m_stat_cur_alloc() += size; + unused_var(alignment); + return ::operator new(size); + } + + static inline void deallocate( void *ptr, size_t size ) noexcept + { + m_stat_cur_alloc() -= size; + ::operator delete(ptr); + } + + bool operator ==(const aligned_arena &rhs) const noexcept + { + plib::unused_var(rhs); + return true; + } + }; + template constexpr T *assume_aligned_ptr(T *p) noexcept { diff --git a/src/lib/netlist/plib/pmempool.h b/src/lib/netlist/plib/pmempool.h index fca4988b639..07987f4e830 100644 --- a/src/lib/netlist/plib/pmempool.h +++ b/src/lib/netlist/plib/pmempool.h @@ -27,28 +27,13 @@ namespace plib { // Memory pool //============================================================ - class mempool_arena + class mempool_arena : public arena_base { public: - using size_type = std::size_t; - - static constexpr const bool has_static_deallocator = true; - - template - using allocator_type = arena_allocator; - - template - using owned_ptr = plib::owned_ptr>; - - template - using unique_ptr = std::unique_ptr>; - mempool_arena(size_t min_alloc = (1<<21), size_t min_align = PALIGN_CACHELINE) : m_min_alloc(min_alloc) , m_min_align(min_align) - , m_stat_cur_alloc(0) - , m_stat_max_alloc(0) { icount()++; } @@ -103,8 +88,9 @@ namespace plib { sinfo().insert({ ret, info(b, b->m_cur)}); rs -= (capacity - size); b->m_cur += rs; - m_stat_cur_alloc += size; - m_stat_max_alloc = std::max(m_stat_max_alloc, m_stat_cur_alloc); + m_stat_cur_alloc() += size; + if (m_stat_max_alloc() < m_stat_cur_alloc()) + m_stat_max_alloc() = m_stat_cur_alloc(); return ret; } @@ -122,7 +108,7 @@ namespace plib { { mempool_arena &mp = b->m_mempool; b->m_num_alloc--; - mp.m_stat_cur_alloc -= size; + mp.m_stat_cur_alloc() -= size; if (b->m_num_alloc == 0) { auto itb = std::find(mp.m_blocks.begin(), mp.m_blocks.end(), b); @@ -136,43 +122,6 @@ namespace plib { } } - template - owned_ptr make_owned(Args&&... args) - { - auto *mem = this->allocate(alignof(T), sizeof(T)); - try - { - // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) - auto *mema = new (mem) T(std::forward(args)...); - return owned_ptr(mema, true, arena_deleter(this)); - } - catch (...) - { - deallocate(mem, sizeof(T)); - throw; - } - } - - template - unique_ptr make_unique(Args&&... args) - { - auto *mem = this->allocate(alignof(T), sizeof(T)); - try - { - // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) - auto *mema = new (mem) T(std::forward(args)...); - return unique_ptr(mema, arena_deleter(this)); - } - catch (...) - { - deallocate(mem, sizeof(T)); - throw; - } - } - - size_type cur_alloc() const noexcept { return m_stat_cur_alloc; } - size_type max_alloc() const noexcept { return m_stat_max_alloc; } - bool operator ==(const mempool_arena &rhs) const noexcept { return this == &rhs; } private: @@ -246,8 +195,6 @@ namespace plib { plib::aligned_vector m_blocks; - size_t m_stat_cur_alloc; - size_t m_stat_max_alloc; }; } // namespace plib -- cgit v1.2.3