Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
chainbase_node_allocator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <boost/interprocess/offset_ptr.hpp>
5
7
8namespace chainbase {
9
10 namespace bip = boost::interprocess;
11
12 template<typename T, typename S>
13 class chainbase_node_allocator {
14 public:
15 using value_type = T;
16 using pointer = bip::offset_ptr<T>;
18 chainbase_node_allocator(segment_manager* manager) : _manager{manager} {}
19 chainbase_node_allocator(const chainbase_node_allocator& other) : _manager(other._manager) {}
20 template<typename U>
21 chainbase_node_allocator(const chainbase_node_allocator<U, S>& other) : _manager(other._manager) {}
22 pointer allocate(std::size_t num) {
23 if (num == 1) {
24 if (_freelist == nullptr) {
25 get_some();
26 }
27 list_item* result = &*_freelist;
28 _freelist = _freelist->_next;
29 result->~list_item();
30 return pointer{(T*)result};
31 } else {
32 return pointer{(T*)_manager->allocate(num*sizeof(T))};
33 }
34 }
35 void deallocate(const pointer& p, std::size_t num) {
36 if (num == 1) {
37 _freelist = new (&*p) list_item{_freelist};
38 } else {
39 _manager->deallocate(&*p);
40 }
41 }
42 bool operator==(const chainbase_node_allocator& other) const { return this == &other; }
43 bool operator!=(const chainbase_node_allocator& other) const { return this != &other; }
44 segment_manager* get_segment_manager() const { return _manager.get(); }
45 private:
46 template<typename T2, typename S2>
48 void get_some() {
49 static_assert(sizeof(T) >= sizeof(list_item), "Too small for free list");
50 static_assert(sizeof(T) % alignof(list_item) == 0, "Bad alignment for free list");
51 char* result = (char*)_manager->allocate(sizeof(T) * 64);
52 _freelist = bip::offset_ptr<list_item>{(list_item*)result};
53 for(int i = 0; i < 63; ++i) {
54 char* next = result + sizeof(T);
55 new(result) list_item{bip::offset_ptr<list_item>{(list_item*)next}};
56 result = next;
57 }
58 new(result) list_item{nullptr};
59 }
60 struct list_item { bip::offset_ptr<list_item> _next; };
61 bip::offset_ptr<pinnable_mapped_file::segment_manager> _manager;
62 bip::offset_ptr<list_item> _freelist{};
63 };
64
65} // namepsace chainbase
const mie::Vuint & p
Definition bn.cpp:27
bool operator==(const chainbase_node_allocator &other) const
chainbase_node_allocator(const chainbase_node_allocator &other)
void deallocate(const pointer &p, std::size_t num)
bool operator!=(const chainbase_node_allocator &other) const
chainbase_node_allocator(const chainbase_node_allocator< U, S > &other)
pinnable_mapped_file::segment_manager segment_manager
bip::managed_mapped_file::segment_manager segment_manager
uint32_t next(octet_iterator &it, octet_iterator end)
Definition checked.h:137
#define T(meth, val, expected)