5#ifndef BITCOIN_SUPPORT_LOCKEDPOOL_H
6#define BITCOIN_SUPPORT_LOCKEDPOOL_H
13#include <unordered_map>
72 void *
alloc(
size_t size);
100 typedef std::unordered_map<char *, SizeToChunkSortedMap::const_iterator>
183 void *
alloc(
size_t size);
190 void free(
void *ptr);
An arena manages a contiguous region of memory by dividing it into chunks.
char * base
Base address of arena.
size_t alignment
Minimum chunk alignment.
ChunkToSizeMap chunks_free_end
Map from end of free chunk to its node in size_to_free_chunk.
char * end
End address of arena.
Arena & operator=(const Arena &)=delete
void * alloc(size_t size)
Allocate size bytes from this arena.
SizeToChunkSortedMap size_to_free_chunk
Map to enable O(log(n)) best-fit allocation, as it's sorted by size.
bool addressInArena(void *ptr) const
Return whether a pointer points inside this arena.
std::unordered_map< char *, SizeToChunkSortedMap::const_iterator > ChunkToSizeMap
Stats stats() const
Get arena usage statistics.
std::multimap< size_t, char * > SizeToChunkSortedMap
std::unordered_map< char *, size_t > chunks_used
Map from begin of used chunk to its size.
ChunkToSizeMap chunks_free
Map from begin of free chunk to its node in size_to_free_chunk.
void free(void *ptr)
Free a previously allocated chunk of memory.
Arena(const Arena &other)=delete
OS-dependent allocation and deallocation of locked/pinned memory pages.
virtual void * AllocateLocked(size_t len, bool *lockingSuccess)=0
Allocate and lock memory pages.
virtual ~LockedPageAllocator()
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.
LockedPageAllocator * allocator
Pool for locked memory chunks.
void free(void *ptr)
Free a previously allocated chunk of memory.
Stats stats() const
Get pool usage statistics.
std::unique_ptr< LockedPageAllocator > allocator
void * alloc(size_t size)
Allocate size bytes from this arena.
size_t cumulative_bytes_locked
LockedPool & operator=(const LockedPool &)=delete
LockingFailed_Callback lf_cb
std::list< LockedPageArena > arenas
bool new_arena(size_t size, size_t align)
static const size_t ARENA_ALIGN
Chunk alignment.
static const size_t ARENA_SIZE
Size of one arena of locked memory.
LockedPool(const LockedPool &other)=delete
std::mutex mutex
Mutex protects access to this pool's data structures, including arenas.
bool(* LockingFailed_Callback)()
Callback when allocation succeeds but locking fails.
Singleton class to keep track of locked (ie, non-swappable) memory, for use in std::allocator templat...
static bool LockingFailed()
Called when locking fails, warn the user here.
static LockedPoolManager * _instance
static void CreateInstance()
Create a new LockedPoolManager specialized to the OS.
static LockedPoolManager & Instance()
Return the current instance, or create it once.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...