Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
FreeList.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdlib.h> // size_t
4 #include <cstddef> //std::ptrdiff_t
5 #include <assert.h>
6 
7 #include "FlingExports.h"
8 
9 namespace Fling
10 {
17  class FLING_API FreeList
18  {
19  public:
20 
31  FreeList(void* t_Start, void* t_End, size_t t_ElmSize, size_t t_Alignment = 8, size_t t_Offset = 0);
32 
38  inline void* Obtain() noexcept;
39 
43  inline void Return(void* t_Ptr);
44 
45  private:
46 
48  FreeList* m_Next = nullptr;
49  };
50 
51  inline void* FreeList::Obtain() noexcept
52  {
53  if (m_Next == nullptr)
54  {
55  return nullptr;
56  }
57 
58  FreeList* head = m_Next;
59  m_Next = head->m_Next;
60  return head;
61  }
62 
63  inline void FreeList::Return(void* t_Ptr)
64  {
65  FreeList* head = static_cast<FreeList*>(t_Ptr);
66  head->m_Next = m_Next;
67  m_Next = head;
68  }
69 
70 } // namespace Fling
FreeList * m_Next
Alias in memory to the next available block.
Definition: FreeList.h:48
Helpful for allocating/freeing objects of a certain size which have to be created/destroeyed dynamica...
Definition: FreeList.h:17
void * Obtain() noexcept
Obtain a chunk of memory of the size and alignment that this list was created with.
Definition: FreeList.h:51
void Return(void *t_Ptr)
Return a block of memory to the free list.
Definition: FreeList.h:63
Definition: Engine.h:29