Skip to content

Commit

Permalink
Limit alignment to nodes only
Browse files Browse the repository at this point in the history
  • Loading branch information
shqke committed Dec 17, 2024
1 parent 0ed6ccd commit 4fa590f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ std::expected<Allocation, Allocator::Error> Allocator::internal_allocate_near(
const std::vector<uint8_t*>& desired_addresses, size_t size, size_t max_distance) {
// Align to 2 bytes to pass MFP virtual method check
// See https://itanium-cxx-abi.github.io/cxx-abi/abi.html#member-function-pointers
size = align_up(size, 2);
size_t aligned_size = align_up(size, 2);

// First search through our list of allocations for a free block that is large
// enough.
for (const auto& allocation : m_memory) {
if (allocation->size < size) {
if (allocation->size < aligned_size) {
continue;
}

for (auto node = allocation->freelist.get(); node != nullptr; node = node->next.get()) {
// Enough room?
if (static_cast<size_t>(node->end - node->start) < size) {
if (static_cast<size_t>(node->end - node->start) < aligned_size) {
continue;
}

Expand All @@ -108,14 +108,14 @@ std::expected<Allocation, Allocator::Error> Allocator::internal_allocate_near(
continue;
}

node->start += size;
node->start += aligned_size;

return Allocation{shared_from_this(), address, size};
}
}

// If we didn't find a free block, we need to allocate a new one.
auto allocation_size = align_up(size, system_info().allocation_granularity);
auto allocation_size = align_up(aligned_size, system_info().allocation_granularity);
auto allocation_address = allocate_nearby_memory(desired_addresses, allocation_size, max_distance);

if (!allocation_address) {
Expand All @@ -127,13 +127,16 @@ std::expected<Allocation, Allocator::Error> Allocator::internal_allocate_near(
allocation->address = *allocation_address;
allocation->size = allocation_size;
allocation->freelist = std::make_unique<FreeNode>();
allocation->freelist->start = *allocation_address + size;
allocation->freelist->start = *allocation_address + aligned_size;
allocation->freelist->end = *allocation_address + allocation_size;

return Allocation{shared_from_this(), *allocation_address, size};
}

void Allocator::internal_free(uint8_t* address, size_t size) {
// See internal_allocate_near
size = align_up(size, 2);

for (const auto& allocation : m_memory) {
if (allocation->address > address || allocation->address + allocation->size < address) {
continue;
Expand Down

0 comments on commit 4fa590f

Please sign in to comment.