# Small changes before rewrite
* Additional bit in remote allocator to prevent type confusion with the backend.
* Move Chunk allocator to backend.
* Improvements to RedBlack tree
* Expose message from Pal
# Complete backend rewrite
This provides two key changes:
* We use buddy allocators to allow memory to reconsolidated
* The backend is factored into a series of small operations that
allocate and deallocate memory.
The backend now uses "Ranges", there are two ranges that don't require a
parent range:
* EmptyRange - Never returns any memory
* PalRange - Returns memory from the platform.
All other ranges require a parent range to supply memory to them. Some
ranges support both allocation and deallocation, and some just
deallocation. For instance, CommitRange supports both, and maps
requests to the parent range, but will Commit and Decommit the memory.
As the ranges perform only a single task, they are generally small and
easy to follow. The two exceptions to this are the two BuddyRanges
(Large and Small). Large is for CHUNK_SIZE and above blocks, while
Small is for below CHUNK_SIZE blocks. Both are implemented with a buddy
allocator, but the SmallBuddyRange uses in place meta-data, while the
LargeBuddyRange uses the pagemap for its meta-data. This means the
LargeBuddyRange can keep the majority of memory it is managing
decommitted.
The Backend glues together the various ranges to support the appropriate
way to manage memory on the platform.
55 lines
1.3 KiB
C++
55 lines
1.3 KiB
C++
#pragma once
|
|
#include "../mem/entropy.h"
|
|
|
|
namespace snmalloc
|
|
{
|
|
/**
|
|
* Creates an area inside a large allocation that is larger by
|
|
* 2^RATIO_BITS. Will not return a the block at the start or
|
|
* the end of the large allocation.
|
|
*/
|
|
template<typename ParentRange, typename PAL, size_t RATIO_BITS>
|
|
class SubRange
|
|
{
|
|
typename ParentRange::State parent{};
|
|
|
|
public:
|
|
class State
|
|
{
|
|
SubRange sub_range{};
|
|
|
|
public:
|
|
constexpr State() = default;
|
|
|
|
SubRange* operator->()
|
|
{
|
|
return &sub_range;
|
|
}
|
|
};
|
|
|
|
constexpr SubRange() = default;
|
|
|
|
static constexpr bool Aligned = ParentRange::Aligned;
|
|
|
|
capptr::Chunk<void> alloc_range(size_t sub_size)
|
|
{
|
|
SNMALLOC_ASSERT(bits::is_pow2(sub_size));
|
|
|
|
auto full_size = sub_size << RATIO_BITS;
|
|
auto overblock = parent->alloc_range(full_size);
|
|
if (overblock == nullptr)
|
|
return nullptr;
|
|
|
|
size_t offset_mask = full_size - sub_size;
|
|
// Don't use first or last block in the larger reservation
|
|
// Loop required to get uniform distribution.
|
|
size_t offset;
|
|
do
|
|
{
|
|
offset = get_entropy64<PAL>() & offset_mask;
|
|
} while ((offset == 0) || (offset == offset_mask));
|
|
|
|
return pointer_offset(overblock, offset);
|
|
}
|
|
};
|
|
} // namespace snmalloc
|