Lugdunum  0.1.0
Arena.inl
Go to the documentation of this file.
1 template <
2  class Allocator,
3  class ThreadPolicy,
4  class BoundsCheckingPolicy,
5  class MemoryMarkingPolicy
6 >
8 
9 template <
10  class Allocator,
11  class ThreadPolicy,
12  class BoundsCheckingPolicy,
13  class MemoryMarkingPolicy
14 >
15 void* Arena<Allocator, ThreadPolicy, BoundsCheckingPolicy, MemoryMarkingPolicy>::allocate(size_t size, size_t alignment, size_t offset, const char* file, size_t line) {
16  // TODO: Use file and line
17  (void)(file);
18  (void)(line);
19 
20  const size_t newSize = size + BoundsCheckingPolicy::SizeFront + BoundsCheckingPolicy::SizeBack;
21 
22  _threadGuard.enter();
23 
24  char* const ptr = static_cast<char*>(_allocator.allocate(newSize, alignment, offset + BoundsCheckingPolicy::SizeFront));
25  const size_t allocatedSize = _allocator.getSize(ptr);
26 
27  _boundsChecker.guardFront(ptr, allocatedSize);
28  _memoryMarker.markAllocation(ptr + BoundsCheckingPolicy::SizeFront, allocatedSize - BoundsCheckingPolicy::SizeFront - BoundsCheckingPolicy::SizeBack);
29  _boundsChecker.guardBack(ptr, allocatedSize);
30 
31  _threadGuard.leave();
32 
33  return (ptr + BoundsCheckingPolicy::SizeFront);
34 }
35 
36 template <
37  class Allocator,
38  class ThreadPolicy,
39  class BoundsCheckingPolicy,
40  class MemoryMarkingPolicy
41 >
43  if (!ptr) {
44  return;
45  }
46 
47  char* const originalMemory = static_cast<char*>(ptr) - BoundsCheckingPolicy::SizeFront;
48  const size_t allocatedSize = _allocator.getSize(originalMemory);
49 
50  _threadGuard.enter();
51 
52  _boundsChecker.checkFront(originalMemory, allocatedSize);
53  _boundsChecker.checkBack(originalMemory, allocatedSize);
54 
55  _memoryMarker.markDeallocation(originalMemory, allocatedSize);
56 
57  _allocator.free(originalMemory);
58 
59  _threadGuard.leave();
60 }
61 
62 template <
63  class Allocator,
64  class ThreadPolicy,
65  class BoundsCheckingPolicy,
66  class MemoryMarkingPolicy
67 >
69  _threadGuard.enter();
70 
71  _boundsChecker.checkReset();
72  _allocator.reset();
73 
74  _threadGuard.leave();
75 }
76 
77 template <
78  class Allocator,
79  class ThreadPolicy,
80  class BoundsCheckingPolicy,
81  class MemoryMarkingPolicy
82 >
84  return _allocator;
85 }
86 
87 template <
88  class Allocator,
89  class ThreadPolicy,
90  class BoundsCheckingPolicy,
91  class MemoryMarkingPolicy
92 >
94  return _allocator;
95 }
ThreadPolicy _threadGuard
Definition: Arena.hpp:39
BoundsCheckingPolicy _boundsChecker
Definition: Arena.hpp:40
MemoryMarkingPolicy _memoryMarker
Definition: Arena.hpp:41
Allocator & allocator()
Definition: Arena.inl:83
void free(void *ptr)
Definition: Arena.inl:42
void * allocate(size_t size, size_t alignment, size_t offset, const char *file, size_t line)
Definition: Arena.inl:15