Bitcoin Core  21.99.0
P2P Digital Currency
lockedpool.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 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 <stdint.h>
9 #include <list>
10 #include <map>
11 #include <mutex>
12 #include <memory>
13 #include <unordered_map>
14 
20 {
21 public:
22  virtual ~LockedPageAllocator() {}
31  virtual void* AllocateLocked(size_t len, bool *lockingSuccess) = 0;
32 
36  virtual void FreeLocked(void* addr, size_t len) = 0;
37 
42  virtual size_t GetLimit() = 0;
43 };
44 
45 /* An arena manages a contiguous region of memory by dividing it into
46  * chunks.
47  */
48 class Arena
49 {
50 public:
51  Arena(void *base, size_t size, size_t alignment);
52  virtual ~Arena();
53 
54  Arena(const Arena& other) = delete; // non construction-copyable
55  Arena& operator=(const Arena&) = delete; // non copyable
56 
58  struct Stats
59  {
60  size_t used;
61  size_t free;
62  size_t total;
63  size_t chunks_used;
64  size_t chunks_free;
65  };
66 
71  void* alloc(size_t size);
72 
77  void free(void *ptr);
78 
80  Stats stats() const;
81 
82 #ifdef ARENA_DEBUG
83  void walk() const;
84 #endif
85 
90  bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
91 private:
92  typedef std::multimap<size_t, char*> SizeToChunkSortedMap;
95 
96  typedef std::unordered_map<char*, SizeToChunkSortedMap::const_iterator> ChunkToSizeMap;
101 
103  std::unordered_map<char*, size_t> chunks_used;
104 
106  char* base;
108  char* end;
110  size_t alignment;
111 };
112 
127 {
128 public:
134  static const size_t ARENA_SIZE = 256*1024;
138  static const size_t ARENA_ALIGN = 16;
139 
142  typedef bool (*LockingFailed_Callback)();
143 
145  struct Stats
146  {
147  size_t used;
148  size_t free;
149  size_t total;
150  size_t locked;
151  size_t chunks_used;
152  size_t chunks_free;
153  };
154 
162  explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = nullptr);
163  ~LockedPool();
164 
165  LockedPool(const LockedPool& other) = delete; // non construction-copyable
166  LockedPool& operator=(const LockedPool&) = delete; // non copyable
167 
172  void* alloc(size_t size);
173 
178  void free(void *ptr);
179 
181  Stats stats() const;
182 private:
183  std::unique_ptr<LockedPageAllocator> allocator;
184 
186  class LockedPageArena: public Arena
187  {
188  public:
189  LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align);
191  private:
192  void *base;
193  size_t size;
195  };
196 
197  bool new_arena(size_t size, size_t align);
198 
199  std::list<LockedPageArena> arenas;
204  mutable std::mutex mutex;
205 };
206 
219 {
220 public:
223  {
224  static std::once_flag init_flag;
225  std::call_once(init_flag, LockedPoolManager::CreateInstance);
227  }
228 
229 private:
230  explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
231 
233  static void CreateInstance();
235  static bool LockingFailed();
236 
238 };
239 
240 #endif // BITCOIN_SUPPORT_LOCKEDPOOL_H
LockedPoolManager
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
Definition: lockedpool.h:218
LockedPool::operator=
LockedPool & operator=(const LockedPool &)=delete
LockedPool::Stats::free
size_t free
Definition: lockedpool.h:148
LockedPool::new_arena
bool new_arena(size_t size, size_t align)
Definition: lockedpool.cpp:345
LockedPool::ARENA_ALIGN
static const size_t ARENA_ALIGN
Chunk alignment.
Definition: lockedpool.h:138
LockedPool::LockedPageArena::LockedPageArena
LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align)
Definition: lockedpool.cpp:374
Arena::Stats::chunks_free
size_t chunks_free
Definition: lockedpool.h:64
LockedPool::LockedPageArena::size
size_t size
Definition: lockedpool.h:193
LockedPool::LockedPageArena::allocator
LockedPageAllocator * allocator
Definition: lockedpool.h:194
Arena::Stats::chunks_used
size_t chunks_used
Definition: lockedpool.h:63
LockedPool::allocator
std::unique_ptr< LockedPageAllocator > allocator
Definition: lockedpool.h:183
LockedPoolManager::Instance
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:222
LockedPool::Stats::chunks_free
size_t chunks_free
Definition: lockedpool.h:152
LockedPool::lf_cb
LockingFailed_Callback lf_cb
Definition: lockedpool.h:200
Arena::Stats::total
size_t total
Definition: lockedpool.h:62
Arena::end
char * end
End address of arena.
Definition: lockedpool.h:108
LockedPool::Stats::total
size_t total
Definition: lockedpool.h:149
LockedPool::Stats::locked
size_t locked
Definition: lockedpool.h:150
LockedPageAllocator::FreeLocked
virtual void FreeLocked(void *addr, size_t len)=0
Unlock and free memory pages.
Arena::chunks_free_end
ChunkToSizeMap chunks_free_end
Map from end of free chunk to its node in size_to_free_chunk.
Definition: lockedpool.h:100
Arena::base
char * base
Base address of arena.
Definition: lockedpool.h:106
Arena::operator=
Arena & operator=(const Arena &)=delete
LockedPageAllocator::~LockedPageAllocator
virtual ~LockedPageAllocator()
Definition: lockedpool.h:22
LockedPool::arenas
std::list< LockedPageArena > arenas
Definition: lockedpool.h:199
Arena::~Arena
virtual ~Arena()
Definition: lockedpool.cpp:53
LockedPool::alloc
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:294
Arena::stats
Stats stats() const
Get arena usage statistics.
Definition: lockedpool.cpp:130
Arena::alignment
size_t alignment
Minimum chunk alignment.
Definition: lockedpool.h:110
Arena::Stats::free
size_t free
Definition: lockedpool.h:61
LockedPool::Stats::chunks_used
size_t chunks_used
Definition: lockedpool.h:151
LockedPool::Stats::used
size_t used
Definition: lockedpool.h:147
Arena::SizeToChunkSortedMap
std::multimap< size_t, char * > SizeToChunkSortedMap
Definition: lockedpool.h:92
Arena::size_to_free_chunk
SizeToChunkSortedMap size_to_free_chunk
Map to enable O(log(n)) best-fit allocation, as it's sorted by size.
Definition: lockedpool.h:94
LockedPool::Stats
Memory statistics.
Definition: lockedpool.h:145
Arena::Stats
Memory statistics.
Definition: lockedpool.h:58
Arena::chunks_free
ChunkToSizeMap chunks_free
Map from begin of free chunk to its node in size_to_free_chunk.
Definition: lockedpool.h:98
LockedPoolManager::_instance
static LockedPoolManager * _instance
Definition: lockedpool.h:237
LockedPool::~LockedPool
~LockedPool()
Definition: lockedpool.cpp:291
Arena::ChunkToSizeMap
std::unordered_map< char *, SizeToChunkSortedMap::const_iterator > ChunkToSizeMap
Definition: lockedpool.h:96
Arena
Definition: lockedpool.h:48
LockedPoolManager::LockingFailed
static bool LockingFailed()
Called when locking fails, warn the user here.
Definition: lockedpool.cpp:391
LockedPool::LockedPageArena::base
void * base
Definition: lockedpool.h:192
LockedPool::LockedPageArena
Create an arena from locked pages.
Definition: lockedpool.h:186
Arena::free
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:92
LockedPool::free
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:316
LockedPageAllocator::AllocateLocked
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
Arena::Arena
Arena(void *base, size_t size, size_t alignment)
Definition: lockedpool.cpp:44
Arena::addressInArena
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
Definition: lockedpool.h:90
LockedPool::ARENA_SIZE
static const size_t ARENA_SIZE
Size of one arena of locked memory.
Definition: lockedpool.h:134
LockedPool
Pool for locked memory chunks.
Definition: lockedpool.h:126
Arena::alloc
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:57
LockedPoolManager::CreateInstance
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
Definition: lockedpool.cpp:397
Arena::chunks_used
std::unordered_map< char *, size_t > chunks_used
Map from begin of used chunk to its size.
Definition: lockedpool.h:103
LockedPageAllocator::GetLimit
virtual size_t GetLimit()=0
Get the total limit on the amount of memory that may be locked by this process, in bytes.
LockedPool::stats
Stats stats() const
Get pool usage statistics.
Definition: lockedpool.cpp:330
Arena::Stats::used
size_t used
Definition: lockedpool.h:60
LockedPool::LockedPool
LockedPool(std::unique_ptr< LockedPageAllocator > allocator, LockingFailed_Callback lf_cb_in=nullptr)
Create a new LockedPool.
Definition: lockedpool.cpp:286
LockedPool::cumulative_bytes_locked
size_t cumulative_bytes_locked
Definition: lockedpool.h:201
LockedPoolManager::LockedPoolManager
LockedPoolManager(std::unique_ptr< LockedPageAllocator > allocator)
Definition: lockedpool.cpp:386
LockedPageAllocator
OS-dependent allocation and deallocation of locked/pinned memory pages.
Definition: lockedpool.h:19
LockedPool::LockedPageArena::~LockedPageArena
~LockedPageArena()
Definition: lockedpool.cpp:378
LockedPool::LockingFailed_Callback
bool(* LockingFailed_Callback)()
Callback when allocation succeeds but locking fails.
Definition: lockedpool.h:142
LockedPool::mutex
std::mutex mutex
Mutex protects access to this pool's data structures, including arenas.
Definition: lockedpool.h:204