FreeList often used the game engine to achieve

Disclaimer: This article is a blogger original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44312858/article/details/96787788

FreeList often used the game engine to achieve


In recent reading of Game Engine Architecture which talked about the Memory Management, which little mention FreeList. FreeList in the game engine is mainly used for flexible, fast for small same type of Object and distribution for memory release, such as bullets. Because FreeList relatively simple to achieve, so to share with you.

FreeList has the following characteristics:

  1. FreeList is actually a LinkedList, but the LinkedList in the next existed Value inside.
  2. FreeList not only to allocate memory Node management.
  3. Users need to release the memory being used.
  4. FreeList no problem of memory fragmentation.
    First look at a basic interface.
template<class T, std::size_t N>
class FreeList {
public:
    struct Element {
        Element* next;
    };
private:
    alignas(alignof(T)) char memoryPool_[N * sizeof(T)];
    Element* head;

public:
    FreeList();

    T* Allocate();
    void Deallocate(T*);
};

In this implementation of the above process, next is a pointer, so the memory required is greater than T type memory pointer, we assert this constructor on the inside, next index may be one, so can have a smaller memory FreeList T. If used as an index next Note that if T is 1 byte, then the list can be stored up to 2 ^ 8 Element.

template<class T, std::size_t N>
FreeList<T, N>::FreeList() {
    static_assert(sizeof(T) >= sizeof(T*), "size of T is less than size of an address");
    //….
}

In the constructor, we need to do next is memoryPool_ in each node are negative.

//FreeList Constructor….
Element* runningNode = head;

    while(true) {
        if (reinterpret_cast<char*>(runningNode) < memoryPool_ + (N-1) * sizeof(T)){
            runningNode->next = reinterpret_cast<Element*>(reinterpret_cast<T*>(runningNode) + 1);
        }
        else {
            runningNode->next = nullptr;
            break;
        }
        runningNode = runningNode->next;
    };
//….

Allocate followed, in the Allocate, wherein a value of We FreeList taking out head, and the head points next, if the head is then directly nullptr return nullptr.

//Allocate...
Element* temp = head;
head = head->next;
return reinterpret_cast<T*>(temp);
//...
在Deallocate,我们让返回的值重新回到FreeList中。

//Deallocate...
Element* temp = head;
head = reinterpret_cast<Element*>(p);
head->next = temp;
//...

In fact, I wanted to write Stack Based Allocator before, but because Stack Based Allocator harder than FreeList, limited to my own writing itself would not send such a lengthy time being. If some students want to see Stack Based Allocator can see this from the beginning: Howard Hinnant. If there are students interested in game engine and game development welcomed the exchange.

Because the technology began to write the text, and also welcomes pointed out errors in the article :)

This last FreeList in my own engine FixedArrayAllocator.hpp of varieties to use. Check out :) If you are interested you can view my engine twitter

Guess you like

Origin blog.csdn.net/weixin_44312858/article/details/96787788