mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
page_fixed_alloc
This commit is contained in:
parent
c625a2e5ff
commit
52f447111c
@ -25,6 +25,10 @@ public:
|
|||||||
static void free(void* p, std::size_t /*size*/) {
|
static void free(void* p, std::size_t /*size*/) {
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::size_t size_of(void* /*p*/) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////
|
||||||
@ -43,6 +47,10 @@ protected:
|
|||||||
public:
|
public:
|
||||||
void free(void* /*p*/) {}
|
void free(void* /*p*/) {}
|
||||||
void free(void* /*p*/, std::size_t) {}
|
void free(void* /*p*/, std::size_t) {}
|
||||||
|
|
||||||
|
constexpr std::size_t size_of(void* /*p*/) const {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
@ -92,14 +100,13 @@ public:
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
class fixed_pool_base {
|
class fixed_alloc_base {
|
||||||
protected:
|
protected:
|
||||||
std::size_t init_expand_;
|
std::size_t init_expand_;
|
||||||
std::size_t iter_;
|
|
||||||
void* cursor_;
|
void* cursor_;
|
||||||
|
|
||||||
void init(std::size_t init_expand) {
|
void init(std::size_t init_expand) {
|
||||||
iter_ = init_expand_ = init_expand;
|
init_expand_ = init_expand;
|
||||||
cursor_ = nullptr;
|
cursor_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +133,7 @@ public:
|
|||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <std::size_t BlockSize, typename AllocP = scope_alloc<>>
|
template <std::size_t BlockSize, typename AllocP = scope_alloc<>>
|
||||||
class fixed_pool : public detail::fixed_pool_base {
|
class fixed_alloc : public detail::fixed_alloc_base {
|
||||||
public:
|
public:
|
||||||
using alloc_policy = AllocP;
|
using alloc_policy = AllocP;
|
||||||
|
|
||||||
@ -142,26 +149,25 @@ private:
|
|||||||
alloc_policy alloc_;
|
alloc_policy alloc_;
|
||||||
|
|
||||||
void expand() {
|
void expand() {
|
||||||
auto p = node_p(cursor_ = alloc_.alloc(block_size * iter_));
|
auto p = node_p(cursor_ = alloc_.alloc(block_size));
|
||||||
for (std::size_t i = 0; i < iter_ - 1; ++i)
|
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<byte_t*>(p) + block_size);
|
p = node_p((*p) = reinterpret_cast<byte_t*>(p) + block_size);
|
||||||
(*p) = nullptr;
|
(*p) = nullptr;
|
||||||
iter_ *= 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit fixed_pool(std::size_t init_expand = 1) {
|
explicit fixed_alloc(std::size_t init_expand = 1) {
|
||||||
init(init_expand);
|
init(init_expand);
|
||||||
}
|
}
|
||||||
|
|
||||||
fixed_pool(fixed_pool&& rhs) { this->swap(rhs); }
|
fixed_alloc(fixed_alloc&& rhs) { this->swap(rhs); }
|
||||||
fixed_pool& operator=(fixed_pool&& rhs) { this->swap(rhs); return (*this); }
|
fixed_alloc& operator=(fixed_alloc&& rhs) { this->swap(rhs); return (*this); }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void swap(fixed_pool& rhs) {
|
void swap(fixed_alloc& rhs) {
|
||||||
std::swap(this->alloc_ , rhs.alloc_);
|
std::swap(this->alloc_ , rhs.alloc_);
|
||||||
std::swap(this->init_expand_, rhs.init_expand_);
|
std::swap(this->init_expand_, rhs.init_expand_);
|
||||||
std::swap(this->iter_ , rhs.iter_);
|
|
||||||
std::swap(this->cursor_ , rhs.cursor_);
|
std::swap(this->cursor_ , rhs.cursor_);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,6 +176,10 @@ public:
|
|||||||
init(init_expand_);
|
init(init_expand_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr std::size_t size_of(void* /*p*/) const {
|
||||||
|
return block_size;
|
||||||
|
}
|
||||||
|
|
||||||
void* alloc() {
|
void* alloc() {
|
||||||
if (cursor_ == nullptr) expand();
|
if (cursor_ == nullptr) expand();
|
||||||
void* p = cursor_;
|
void* p = cursor_;
|
||||||
@ -182,5 +192,14 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
/// page memory allocation
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
using page_alloc = fixed_alloc<4096>;
|
||||||
|
|
||||||
|
template <std::size_t BlockSize>
|
||||||
|
using page_fixed_alloc = fixed_alloc<BlockSize, page_alloc>;
|
||||||
|
|
||||||
} // namespace mem
|
} // namespace mem
|
||||||
} // namespace ipc
|
} // namespace ipc
|
||||||
|
|||||||
@ -39,7 +39,7 @@ inline void static_for(std::index_sequence<I...>, F&& f) {
|
|||||||
|
|
||||||
template <std::size_t Size>
|
template <std::size_t Size>
|
||||||
auto& fixed() {
|
auto& fixed() {
|
||||||
static synchronized<fixed_pool<Size>> pool;
|
static synchronized<page_fixed_alloc<Size>> pool;
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
#include "rw_lock.h"
|
#include "rw_lock.h"
|
||||||
#include "tls_pointer.h"
|
|
||||||
|
|
||||||
namespace ipc {
|
namespace ipc {
|
||||||
|
|
||||||
@ -85,8 +84,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
auto& alc_info() {
|
auto& alc_info() {
|
||||||
static tls::pointer<alloc_t> alc;
|
thread_local alloc_t alc { this };
|
||||||
return *alc.create(this);
|
return alc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user