diff --git a/include/ipc.h b/include/ipc.h index 672ba7f..f440026 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -5,6 +5,8 @@ #include "buffer.h" #include "shm.h" +#include + namespace ipc { using handle_t = void*; diff --git a/src/memory/alloc.h b/src/memory/alloc.h index 63c20a5..a2a53fc 100644 --- a/src/memory/alloc.h +++ b/src/memory/alloc.h @@ -43,7 +43,8 @@ constexpr std::size_t aligned(std::size_t size, size_t alignment) noexcept { class scope_alloc_base { protected: struct block_t { - block_t* next_; + block_t * next_; + std::size_t size_; } * list_ = nullptr; enum : std::size_t { @@ -74,7 +75,7 @@ private: while (list_ != nullptr) { auto curr = list_; list_ = list_->next_; - alloc_.free(curr); + alloc_.free(curr, curr->size_); } // now list_ is nullptr } @@ -103,8 +104,9 @@ public: } void* alloc(std::size_t size) { - auto curr = static_cast(alloc_.alloc(aligned_block_size + size)); + auto curr = static_cast(alloc_.alloc(size += aligned_block_size)); curr->next_ = list_; + curr->size_ = size; return (reinterpret_cast(list_ = curr) + aligned_block_size); } }; @@ -151,9 +153,23 @@ public: }; struct fixed_expand_policy { + + enum : std::size_t { + base_size = sizeof(void*) * 1024 / 2 + }; + + static std::size_t prev(std::size_t& e) { + if ((e /= 2) == 0) e = 1; + return e; + } + + static std::size_t next(std::size_t& e) { + return e *= 2; + } + template - IPC_CONSTEXPR_ static std::size_t next(std::size_t & e) { - return ipc::detail::max(BlockSize, (sizeof(void*) * 1024) / 2) * (e *= 2); + static std::size_t next(std::size_t & e) { + return ipc::detail::max(BlockSize, base_size) * next(e); } }; @@ -205,8 +221,9 @@ public: } void clear() { + ExpandP::prev(this->init_expand_); + this->cursor_ = nullptr; alloc_.~alloc_policy(); - this->init(this->init_expand_); } void* alloc() { diff --git a/src/memory/resource.h b/src/memory/resource.h index 7208f2b..f20a413 100644 --- a/src/memory/resource.h +++ b/src/memory/resource.h @@ -35,14 +35,11 @@ struct chunk_mapping_policy { }; template -struct chunk_alloc_recoverer { -public: - using alloc_policy = AllocP; - - constexpr static void swap(chunk_alloc_recoverer &) {} - constexpr static void clear() {} - constexpr static void try_recover(alloc_policy &) {} - constexpr static void collect(alloc_policy &&) {} +struct chunk_alloc_recoverer : default_alloc_recoverer { + void collect(alloc_policy && alc) { + alc.clear(); // recycle memory to the central heap (static_chunk_alloc) + default_alloc_recoverer::collect(std::move(alc)); + } }; } // namespace detail diff --git a/test/test_circ.cpp b/test/test_circ.cpp index 2e10e50..bdf6b7d 100644 --- a/test/test_circ.cpp +++ b/test/test_circ.cpp @@ -238,7 +238,7 @@ private slots: void test_prod_cons_1v3(); void test_prod_cons_performance(); void test_queue(); -} unit__; +} /*unit__*/; #include "test_circ.moc" diff --git a/test/test_mem.cpp b/test/test_mem.cpp index 44d060f..88a81a9 100644 --- a/test/test_mem.cpp +++ b/test/test_mem.cpp @@ -172,9 +172,9 @@ struct test_performance { }; void Unit::test_static() { - test_performance::start(); - test_performance::start(); - test_performance::start(); + //test_performance::start(); + //test_performance::start(); + //test_performance::start(); } void Unit::test_pool() {