Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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 <stdint.h>
9 #include <list>
10 #include <map>
11 #include <mutex>
12 #include <memory>
13 
19 {
20 public:
21  virtual ~LockedPageAllocator() {}
30  virtual void* AllocateLocked(size_t len, bool *lockingSuccess) = 0;
31 
35  virtual void FreeLocked(void* addr, size_t len) = 0;
36 
41  virtual size_t GetLimit() = 0;
42 };
43 
44 /* An arena manages a contiguous region of memory by dividing it into
45  * chunks.
46  */
47 class Arena
48 {
49 public:
50  Arena(void *base, size_t size, size_t alignment);
51  virtual ~Arena();
52 
53  Arena(const Arena& other) = delete; // non construction-copyable
54  Arena& operator=(const Arena&) = delete; // non copyable
55 
57  struct Stats
58  {
59  size_t used;
60  size_t free;
61  size_t total;
62  size_t chunks_used;
63  size_t chunks_free;
64  };
65 
70  void* alloc(size_t size);
71 
76  void free(void *ptr);
77 
79  Stats stats() const;
80 
81 #ifdef ARENA_DEBUG
82  void walk() const;
83 #endif
84 
89  bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
90 private:
94  std::map<char*, size_t> chunks_free;
95  std::map<char*, size_t> chunks_used;
97  char* base;
99  char* end;
101  size_t alignment;
102 };
103 
118 {
119 public:
125  static const size_t ARENA_SIZE = 256*1024;
129  static const size_t ARENA_ALIGN = 16;
130 
133  typedef bool (*LockingFailed_Callback)();
134 
136  struct Stats
137  {
138  size_t used;
139  size_t free;
140  size_t total;
141  size_t locked;
142  size_t chunks_used;
143  size_t chunks_free;
144  };
145 
153  explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = nullptr);
154  ~LockedPool();
155 
156  LockedPool(const LockedPool& other) = delete; // non construction-copyable
157  LockedPool& operator=(const LockedPool&) = delete; // non copyable
158 
163  void* alloc(size_t size);
164 
169  void free(void *ptr);
170 
172  Stats stats() const;
173 private:
174  std::unique_ptr<LockedPageAllocator> allocator;
175 
177  class LockedPageArena: public Arena
178  {
179  public:
180  LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align);
182  private:
183  void *base;
184  size_t size;
186  };
187 
188  bool new_arena(size_t size, size_t align);
189 
190  std::list<LockedPageArena> arenas;
195  mutable std::mutex mutex;
196 };
197 
210 {
211 public:
214  {
217  }
218 
219 private:
220  explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
221 
223  static void CreateInstance();
225  static bool LockingFailed();
226 
228  static std::once_flag init_flag;
229 };
230 
231 #endif // BITCOIN_SUPPORT_LOCKEDPOOL_H
size_t chunks_free
Definition: lockedpool.h:63
size_t chunks_used
Definition: lockedpool.h:62
static std::once_flag init_flag
Definition: lockedpool.h:228
size_t used
Definition: lockedpool.h:59
Arena & operator=(const Arena &)=delete
size_t alignment
Minimum chunk alignment.
Definition: lockedpool.h:101
bool(* LockingFailed_Callback)()
Callback when allocation succeeds but locking fails.
Definition: lockedpool.h:133
std::mutex mutex
Mutex protects access to this pool&#39;s data structures, including arenas.
Definition: lockedpool.h:195
std::list< LockedPageArena > arenas
Definition: lockedpool.h:190
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:213
static const size_t ARENA_ALIGN
Chunk alignment.
Definition: lockedpool.h:129
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
LockedPool(std::unique_ptr< LockedPageAllocator > allocator, LockingFailed_Callback lf_cb_in=nullptr)
Create a new LockedPool.
Definition: lockedpool.cpp:261
LockingFailed_Callback lf_cb
Definition: lockedpool.h:191
size_t total
Definition: lockedpool.h:61
LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align)
Definition: lockedpool.cpp:349
OS-dependent allocation and deallocation of locked/pinned memory pages.
Definition: lockedpool.h:18
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
Definition: lockedpool.h:89
LockedPageAllocator * allocator
Definition: lockedpool.h:185
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
Definition: lockedpool.h:209
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:58
char * end
End address of arena.
Definition: lockedpool.h:99
Stats stats() const
Get arena usage statistics.
Definition: lockedpool.cpp:113
static LockedPoolManager * _instance
Definition: lockedpool.h:227
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:269
virtual ~Arena()
Definition: lockedpool.cpp:54
virtual void FreeLocked(void *addr, size_t len)=0
Unlock and free memory pages.
static const size_t ARENA_SIZE
Size of one arena of locked memory.
Definition: lockedpool.h:125
virtual ~LockedPageAllocator()
Definition: lockedpool.h:21
std::map< char *, size_t > chunks_used
Definition: lockedpool.h:95
static bool LockingFailed()
Called when locking fails, warn the user here.
Definition: lockedpool.cpp:366
size_t free
Definition: lockedpool.h:60
Pool for locked memory chunks.
Definition: lockedpool.h:117
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:89
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:291
Create an arena from locked pages.
Definition: lockedpool.h:177
char * base
Base address of arena.
Definition: lockedpool.h:97
virtual size_t GetLimit()=0
Get the total limit on the amount of memory that may be locked by this process, in bytes...
bool new_arena(size_t size, size_t align)
Definition: lockedpool.cpp:320
Memory statistics.
Definition: lockedpool.h:57
LockedPoolManager(std::unique_ptr< LockedPageAllocator > allocator)
Definition: lockedpool.cpp:361
Stats stats() const
Get pool usage statistics.
Definition: lockedpool.cpp:305
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
Definition: lockedpool.cpp:372
size_t cumulative_bytes_locked
Definition: lockedpool.h:192
std::map< char *, size_t > chunks_free
Map of chunk address to chunk information.
Definition: lockedpool.h:94
Arena(void *base, size_t size, size_t alignment)
Definition: lockedpool.cpp:47
std::unique_ptr< LockedPageAllocator > allocator
Definition: lockedpool.h:174
Memory statistics.
Definition: lockedpool.h:136
LockedPool & operator=(const LockedPool &)=delete
Released under the MIT license