diff options
author | 2020-06-13 15:58:27 +0200 | |
---|---|---|
committer | 2020-06-13 15:58:27 +0200 | |
commit | 284f196df1c65a91eb38a5a9f31a2da7fb86a1ac (patch) | |
tree | d29473e4024dce305d0ee3bb957a663420417312 /src/lib/netlist/plib/pmempool.h | |
parent | 52b8d5fd2b656c317e589da93467c33be74e76ea (diff) | |
parent | e949e9c29de82ee7c32692e7b65da05dd22bdc9d (diff) |
Merge branch 'master' of https://github.com/mamedev/mame
Diffstat (limited to 'src/lib/netlist/plib/pmempool.h')
-rw-r--r-- | src/lib/netlist/plib/pmempool.h | 65 |
1 files changed, 27 insertions, 38 deletions
diff --git a/src/lib/netlist/plib/pmempool.h b/src/lib/netlist/plib/pmempool.h index 441595d9e58..ece0f117a8d 100644 --- a/src/lib/netlist/plib/pmempool.h +++ b/src/lib/netlist/plib/pmempool.h @@ -40,31 +40,26 @@ namespace plib { 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_block_align(1024) , m_blocks(base_allocator_type<block *>(m_arena)) { - icount()++; } PCOPYASSIGNMOVE(mempool_arena, delete) ~mempool_arena() { - for (auto & b : m_blocks) { if (b->m_num_alloc != 0) { - plib::perrlogger("Found {} info blocks\n", sinfo().size()); + plib::perrlogger("Found {} info blocks\n", m_info.size()); plib::perrlogger("Found block with {} dangling allocations\n", b->m_num_alloc); } - m_arena.free(b); - //::operator delete(b->m_data); - } - if (icount()-- == 1) - { - if (!sinfo().empty()) - plib::perrlogger("Still found {} info blocks after last mempool deleted\n", sinfo().size()); + detail::free(m_arena, b); } + if (!m_info.empty()) + plib::perrlogger("Still found {} info blocks after mempool deleted\n", m_info.size()); } void *allocate(size_t align, size_t size) @@ -92,21 +87,18 @@ namespace plib { void *ret = reinterpret_cast<void *>(b->m_data + b->m_cur); auto capacity(rs); ret = std::align(align, size, ret, capacity); - sinfo().insert({ ret, info(b, b->m_cur)}); + m_info.insert({ ret, info(b, b->m_cur)}); rs -= (capacity - size); b->m_cur += rs; - base_type::m_stat_cur_alloc() += size; - if (base_type::m_stat_max_alloc() < base_type::m_stat_cur_alloc()) - base_type::m_stat_max_alloc() = base_type::m_stat_cur_alloc(); + this->inc_alloc_stat(size); return ret; } - /*static */ void deallocate(void *ptr, size_t size) noexcept + void deallocate(void *ptr, size_t size) noexcept { - - auto it = sinfo().find(ptr); - if (it == sinfo().end()) + auto it = m_info.find(ptr); + if (it == m_info.end()) plib::terminate("mempool::free - pointer not found"); block *b = it->second.m_block; if (b->m_num_alloc == 0) @@ -115,7 +107,7 @@ namespace plib { { mempool_arena &mp = b->m_mempool; b->m_num_alloc--; - mp.m_stat_cur_alloc() -= size; + mp.dec_alloc_stat(size); if (b->m_num_alloc == 0) { auto itb = std::find(mp.m_blocks.begin(), mp.m_blocks.end(), b); @@ -123,14 +115,15 @@ namespace plib { plib::terminate("mempool::free - block not found"); mp.m_blocks.erase(itb); - m_arena.free(b); + detail::free(mp.base_arena(), b); } - sinfo().erase(it); + m_info.erase(it); } } bool operator ==(const mempool_arena &rhs) const noexcept { return this == &rhs; } + BASEARENA &base_arena() noexcept { return m_arena; } private: struct block { @@ -142,17 +135,19 @@ namespace plib { { min_bytes = std::max(mp.m_min_alloc, min_bytes); m_free = min_bytes; - size_type alloc_bytes = (min_bytes + mp.m_min_align); // - 1); // & ~(mp.m_min_align - 1); + m_bytes_allocated = (min_bytes + mp.m_block_align); // - 1); // & ~(mp.m_min_align - 1); // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) - m_data_allocated = new std::uint8_t[alloc_bytes]; + //m_data_allocated = new std::uint8_t[alloc_bytes]; + m_data_allocated = static_cast<std::uint8_t *>(mp.base_arena().allocate(mp.m_block_align, m_bytes_allocated)); void *r = m_data_allocated; - std::align(mp.m_min_align, min_bytes, r, alloc_bytes); + std::align(mp.m_block_align, min_bytes, r, m_bytes_allocated); m_data = reinterpret_cast<std::uint8_t *>(r); } ~block() { //::operator delete(m_data_allocated); - delete [] m_data_allocated; + //delete [] m_data_allocated; + m_mempool.base_arena().deallocate(m_data_allocated, m_bytes_allocated); } block(const block &) = delete; @@ -163,6 +158,7 @@ namespace plib { size_type m_num_alloc; size_type m_free; size_type m_cur; + size_type m_bytes_allocated; std::uint8_t *m_data; std::uint8_t *m_data_allocated; mempool_arena &m_mempool; @@ -180,27 +176,20 @@ namespace plib { block * new_block(size_type min_bytes) { - auto *b = m_arena.template alloc<block>(*this, min_bytes); + auto *b = detail::alloc<block>(m_arena, *this, min_bytes); m_blocks.push_back(b); return b; } - static std::unordered_map<void *, info> &sinfo() - { - static std::unordered_map<void *, info> spinfo; - return spinfo; - } - - static std::size_t &icount() - { - static std::size_t count = 0; - return count; - } - size_t m_min_alloc; size_t m_min_align; + size_t m_block_align; BASEARENA m_arena; + using base_allocator_typex = typename BASEARENA::template allocator_type<std::pair<void * const, info>>; + std::unordered_map<void *, info, std::hash<void *>, std::equal_to<void *>, + base_allocator_typex> m_info; +// std::unordered_map<void *, info> m_info; std::vector<block *, typename BASEARENA::template allocator_type<block *>> m_blocks; }; |