Bitcoin ABC 0.30.7
P2P Digital Currency
lockedpool.h
Go to the documentation of this file.
1// Copyright (c) 2016 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_SUPPORT_LOCKEDPOOL_H
6#define BITCOIN_SUPPORT_LOCKEDPOOL_H
7
8#include <cstddef>
9#include <list>
10#include <map>
11#include <memory>
12#include <mutex>
13#include <unordered_map>
14
20public:
31 virtual void *AllocateLocked(size_t len, bool *lockingSuccess) = 0;
32
37 virtual void FreeLocked(void *addr, size_t len) = 0;
38
44 virtual size_t GetLimit() = 0;
45};
46
50class Arena {
51public:
52 Arena(void *base, size_t size, size_t alignment);
53 virtual ~Arena();
54
55 Arena(const Arena &other) = delete; // non construction-copyable
56 Arena &operator=(const Arena &) = delete; // non copyable
57
59 struct Stats {
60 size_t used;
61 size_t free;
62 size_t total;
65 };
66
72 void *alloc(size_t size);
73
79 void free(void *ptr);
80
82 Stats stats() const;
83
84#ifdef ARENA_DEBUG
85 void walk() const;
86#endif
87
93 bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
94
95private:
96 typedef std::multimap<size_t, char *> SizeToChunkSortedMap;
99
100 typedef std::unordered_map<char *, SizeToChunkSortedMap::const_iterator>
106
108 std::unordered_map<char *, size_t> chunks_used;
109
111 char *base;
113 char *end;
115 size_t alignment;
116};
117
133public:
140 static const size_t ARENA_SIZE = 256 * 1024;
145 static const size_t ARENA_ALIGN = 16;
146
150 typedef bool (*LockingFailed_Callback)();
151
153 struct Stats {
154 size_t used;
155 size_t free;
156 size_t total;
157 size_t locked;
160 };
161
171 explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator,
172 LockingFailed_Callback lf_cb_in = nullptr);
173 ~LockedPool();
174
175 LockedPool(const LockedPool &other) = delete; // non construction-copyable
176 LockedPool &operator=(const LockedPool &) = delete; // non copyable
177
183 void *alloc(size_t size);
184
190 void free(void *ptr);
191
193 Stats stats() const;
194
195private:
196 std::unique_ptr<LockedPageAllocator> allocator;
197
199 class LockedPageArena : public Arena {
200 public:
201 LockedPageArena(LockedPageAllocator *alloc_in, void *base_in,
202 size_t size, size_t align);
204
205 private:
206 void *base;
207 size_t size;
209 };
210
211 bool new_arena(size_t size, size_t align);
212
213 std::list<LockedPageArena> arenas;
219 mutable std::mutex mutex;
220};
221
234public:
237 static std::once_flag init_flag;
238 std::call_once(init_flag, LockedPoolManager::CreateInstance);
240 }
241
242private:
243 explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
244
246 static void CreateInstance();
248 static bool LockingFailed();
249
251};
252
253#endif // BITCOIN_SUPPORT_LOCKEDPOOL_H
An arena manages a contiguous region of memory by dividing it into chunks.
Definition: lockedpool.h:50
char * base
Base address of arena.
Definition: lockedpool.h:111
size_t alignment
Minimum chunk alignment.
Definition: lockedpool.h:115
ChunkToSizeMap chunks_free_end
Map from end of free chunk to its node in size_to_free_chunk.
Definition: lockedpool.h:105
char * end
End address of arena.
Definition: lockedpool.h:113
Arena & operator=(const Arena &)=delete
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:57
SizeToChunkSortedMap size_to_free_chunk
Map to enable O(log(n)) best-fit allocation, as it's sorted by size.
Definition: lockedpool.h:98
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
Definition: lockedpool.h:93
std::unordered_map< char *, SizeToChunkSortedMap::const_iterator > ChunkToSizeMap
Definition: lockedpool.h:101
Arena(void *base, size_t size, size_t alignment)
Definition: lockedpool.cpp:46
Stats stats() const
Get arena usage statistics.
Definition: lockedpool.cpp:135
std::multimap< size_t, char * > SizeToChunkSortedMap
Definition: lockedpool.h:96
std::unordered_map< char *, size_t > chunks_used
Map from begin of used chunk to its size.
Definition: lockedpool.h:108
ChunkToSizeMap chunks_free
Map from begin of free chunk to its node in size_to_free_chunk.
Definition: lockedpool.h:103
virtual ~Arena()
Definition: lockedpool.cpp:55
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:98
Arena(const Arena &other)=delete
OS-dependent allocation and deallocation of locked/pinned memory pages.
Definition: lockedpool.h:19
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
virtual ~LockedPageAllocator()
Definition: lockedpool.h:21
virtual void FreeLocked(void *addr, size_t len)=0
Unlock and free memory pages.
virtual size_t GetLimit()=0
Get the total limit on the amount of memory that may be locked by this process, in bytes.
Create an arena from locked pages.
Definition: lockedpool.h:199
LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align)
Definition: lockedpool.cpp:377
LockedPageAllocator * allocator
Definition: lockedpool.h:208
Pool for locked memory chunks.
Definition: lockedpool.h:132
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:318
Stats stats() const
Get pool usage statistics.
Definition: lockedpool.cpp:332
std::unique_ptr< LockedPageAllocator > allocator
Definition: lockedpool.h:196
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:296
size_t cumulative_bytes_locked
Definition: lockedpool.h:215
LockedPool(std::unique_ptr< LockedPageAllocator > allocator, LockingFailed_Callback lf_cb_in=nullptr)
Create a new LockedPool.
Definition: lockedpool.cpp:290
LockedPool & operator=(const LockedPool &)=delete
LockingFailed_Callback lf_cb
Definition: lockedpool.h:214
std::list< LockedPageArena > arenas
Definition: lockedpool.h:213
bool new_arena(size_t size, size_t align)
Definition: lockedpool.cpp:346
static const size_t ARENA_ALIGN
Chunk alignment.
Definition: lockedpool.h:145
static const size_t ARENA_SIZE
Size of one arena of locked memory.
Definition: lockedpool.h:140
LockedPool(const LockedPool &other)=delete
std::mutex mutex
Mutex protects access to this pool's data structures, including arenas.
Definition: lockedpool.h:219
bool(* LockingFailed_Callback)()
Callback when allocation succeeds but locking fails.
Definition: lockedpool.h:150
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
Definition: lockedpool.h:233
LockedPoolManager(std::unique_ptr< LockedPageAllocator > allocator)
Definition: lockedpool.cpp:389
static bool LockingFailed()
Called when locking fails, warn the user here.
Definition: lockedpool.cpp:393
static LockedPoolManager * _instance
Definition: lockedpool.h:250
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
Definition: lockedpool.cpp:398
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:236
Memory statistics.
Definition: lockedpool.h:59
size_t used
Definition: lockedpool.h:60
size_t chunks_used
Definition: lockedpool.h:63
size_t total
Definition: lockedpool.h:62
size_t free
Definition: lockedpool.h:61
size_t chunks_free
Definition: lockedpool.h:64
Memory statistics.
Definition: lockedpool.h:153