mtrebi / memory-allocators

Custom memory allocators in C++ to improve the performance of dynamic memory allocation
MIT License
1.75k stars 160 forks source link

FreeListAllocator :: FindBest() question #14

Open manupedrozo opened 5 years ago

manupedrozo commented 5 years ago

Shouldn't the smallestDiff be updated along with the bestBlock in here? if (it->data.blockSize >= requiredSpace && (it->data.blockSize - requiredSpace < smallestDiff)) { bestBlock = it; }

smallestDiff = it->data.blockSize would be added.

m110h commented 4 years ago

FreeListAllocator::FindBest must be rewritten

void FreeListAllocator::FindBest(const std::size_t size, const std::size_t alignment, std::size_t& padding, Node *& previousNode, Node *& foundNode)
{
    std::size_t smallestDiff = std::numeric_limits<std::size_t>::max();

    Node * it = m_freeList.head;
    Node * itPrev {nullptr};

    while (it != nullptr)
    {
        const std::size_t _padding = Utils::CalculatePaddingWithHeader((std::size_t)it, alignment, sizeof (FreeListAllocator::AllocationHeader));
        const std::size_t requiredSpace = size + _padding;

        if ( (it->data.blockSize >= requiredSpace) && ((it->data.blockSize - requiredSpace) < smallestDiff) )
        {
            smallestDiff = it->data.blockSize - requiredSpace;
            padding = _padding;

            previousNode = itPrev;
            foundNode = it;

            if (smallestDiff == 0)
                return;
        }

        itPrev = it;
        it = it->next;
    }
}