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:
Jack Grigg 2020-07-31 07:34:20 +01:00
parent 71b79b85d1
commit e591f94fcf
3 changed files with 13 additions and 13 deletions

View File

@ -21,14 +21,14 @@ static void LockedPool(benchmark::State& state)
std::vector<void*> addr;
for (int x=0; x<ASIZE; ++x)
addr.push_back(0);
addr.push_back(nullptr);
uint32_t s = 0x12345678;
while (state.KeepRunning()) {
for (int x=0; x<BITER; ++x) {
int idx = s & (addr.size()-1);
if (s & 0x80000000) {
b.free(addr[idx]);
addr[idx] = 0;
addr[idx] = nullptr;
} else if(!addr[idx]) {
addr[idx] = b.alloc((s >> 16) & (MSIZE-1));
}

View File

@ -32,7 +32,7 @@
#include <iostream>
#endif
LockedPoolManager* LockedPoolManager::_instance = NULL;
LockedPoolManager* LockedPoolManager::_instance = nullptr;
std::once_flag LockedPoolManager::init_flag;
/*******************************************************************************/
@ -97,7 +97,7 @@ void* Arena::alloc(size_t size)
void Arena::free(void *ptr)
{
// Freeing the NULL pointer is OK.
// Freeing the nullptr pointer is OK.
if (ptr == nullptr) {
return;
}

View File

@ -51,6 +51,9 @@ public:
Arena(void *base, size_t size, size_t alignment);
virtual ~Arena();
Arena(const Arena& other) = delete; // non construction-copyable
Arena& operator=(const Arena&) = delete; // non copyable
/** Memory statistics. */
struct Stats
{
@ -86,9 +89,6 @@ public:
*/
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; }
private:
Arena(const Arena& other) = delete; // non construction-copyable
Arena& operator=(const Arena&) = delete; // non copyable
typedef std::multimap<size_t, char*> SizeToChunkSortedMap;
/** Map to enable O(log(n)) best-fit allocation, as it's sorted by size */
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
* 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
* information, as to conserve precious locked memory. In some operating systems
* 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
* 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(const LockedPool& other) = delete; // non construction-copyable
LockedPool& operator=(const LockedPool&) = delete; // non copyable
/** Allocate size bytes from this arena.
* Returns pointer on success, or 0 if memory is full or
* the application tried to allocate 0 bytes.
@ -177,9 +180,6 @@ public:
/** Get pool usage statistics */
Stats stats() const;
private:
LockedPool(const LockedPool& other) = delete; // non construction-copyable
LockedPool& operator=(const LockedPool&) = delete; // non copyable
std::unique_ptr<LockedPageAllocator> allocator;
/** Create an arena from locked pages */
@ -226,7 +226,7 @@ public:
}
private:
LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);
/** Create a new LockedPoolManager specialized to the OS */
static void CreateInstance();