Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
CircularBuffer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "FlingTypes.h"
4 
5 namespace Fling
6 {
7 
15  template<typename T, size_t t_Size>
17  {
18  public:
19 
21 
22  ~CircularBuffer() = default;
23 
24  T* GetItem();
25 
26  private:
27 
29 
30  T m_Buffer [t_Size];
31 
33  };
34 
35  template<typename T, size_t t_Size>
37  : m_BufferSize(t_Size)
38  , m_AllocatedIndex(0)
39  , m_Buffer{}
40  {
41  static_assert((t_Size != 0 && (t_Size & (t_Size-1)) == 0), "CircularBuffer::t_Size must be a power of 2!");
42  }
43 
44  template<typename T, size_t t_Size>
46  {
47  const uint32_t index = m_AllocatedIndex++;
48  // This bit shifting is the same as the % operator
49  // @see https://blog.molecular-matters.com/2015/09/08/job-system-2-0-lock-free-work-stealing-part-2-a-specialized-allocator/
50  // for more about that
51  return &(m_Buffer[(index - 1u) & (m_BufferSize - 1u)]);
52  }
53 } // namespace Fling
T * GetItem()
Definition: CircularBuffer.hpp:45
A simple circular buffer that will allow you get the next element in a buffer It does not ensure that...
Definition: CircularBuffer.hpp:16
T m_Buffer[t_Size]
Definition: CircularBuffer.hpp:30
const UINT32 m_BufferSize
Definition: CircularBuffer.hpp:28
CircularBuffer()
Definition: CircularBuffer.hpp:36
UINT32 m_AllocatedIndex
Definition: CircularBuffer.hpp:32
uint32_t UINT32
Definition: FlingTypes.h:13
Definition: Engine.h:29