PIVX Core  5.6.99
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 PIVX_SUPPORT_LOCKEDPOOL_H
6 #define PIVX_SUPPORT_LOCKEDPOOL_H
7 
8 #include <list>
9 #include <map>
10 #include <memory>
11 #include <mutex>
12 #include <stdexcept>
13 #include <stdint.h>
14 #include <unordered_map>
15 
21 {
22 public:
23  virtual ~LockedPageAllocator() {}
32  virtual void* AllocateLocked(size_t len, bool *lockingSuccess) = 0;
33 
37  virtual void FreeLocked(void* addr, size_t len) = 0;
38 
43  virtual size_t GetLimit() = 0;
44 };
45 
46 /* An arena manages a contiguous region of memory by dividing it into
47  * chunks.
48  */
49 class Arena
50 {
51 public:
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  {
61  size_t used;
62  size_t free;
63  size_t total;
64  size_t chunks_used;
65  size_t chunks_free;
66  };
67 
72  void* alloc(size_t size);
73 
78  void free(void *ptr);
79 
81  Stats stats() const;
82 
83 #ifdef ARENA_DEBUG
84  void walk() const;
85 #endif
86 
91  bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
92 private:
93  typedef std::multimap<size_t, char*> SizeToChunkSortedMap;
96 
97  typedef std::unordered_map<char*, SizeToChunkSortedMap::const_iterator> ChunkToSizeMap;
102 
104  std::unordered_map<char*, size_t> chunks_used;
105 
107  char* base;
109  char* end;
111  size_t alignment;
112 };
113 
128 {
129 public:
135  static const size_t ARENA_SIZE = 256*1024;
139  static const size_t ARENA_ALIGN = 16;
140 
143  typedef bool (*LockingFailed_Callback)();
144 
146  struct Stats
147  {
148  size_t used;
149  size_t free;
150  size_t total;
151  size_t locked;
152  size_t chunks_used;
153  size_t chunks_free;
154  };
155 
163  explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = 0);
164  ~LockedPool();
165 
166  LockedPool(const LockedPool& other) = delete; // non construction-copyable
167  LockedPool& operator=(const LockedPool&) = delete; // non copyable
168 
173  void* alloc(size_t size);
174 
179  void free(void *ptr);
180 
182  Stats stats() const;
183 private:
184  std::unique_ptr<LockedPageAllocator> allocator;
185 
187  class LockedPageArena: public Arena
188  {
189  public:
190  LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align);
192  private:
193  void *base;
194  size_t size;
196  };
197 
198  bool new_arena(size_t size, size_t align);
199 
200  std::list<LockedPageArena> arenas;
205  mutable std::mutex mutex;
206 };
207 
220 {
221 public:
224  {
227  }
228 
229 private:
230  explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
231 
233  static void CreateInstance();
235  static bool LockingFailed();
236 
238  static std::once_flag init_flag;
239 };
240 
241 #endif // PIVX_SUPPORT_LOCKEDPOOL_H
char * base
Base address of arena.
Definition: lockedpool.h:107
size_t alignment
Minimum chunk alignment.
Definition: lockedpool.h:111
ChunkToSizeMap chunks_free_end
Map from end of free chunk to its node in size_to_free_chunk.
Definition: lockedpool.h:101
std::unordered_map< char *, SizeToChunkSortedMap::const_iterator > ChunkToSizeMap
Definition: lockedpool.h:97
char * end
End address of arena.
Definition: lockedpool.h:109
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:63
std::unordered_map< char *, size_t > chunks_used
Map from begin of used chunk to its size.
Definition: lockedpool.h:104
SizeToChunkSortedMap size_to_free_chunk
Map to enable O(log(n)) best-fit allocation, as it's sorted by size.
Definition: lockedpool.h:95
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
Definition: lockedpool.h:91
Arena & operator=(const Arena &)=delete
std::multimap< size_t, char * > SizeToChunkSortedMap
Definition: lockedpool.h:93
Arena(void *base, size_t size, size_t alignment)
Definition: lockedpool.cpp:50
Stats stats() const
Get arena usage statistics.
Definition: lockedpool.cpp:136
ChunkToSizeMap chunks_free
Map from begin of free chunk to its node in size_to_free_chunk.
Definition: lockedpool.h:99
virtual ~Arena()
Definition: lockedpool.cpp:59
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:21
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
virtual ~LockedPageAllocator()
Definition: lockedpool.h:23
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:188
LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, size_t size, size_t align)
Definition: lockedpool.cpp:380
LockedPageAllocator * allocator
Definition: lockedpool.h:195
Pool for locked memory chunks.
Definition: lockedpool.h:128
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:322
Stats stats() const
Get pool usage statistics.
Definition: lockedpool.cpp:336
std::unique_ptr< LockedPageAllocator > allocator
Definition: lockedpool.h:184
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:300
size_t cumulative_bytes_locked
Definition: lockedpool.h:202
LockingFailed_Callback lf_cb
Definition: lockedpool.h:201
std::list< LockedPageArena > arenas
Definition: lockedpool.h:200
bool new_arena(size_t size, size_t align)
Definition: lockedpool.cpp:351
static const size_t ARENA_ALIGN
Chunk alignment.
Definition: lockedpool.h:139
static const size_t ARENA_SIZE
Size of one arena of locked memory.
Definition: lockedpool.h:135
LockedPool(const LockedPool &other)=delete
std::mutex mutex
Mutex protects access to this pool's data structures, including arenas.
Definition: lockedpool.h:205
bool(* LockingFailed_Callback)()
Callback when allocation succeeds but locking fails.
Definition: lockedpool.h:143
LockedPool(std::unique_ptr< LockedPageAllocator > allocator, LockingFailed_Callback lf_cb_in=0)
Create a new LockedPool.
Definition: lockedpool.cpp:292
LockedPool & operator=(const LockedPool &)=delete
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
Definition: lockedpool.h:220
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:223
LockedPoolManager(std::unique_ptr< LockedPageAllocator > allocator)
Definition: lockedpool.cpp:392
static std::once_flag init_flag
Definition: lockedpool.h:238
static bool LockingFailed()
Called when locking fails, warn the user here.
Definition: lockedpool.cpp:397
static LockedPoolManager * _instance
Definition: lockedpool.h:237
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
Definition: lockedpool.cpp:403
Memory statistics.
Definition: lockedpool.h:60
size_t used
Definition: lockedpool.h:61
size_t chunks_used
Definition: lockedpool.h:64
size_t total
Definition: lockedpool.h:63
size_t free
Definition: lockedpool.h:62
size_t chunks_free
Definition: lockedpool.h:65
Memory statistics.
Definition: lockedpool.h:147