The spica renderer
memory.h
1 #ifdef _MSC_VER
2 #pragma once
3 #endif
4 
5 #ifndef _SPICA_MEMORY_H_
6 #define _SPICA_MEMORY_H_
7 
8 #include <list>
9 
10 #include "../core/common.h"
11 #include "../core/uncopyable.h"
12 
13 namespace spica {
14 
24 class SPICA_EXPORTS MEM_ALIGN(64) MemoryArena : private Uncopyable {
25 public:
26  MemoryArena(size_t blockSize = 262144);
27  MemoryArena(MemoryArena&& arena) noexcept;
28  virtual ~MemoryArena();
29 
30  MemoryArena& operator=(MemoryArena&& arena) = delete;
31 
32  void* allocate(size_t nBytes);
33 
34  template <class T, class... Args>
35  T* allocate(const Args&... args) {
36  T* ret = (T*)allocate(sizeof(T));
37  new (ret) T(args...);
38  return ret;
39  }
40 
41  void reset();
42 
43  size_t totalAllocated() const;
44 
45 private:
46  // Private fields
47  const size_t blockSize_;
48  size_t currentBlockPos_ = 0;
49  size_t currentAllocSize_ = 0;
50  unsigned char* currentBlock_ = nullptr;
51  std::list<std::pair<size_t, unsigned char*>> usedBlocks_, availableBlocks_;
52 }; // class MemoryArena
53 
54 } // namespace spica
55 
56 #endif // _SPICA_MEMORY_H_