Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
MemoryPool.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Graphics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_MEMORYPOOL_HPP
8 #define NAZARA_MEMORYPOOL_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <atomic>
12 #include <memory>
13 
14 namespace Nz
15 {
16  class MemoryPool
17  {
18  public:
19  MemoryPool(unsigned int blockSize, unsigned int size = 1024, bool canGrow = true);
20  MemoryPool(const MemoryPool&) = delete;
21  MemoryPool(MemoryPool&& pool) noexcept;
22  ~MemoryPool() = default;
23 
24  void* Allocate(unsigned int size);
25 
26  template<typename T> void Delete(T* ptr);
27 
28  void Free(void* ptr);
29 
30  inline unsigned int GetBlockSize() const;
31  inline unsigned int GetFreeBlocks() const;
32  inline unsigned int GetSize() const;
33 
34  template<typename T, typename... Args> T* New(Args&&... args);
35 
36  MemoryPool& operator=(const MemoryPool&) = delete;
37  MemoryPool& operator=(MemoryPool&& pool) noexcept;
38 
39  private:
40  MemoryPool(MemoryPool* pool);
41 
42  std::unique_ptr<void* []> m_freeList;
43  std::unique_ptr<UInt8[]> m_pool;
44  std::unique_ptr<MemoryPool> m_next;
45  std::atomic_uint m_freeCount;
46  MemoryPool* m_previous;
47  bool m_canGrow;
48  unsigned int m_blockSize;
49  unsigned int m_size;
50  };
51 }
52 
53 #include <Nazara/Core/MemoryPool.inl>
54 
55 #endif // NAZARA_MEMORYPOOL_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Core class that represents a memory pool.
Definition: MemoryPool.hpp:16
unsigned int GetSize() const
Gets the pool size.
Definition: MemoryPool.inl:176
unsigned int GetFreeBlocks() const
Gets the number of free blocks.
Definition: MemoryPool.inl:166
void * Allocate(unsigned int size)
Allocates enough memory for the size and returns a pointer to it.
Definition: MemoryPool.inl:74
MemoryPool(unsigned int blockSize, unsigned int size=1024, bool canGrow=true)
Constructs a MemoryPool object.
Definition: MemoryPool.inl:27
void Free(void *ptr)
Frees the memory represented by the poiner.
Definition: MemoryPool.inl:118
void Delete(T *ptr)
Deletes the memory represented by the poiner.
Definition: MemoryPool.inl:100
unsigned int GetBlockSize() const
Gets the block size.
Definition: MemoryPool.inl:156
T * New(Args &&... args)
Creates a new value of type T with arguments.
Definition: MemoryPool.inl:191