Lugdunum  0.1.0
Chunk.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 #include <cstddef>
5 #include <lug/System/Debug.hpp>
6 #include <lug/System/Export.hpp>
9 
10 namespace lug {
11 namespace System {
12 namespace Memory {
13 namespace Allocator {
14 
18 namespace priv {
19 
20 // std::ceil isn't constexpr in clang and msvc
21 constexpr int32_t ceil(float num)
22 {
23  return (static_cast<float>(static_cast<int32_t>(num)) == num)
24  ? static_cast<int32_t>(num)
25  : static_cast<int32_t>(num) + ((num > 0) ? 1 : 0);
26 }
27 
28 }
33 template <size_t MaxSize, size_t MaxAlignment = MaxSize, size_t Offset = 0>
34 class Chunk {
35 public:
37  Chunk(const Chunk&) = delete;
38  Chunk(Chunk&&) = default;
39 
40  Chunk& operator=(const Chunk&) = delete;
41  Chunk& operator=(Chunk&&) = default;
42 
43  ~Chunk() = default;
44 
45  void* allocate(size_t size, size_t alignment, size_t offset);
46  void free(void* ptr);
47  void reset();
48 
49  size_t getSize(void* ptr) const;
50 
51 private:
52  static constexpr size_t ChunkSize = (MaxAlignment <= 1 ? MaxSize : priv::ceil(static_cast<float>(MaxSize) / MaxAlignment) * MaxAlignment);
53 
55  FreeList _freeList{ChunkSize};
56 
57  lug::System::Memory::Area::Page* _currentPage{nullptr};
58  lug::System::Memory::Area::Page* _firstPage{nullptr};
59 };
60 
62 
63 } // Allocator
64 } // Memory
65 } // System
66 } // lug
lug::System::Memory::Area::IArea *const _area
Definition: Chunk.hpp:54