Raven Core  3.0.0
P2P Digital Currency
lockedpool.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_SUPPORT_LOCKEDPOOL_H
7 #define RAVEN_SUPPORT_LOCKEDPOOL_H
8 
9 #include <stdint.h>
10 #include <list>
11 #include <map>
12 #include <mutex>
13 #include <memory>
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:
95  std::map<char*, size_t> chunks_free;
96  std::map<char*, size_t> chunks_used;
98  char* base;
100  char* end;
102  size_t alignment;
103 };
104 
119 {
120 public:
126  static const size_t ARENA_SIZE = 256*1024;
130  static const size_t ARENA_ALIGN = 16;
131 
134  typedef bool (*LockingFailed_Callback)();
135 
137  struct Stats
138  {
139  size_t used;
140  size_t free;
141  size_t total;
142  size_t locked;
143  size_t chunks_used;
144  size_t chunks_free;
145  };
146 
154  explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = nullptr);
155  ~LockedPool();
156 
157  LockedPool(const LockedPool& other) = delete; // non construction-copyable
158  LockedPool& operator=(const LockedPool&) = delete; // non copyable
159 
164  void* alloc(size_t size);
165 
170  void free(void *ptr);
171 
173  Stats stats() const;
174 private:
175  std::unique_ptr<LockedPageAllocator> allocator;
176 
178  class LockedPageArena: public Arena
179  {
180  public:
181  LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align);
182  ~LockedPageArena();
183  private:
184  void *base;
185  size_t size;
187  };
188 
189  bool new_arena(size_t size, size_t align);
190 
191  std::list<LockedPageArena> arenas;
192  LockingFailed_Callback lf_cb;
196  mutable std::mutex mutex;
197 };
198 
211 {
212 public:
215  {
218  }
219 
220 private:
221  explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
222 
224  static void CreateInstance();
226  static bool LockingFailed();
227 
229  static std::once_flag init_flag;
230 };
231 
232 #endif // RAVEN_SUPPORT_LOCKEDPOOL_H
size_t chunks_free
Definition: lockedpool.h:64
size_t chunks_used
Definition: lockedpool.h:63
static std::once_flag init_flag
Definition: lockedpool.h:229
size_t used
Definition: lockedpool.h:60
size_t alignment
Minimum chunk alignment.
Definition: lockedpool.h:102
std::mutex mutex
Mutex protects access to this pool&#39;s data structures, including arenas.
Definition: lockedpool.h:196
std::list< LockedPageArena > arenas
Definition: lockedpool.h:191
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:214
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
LockingFailed_Callback lf_cb
Definition: lockedpool.h:192
size_t total
Definition: lockedpool.h:62
OS-dependent allocation and deallocation of locked/pinned memory pages.
Definition: lockedpool.h:19
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
Definition: lockedpool.h:90
LockedPageAllocator * allocator
Definition: lockedpool.h:186
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
Definition: lockedpool.h:210
char * end
End address of arena.
Definition: lockedpool.h:100
static LockedPoolManager * _instance
Definition: lockedpool.h:228
virtual void FreeLocked(void *addr, size_t len)=0
Unlock and free memory pages.
virtual ~LockedPageAllocator()
Definition: lockedpool.h:22
std::map< char *, size_t > chunks_used
Definition: lockedpool.h:96
size_t free
Definition: lockedpool.h:61
Pool for locked memory chunks.
Definition: lockedpool.h:118
Create an arena from locked pages.
Definition: lockedpool.h:178
char * base
Base address of arena.
Definition: lockedpool.h:98
virtual size_t GetLimit()=0
Get the total limit on the amount of memory that may be locked by this process, in bytes...
Memory statistics.
Definition: lockedpool.h:58
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
Definition: lockedpool.cpp:372
size_t cumulative_bytes_locked
Definition: lockedpool.h:193
std::map< char *, size_t > chunks_free
Map of chunk address to chunk information.
Definition: lockedpool.h:95
std::unique_ptr< LockedPageAllocator > allocator
Definition: lockedpool.h:175
Memory statistics.
Definition: lockedpool.h:137