Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
IAllocator.h
Go to the documentation of this file.
1/*
2 Author : Tobias Stein
3 Date : 9th July, 2016
4 File : IAllocator.h
5
6 Base allocator class.
7
8 All Rights Reserved. (c) Copyright 2016.
9*/
10
11#pragma once
12#ifndef ECS__I_ALLOC_H__
13#define ECS__I_ALLOC_H__
14
15#include "API.h"
16
17namespace ECS { namespace Memory { namespace Allocator {
18
19
20 // returns address aligned
21 static inline void* AlignForward(void* address, u8 alignment)
22 {
23 return (void*)((reinterpret_cast<uptr>(address)+static_cast<uptr>(alignment - 1)) & static_cast<uptr>(~(alignment - 1)));
24 }
25
26 // returns the number of bytes needed to align the address
27 static inline u8 GetAdjustment(const void* address, u8 alignment)
28 {
29 const u8 adjustment = alignment - (reinterpret_cast<uptr>(address)& static_cast<uptr>(alignment - 1));
30
31 return adjustment == alignment ? 0 : adjustment;
32 }
33
34 static inline u8 GetAdjustment(const void* address, u8 alignment, u8 extra)
35 {
36 u8 adjustment = GetAdjustment(address, alignment);
37
38 u8 neededSpace = extra;
39
40 if (adjustment < neededSpace)
41 {
42 neededSpace -= adjustment;
43
44 //Increase adjustment to fit header
45 adjustment += alignment * (neededSpace / alignment);
46
47 if (neededSpace % alignment > 0)
48 adjustment += alignment;
49 }
50
51 return adjustment;
52 }
53
54
56 {
57 protected:
58
59 const size_t m_MemorySize;
61
64
65 public:
66
67 IAllocator(const size_t memSize, const void* mem);
68 virtual ~IAllocator();
69
70 virtual void* allocate(size_t size, u8 alignment) = 0;
71 virtual void free(void* p) = 0;
72 virtual void clear() = 0;
73
74 // ACCESSOR
75 inline size_t GetMemorySize() const
76 {
77 return this->m_MemorySize;
78 }
79
80 inline const void* GetMemoryAddress0() const
81 {
82 return this->m_MemoryFirstAddress;
83 }
84
85 inline size_t GetUsedMemory() const
86 {
87 return this->m_MemoryUsed;
88 }
89
90 inline u64 GetAllocationCount() const
91 {
92 return this->m_MemoryAllocations;
93 }
94
95 };
96
97}}} // ECS::Memory::Allocator
98
99#endif // ECS__I_ALLOC_H__
#define ECS_API
Definition: Platform.h:16
virtual void free(void *p)=0
const void * GetMemoryAddress0() const
Definition: IAllocator.h:80
virtual void * allocate(size_t size, u8 alignment)=0
static void * AlignForward(void *address, u8 alignment)
Definition: IAllocator.h:21
static u8 GetAdjustment(const void *address, u8 alignment)
Definition: IAllocator.h:27
uint32_t u64
Definition: Platform.h:55
uintptr_t uptr
Definition: Platform.h:64
uint8_t u8
Definition: Platform.h:49