5 #ifndef BITCOIN_SUPPORT_LOCKEDPOOL_H 6 #define BITCOIN_SUPPORT_LOCKEDPOOL_H 13 #include <unordered_map> 31 virtual void *
AllocateLocked(
size_t len,
bool *lockingSuccess) = 0;
37 virtual void FreeLocked(
void *addr,
size_t len) = 0;
52 Arena(
void *base,
size_t size,
size_t alignment);
72 void *alloc(
size_t size);
100 typedef std::unordered_map<char *, SizeToChunkSortedMap::const_iterator>
140 static const size_t ARENA_SIZE = 256 * 1024;
145 static const size_t ARENA_ALIGN = 16;
150 typedef bool (*LockingFailed_Callback)();
171 explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator,
172 LockingFailed_Callback lf_cb_in =
nullptr);
183 void *alloc(
size_t size);
190 void free(
void *ptr);
202 size_t size,
size_t align);
211 bool new_arena(
size_t size,
size_t align);
246 static void CreateInstance();
248 static bool LockingFailed();
254 #endif // BITCOIN_SUPPORT_LOCKEDPOOL_H
static std::once_flag init_flag
size_t alignment
Minimum chunk alignment.
std::mutex mutex
Mutex protects access to this pool's data structures, including arenas.
std::unordered_map< char *, SizeToChunkSortedMap::const_iterator > ChunkToSizeMap
std::list< LockedPageArena > arenas
static LockedPoolManager & Instance()
Return the current instance, or create it once.
std::unordered_map< char *, size_t > chunks_used
Map from begin of used chunk to its size.
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
ChunkToSizeMap chunks_free
Map from begin of free chunk to its node in size_to_free_chunk.
LockingFailed_Callback lf_cb
SizeToChunkSortedMap size_to_free_chunk
Map to enable O(log(n)) best-fit allocation, as it's sorted by size.
OS-dependent allocation and deallocation of locked/pinned memory pages.
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
LockedPageAllocator * allocator
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
char * end
End address of arena.
static LockedPoolManager * _instance
virtual void FreeLocked(void *addr, size_t len)=0
Unlock and free memory pages.
virtual ~LockedPageAllocator()
Pool for locked memory chunks.
Create an arena from locked pages.
char * base
Base address of arena.
virtual size_t GetLimit()=0
Get the total limit on the amount of memory that may be locked by this process, in bytes...
std::multimap< size_t, char * > SizeToChunkSortedMap
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
An arena manages a contiguous region of memory by dividing it into chunks.
size_t cumulative_bytes_locked
ChunkToSizeMap chunks_free_end
Map from end of free chunk to its node in size_to_free_chunk.
std::unique_ptr< LockedPageAllocator > allocator