Skip to content

freelist allcoator bugfix #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/FreeListAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ void* FreeListAllocator::Allocate(const std::size_t size, const std::size_t alig


const std::size_t alignmentPadding = padding - allocationHeaderSize;
const std::size_t requiredSize = size + padding;
std::size_t requiredSize = size + padding;

std::size_t rest = affectedNode->data.blockSize - requiredSize;

if (rest && rest < sizeof(Node)) {
requiredSize += rest;
rest = 0;
}

const std::size_t rest = affectedNode->data.blockSize - requiredSize;

if (rest > 0) {
// We have to split the block into the data block and a free block of size 'rest'
Expand Down Expand Up @@ -126,21 +132,27 @@ void FreeListAllocator::Free(void* ptr) {
const std::size_t headerAddress = currentAddress - sizeof (FreeListAllocator::AllocationHeader);
const FreeListAllocator::AllocationHeader * allocationHeader{ (FreeListAllocator::AllocationHeader *) headerAddress};

Node * freeNode = (Node *) (headerAddress);
freeNode->data.blockSize = allocationHeader->blockSize + allocationHeader->padding;
Node *freeNode = (Node *)(headerAddress - allocationHeader->padding);
freeNode->data.blockSize = allocationHeader->blockSize;
freeNode->next = nullptr;

Node * it = m_freeList.head;
Node * itPrev = nullptr;
while (it != nullptr) {
if (ptr < it) {
m_freeList.insert(itPrev, freeNode);
break;

if (!it) {
m_freeList.head = freeNode;
} else {
while (it != nullptr) {
if (ptr < it) {
m_freeList.insert(itPrev, freeNode);
break;
}
itPrev = it;
it = it->next;
}
itPrev = it;
it = it->next;
}


m_used -= freeNode->data.blockSize;

// Merge contiguous nodes
Expand Down