Lugdunum  0.1.0
Linear.cpp
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <memory>
4 #include <lug/System/Debug.hpp>
5 
6 namespace lug {
7 namespace System {
8 namespace Memory {
9 namespace Allocator {
10 
12  if (_currentPage) {
14  }
15 }
16 
17 void* Linear::allocate(size_t size, size_t alignment, size_t offset) {
18  LUG_ASSERT(size > offset, "The size must be greater than the offset");
19 
20  // Adapt the size to store the size at the beginning of the block
21  const size_t newSize = size + sizeof(size_t);
22  const size_t newOffset = offset + sizeof(size_t);
23 
24  while (_current) {
25  // Try to allocate memory on the current page
26  _current = static_cast<char*>(_current) + newOffset;
27 
28  if (_current <= _currentPage->end) {
29  size_t sizeLeft = static_cast<char*>(_currentPage->end) - static_cast<char*>(_current) + 1;
30 
31  if (std::align(alignment, newSize - newOffset, _current, sizeLeft)) {
32  _current = static_cast<char*>(_current) + newSize - newOffset;
33 
34  // Store the size
35  *static_cast<size_t*>(static_cast<void*>(static_cast<char*>(_current) - newSize)) = size;
36 
37  return static_cast<char*>(_current) - size;
38  }
39  }
40 
41  // Out of memory on this page, just request a new one and try to reallocate
43  _current = _currentPage ? _currentPage->start : nullptr;
44  }
45 
46  return nullptr;
47 }
48 
49 void Linear::free(void*) const {
50  // Do nothing here
51 }
52 
53 void Linear::reset() {
55 
56  if (_currentPage) {
58  }
59 }
60 
62  return {_current, _currentPage};
63 }
64 
65 void Linear::rewind(const Linear::Mark& mark) {
66  _current = mark.current;
68 }
69 
70 size_t Linear::getSize(void* ptr) const {
71  return static_cast<size_t*>(ptr)[-1];
72 }
73 
74 } // Allocator
75 } // Memory
76 } // System
77 } // lug
lug::System::Memory::Area::Page * _currentPage
Definition: Linear.hpp:44
void * allocate(size_t size, size_t alignment, size_t offset)
Definition: Linear.cpp:17
#define LUG_ASSERT(assertion, message)
Definition: Debug.hpp:38
virtual Page * requestNextPage()=0
void free(void *ptr) const
Definition: Linear.cpp:49
lug::System::Memory::Area::Page * currentPage
Definition: Linear.hpp:16
lug::System::Memory::Area::IArea *const _area
Definition: Linear.hpp:41
size_t getSize(void *ptr) const
Definition: Linear.cpp:70
Linear(lug::System::Memory::Area::IArea *area)
Definition: Linear.cpp:11
lug::System::Memory::Area::Page * _firstPage
Definition: Linear.hpp:45
void rewind(const Mark &mark)
Definition: Linear.cpp:65