Assorted small changes to the locked pool manager
Cherry-picked from: - bitcoin/bitcoin#9233 - bitcoin/bitcoin#10483 - bitcoin/bitcoin#10645 - bitcoin/bitcoin#10969 - bitcoin/bitcoin#11351 Co-authored-by: fsb4000 <fsb4000@yandex.ru> Co-authored-by: practicalswift <practicalswift@users.noreply.github.com> Co-authored-by: Dan Raviv <dan@soundradix.com>
This commit is contained in:
parent
71b79b85d1
commit
e591f94fcf
|
@ -21,14 +21,14 @@ static void LockedPool(benchmark::State& state)
|
||||||
|
|
||||||
std::vector<void*> addr;
|
std::vector<void*> addr;
|
||||||
for (int x=0; x<ASIZE; ++x)
|
for (int x=0; x<ASIZE; ++x)
|
||||||
addr.push_back(0);
|
addr.push_back(nullptr);
|
||||||
uint32_t s = 0x12345678;
|
uint32_t s = 0x12345678;
|
||||||
while (state.KeepRunning()) {
|
while (state.KeepRunning()) {
|
||||||
for (int x=0; x<BITER; ++x) {
|
for (int x=0; x<BITER; ++x) {
|
||||||
int idx = s & (addr.size()-1);
|
int idx = s & (addr.size()-1);
|
||||||
if (s & 0x80000000) {
|
if (s & 0x80000000) {
|
||||||
b.free(addr[idx]);
|
b.free(addr[idx]);
|
||||||
addr[idx] = 0;
|
addr[idx] = nullptr;
|
||||||
} else if(!addr[idx]) {
|
} else if(!addr[idx]) {
|
||||||
addr[idx] = b.alloc((s >> 16) & (MSIZE-1));
|
addr[idx] = b.alloc((s >> 16) & (MSIZE-1));
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
LockedPoolManager* LockedPoolManager::_instance = NULL;
|
LockedPoolManager* LockedPoolManager::_instance = nullptr;
|
||||||
std::once_flag LockedPoolManager::init_flag;
|
std::once_flag LockedPoolManager::init_flag;
|
||||||
|
|
||||||
/*******************************************************************************/
|
/*******************************************************************************/
|
||||||
|
@ -97,7 +97,7 @@ void* Arena::alloc(size_t size)
|
||||||
|
|
||||||
void Arena::free(void *ptr)
|
void Arena::free(void *ptr)
|
||||||
{
|
{
|
||||||
// Freeing the NULL pointer is OK.
|
// Freeing the nullptr pointer is OK.
|
||||||
if (ptr == nullptr) {
|
if (ptr == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,9 @@ public:
|
||||||
Arena(void *base, size_t size, size_t alignment);
|
Arena(void *base, size_t size, size_t alignment);
|
||||||
virtual ~Arena();
|
virtual ~Arena();
|
||||||
|
|
||||||
|
Arena(const Arena& other) = delete; // non construction-copyable
|
||||||
|
Arena& operator=(const Arena&) = delete; // non copyable
|
||||||
|
|
||||||
/** Memory statistics. */
|
/** Memory statistics. */
|
||||||
struct Stats
|
struct Stats
|
||||||
{
|
{
|
||||||
|
@ -86,9 +89,6 @@ public:
|
||||||
*/
|
*/
|
||||||
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
|
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
|
||||||
private:
|
private:
|
||||||
Arena(const Arena& other) = delete; // non construction-copyable
|
|
||||||
Arena& operator=(const Arena&) = delete; // non copyable
|
|
||||||
|
|
||||||
typedef std::multimap<size_t, char*> SizeToChunkSortedMap;
|
typedef std::multimap<size_t, char*> SizeToChunkSortedMap;
|
||||||
/** Map to enable O(log(n)) best-fit allocation, as it's sorted by size */
|
/** Map to enable O(log(n)) best-fit allocation, as it's sorted by size */
|
||||||
SizeToChunkSortedMap size_to_free_chunk;
|
SizeToChunkSortedMap size_to_free_chunk;
|
||||||
|
@ -118,7 +118,7 @@ private:
|
||||||
* An arena manages a contiguous region of memory. The pool starts out with one arena
|
* An arena manages a contiguous region of memory. The pool starts out with one arena
|
||||||
* but can grow to multiple arenas if the need arises.
|
* but can grow to multiple arenas if the need arises.
|
||||||
*
|
*
|
||||||
* Unlike a normal C heap, the administrative structures are seperate from the managed
|
* Unlike a normal C heap, the administrative structures are separate from the managed
|
||||||
* memory. This has been done as the sizes and bases of objects are not in themselves sensitive
|
* memory. This has been done as the sizes and bases of objects are not in themselves sensitive
|
||||||
* information, as to conserve precious locked memory. In some operating systems
|
* information, as to conserve precious locked memory. In some operating systems
|
||||||
* the amount of memory that can be locked is small.
|
* the amount of memory that can be locked is small.
|
||||||
|
@ -159,9 +159,12 @@ public:
|
||||||
* If this callback is provided and returns false, the allocation fails (hard fail), if
|
* If this callback is provided and returns false, the allocation fails (hard fail), if
|
||||||
* it returns true the allocation proceeds, but it could warn.
|
* it returns true the allocation proceeds, but it could warn.
|
||||||
*/
|
*/
|
||||||
LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = 0);
|
explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, LockingFailed_Callback lf_cb_in = nullptr);
|
||||||
~LockedPool();
|
~LockedPool();
|
||||||
|
|
||||||
|
LockedPool(const LockedPool& other) = delete; // non construction-copyable
|
||||||
|
LockedPool& operator=(const LockedPool&) = delete; // non copyable
|
||||||
|
|
||||||
/** Allocate size bytes from this arena.
|
/** Allocate size bytes from this arena.
|
||||||
* Returns pointer on success, or 0 if memory is full or
|
* Returns pointer on success, or 0 if memory is full or
|
||||||
* the application tried to allocate 0 bytes.
|
* the application tried to allocate 0 bytes.
|
||||||
|
@ -177,9 +180,6 @@ public:
|
||||||
/** Get pool usage statistics */
|
/** Get pool usage statistics */
|
||||||
Stats stats() const;
|
Stats stats() const;
|
||||||
private:
|
private:
|
||||||
LockedPool(const LockedPool& other) = delete; // non construction-copyable
|
|
||||||
LockedPool& operator=(const LockedPool&) = delete; // non copyable
|
|
||||||
|
|
||||||
std::unique_ptr<LockedPageAllocator> allocator;
|
std::unique_ptr<LockedPageAllocator> allocator;
|
||||||
|
|
||||||
/** Create an arena from locked pages */
|
/** Create an arena from locked pages */
|
||||||
|
@ -226,7 +226,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
|
explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
|
||||||
|
|
||||||
/** Create a new LockedPoolManager specialized to the OS */
|
/** Create a new LockedPoolManager specialized to the OS */
|
||||||
static void CreateInstance();
|
static void CreateInstance();
|
||||||
|
|
Loading…
Reference in New Issue