Fabcoin Core  0.16.2
P2P Digital Currency
lockedpool.h
Go to the documentation of this file.
1 // Copyright (c)2016-2017 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 FABCOIN_SUPPORT_LOCKEDPOOL_H
6 #define FABCOIN_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 
54  struct Stats
55  {
56  size_t used;
57  size_t free;
58  size_t total;
59  size_t chunks_used;
60  size_t chunks_free;
61  };
62 
67  void* alloc(size_t size);
68 
73  void free(void *ptr);
74 
76  Stats stats() const;
77 
78 #ifdef ARENA_DEBUG
79  void walk() const;
80 #endif
81 
86  bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
87 private:
88  Arena(const Arena& other) = delete; // non construction-copyable
89  Arena& operator=(const Arena&) = delete; // non copyable
90 
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  LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = 0);
154  ~LockedPool();
155 
160  void* alloc(size_t size);
161 
166  void free(void *ptr);
167 
169  Stats stats() const;
170 private:
171  LockedPool(const LockedPool& other) = delete; // non construction-copyable
172  LockedPool& operator=(const LockedPool&) = delete; // non copyable
173 
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);
181  ~LockedPageArena();
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;
191  LockingFailed_Callback lf_cb;
195  mutable std::mutex mutex;
196 };
197 
210 {
211 public:
214  {
217  }
218 
219 private:
220  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 // FABCOIN_SUPPORT_LOCKEDPOOL_H
size_t chunks_free
Definition: lockedpool.h:60
size_t chunks_used
Definition: lockedpool.h:59
static std::once_flag init_flag
Definition: lockedpool.h:228
size_t used
Definition: lockedpool.h:56
size_t alignment
Minimum chunk alignment.
Definition: lockedpool.h:101
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
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
LockingFailed_Callback lf_cb
Definition: lockedpool.h:191
size_t total
Definition: lockedpool.h:58
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
Definition: lockedpool.h:86
OS-dependent allocation and deallocation of locked/pinned memory pages.
Definition: lockedpool.h:18
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
char * end
End address of arena.
Definition: lockedpool.h:99
static LockedPoolManager * _instance
Definition: lockedpool.h:227
virtual void FreeLocked(void *addr, size_t len)=0
Unlock and free memory pages.
virtual ~LockedPageAllocator()
Definition: lockedpool.h:21
std::map< char *, size_t > chunks_used
Definition: lockedpool.h:95
size_t free
Definition: lockedpool.h:57
Pool for locked memory chunks.
Definition: lockedpool.h:117
Create an arena from locked pages.
Definition: lockedpool.h:177
char * base
Base address of arena.
Definition: lockedpool.h:97
uint8_t const size_t const size
Definition: sha3.h:20
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:54
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
Definition: lockedpool.cpp:371
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
std::unique_ptr< LockedPageAllocator > allocator
Definition: lockedpool.h:174
Memory statistics.
Definition: lockedpool.h:136