Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
LinearAllocator.cpp
Go to the documentation of this file.
1
2/*
3 Author : Tobias Stein
4 Date : 22nd October, 2016
5 File : LinearAllocator.cpp
6
7 Linear allocator.
8
9 All Rights Reserved. (c) Copyright 2016.
10*/
11
13
14
15namespace ECS { namespace Memory { namespace Allocator {
16
17 LinearAllocator::LinearAllocator(size_t memSize, const void* mem) :
18 IAllocator(memSize, mem)
19 {}
20
22 {
23 this->clear();
24 }
25
26 void* LinearAllocator::allocate(size_t memSize, u8 alignment)
27 {
28 assert(memSize > 0 && "allocate called with memSize = 0.");
29
30 union
31 {
32 void* asVoidPtr;
33 uptr asUptr;
34 };
35
36 asVoidPtr = (void*)this->m_MemoryFirstAddress;
37
38 // current address
39 asUptr += this->m_MemoryUsed;
40
41 // get adjustment to align address
42 u8 adjustment = GetAdjustment(asVoidPtr, alignment);
43
44 // check if there is enough memory available
45 if (this->m_MemoryUsed + memSize + adjustment > this->m_MemorySize)
46 {
47 // not enough memory
48 return nullptr;
49 }
50
51 // determine aligned memory address
52 asUptr += adjustment;
53
54 // update book keeping
55 this->m_MemoryUsed += memSize + adjustment;
56 this->m_MemoryAllocations++;
57
58 // return aligned memory address
59 return asVoidPtr;
60 }
61
62 void LinearAllocator::free([[maybe_unused]] void* mem)
63 {
64 assert(false && "Lineaer allocators do not support free operations. Use clear instead.");
65 }
66
68 {
69 // simply reset memory
70 this->m_MemoryUsed = 0;
71 this->m_MemoryAllocations = 0;
72 }
73
74} } } // namespace ECS::Memory::Allocator
LinearAllocator(size_t memSize, const void *mem)
virtual void free(void *p) override
virtual void * allocate(size_t size, u8 alignment) override
static u8 GetAdjustment(const void *address, u8 alignment)
Definition: IAllocator.h:27
uintptr_t uptr
Definition: Platform.h:64
uint8_t u8
Definition: Platform.h:49