From 52f447111ce6aeed34e0ec6e02d1f2ae90ab5a70 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Fri, 18 Jan 2019 18:45:19 +0800 Subject: [PATCH] page_fixed_alloc --- src/memory/alloc.hpp | 43 +++++++++++++++++++++++++++++------------ src/memory/resource.hpp | 2 +- src/memory/wrapper.hpp | 5 ++--- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/memory/alloc.hpp b/src/memory/alloc.hpp index 154e61d..4fb4d35 100644 --- a/src/memory/alloc.hpp +++ b/src/memory/alloc.hpp @@ -25,6 +25,10 @@ public: static void free(void* p, std::size_t /*size*/) { free(p); } + + static std::size_t size_of(void* /*p*/) { + return 0; + } }; //////////////////////////////////////////////////////////////// @@ -43,6 +47,10 @@ protected: public: void free(void* /*p*/) {} void free(void* /*p*/, std::size_t) {} + + constexpr std::size_t size_of(void* /*p*/) const { + return 0; + } }; } // namespace detail @@ -92,14 +100,13 @@ public: namespace detail { -class fixed_pool_base { +class fixed_alloc_base { protected: std::size_t init_expand_; - std::size_t iter_; void* cursor_; void init(std::size_t init_expand) { - iter_ = init_expand_ = init_expand; + init_expand_ = init_expand; cursor_ = nullptr; } @@ -126,7 +133,7 @@ public: } // namespace detail template > -class fixed_pool : public detail::fixed_pool_base { +class fixed_alloc : public detail::fixed_alloc_base { public: using alloc_policy = AllocP; @@ -142,26 +149,25 @@ private: alloc_policy alloc_; void expand() { - auto p = node_p(cursor_ = alloc_.alloc(block_size * iter_)); - for (std::size_t i = 0; i < iter_ - 1; ++i) + auto p = node_p(cursor_ = alloc_.alloc(block_size)); + auto size = alloc_.size_of(p); + if (size > 0) for (std::size_t i = 0; i < (size / block_size) - 1; ++i) p = node_p((*p) = reinterpret_cast(p) + block_size); (*p) = nullptr; - iter_ *= 2; } public: - explicit fixed_pool(std::size_t init_expand = 1) { + explicit fixed_alloc(std::size_t init_expand = 1) { init(init_expand); } - fixed_pool(fixed_pool&& rhs) { this->swap(rhs); } - fixed_pool& operator=(fixed_pool&& rhs) { this->swap(rhs); return (*this); } + fixed_alloc(fixed_alloc&& rhs) { this->swap(rhs); } + fixed_alloc& operator=(fixed_alloc&& rhs) { this->swap(rhs); return (*this); } public: - void swap(fixed_pool& rhs) { + void swap(fixed_alloc& rhs) { std::swap(this->alloc_ , rhs.alloc_); std::swap(this->init_expand_, rhs.init_expand_); - std::swap(this->iter_ , rhs.iter_); std::swap(this->cursor_ , rhs.cursor_); } @@ -170,6 +176,10 @@ public: init(init_expand_); } + constexpr std::size_t size_of(void* /*p*/) const { + return block_size; + } + void* alloc() { if (cursor_ == nullptr) expand(); void* p = cursor_; @@ -182,5 +192,14 @@ public: } }; +//////////////////////////////////////////////////////////////// +/// page memory allocation +//////////////////////////////////////////////////////////////// + +using page_alloc = fixed_alloc<4096>; + +template +using page_fixed_alloc = fixed_alloc; + } // namespace mem } // namespace ipc diff --git a/src/memory/resource.hpp b/src/memory/resource.hpp index aee1676..7f77d7f 100644 --- a/src/memory/resource.hpp +++ b/src/memory/resource.hpp @@ -39,7 +39,7 @@ inline void static_for(std::index_sequence, F&& f) { template auto& fixed() { - static synchronized> pool; + static synchronized> pool; return pool; } diff --git a/src/memory/wrapper.hpp b/src/memory/wrapper.hpp index d97da4e..bd797f1 100644 --- a/src/memory/wrapper.hpp +++ b/src/memory/wrapper.hpp @@ -14,7 +14,6 @@ #include "def.h" #include "rw_lock.h" -#include "tls_pointer.h" namespace ipc { @@ -85,8 +84,8 @@ private: }; auto& alc_info() { - static tls::pointer alc; - return *alc.create(this); + thread_local alloc_t alc { this }; + return alc; } public: