| Fling Engine 0.00.1 Fling Engine is a game engine written in Vulkan

Memory.h
Go to the documentation of this file.
1 #pragma once
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "FlingTypes.h"
6
7 namespace Fling
8 {
9 void* AlignedAlloc(size_t t_Size, size_t t_Alignment);
10
11 void AlignedFree(void* t_Data);
12
13 #if FLING_LINUX
14
15 #define leading_zeroes(x) ((x) == 0 ? 32 : __builtin_clz(x))
16 #define trailing_zeroes(x) ((x) == 0 ? 32 : __builtin_ctz(x))
17 #define trailing_ones(x) __builtin_ctz(~(x))
18 #elif FLING_WINDOWS
19 #include <intrin.h>
20
21 namespace Internal
22 {
23 static inline uint32_t clz(uint32_t x)
24 {
25 unsigned long result;
26 if (_BitScanReverse(&result, x))
27 return 31 - result;
28 else
29 return 32;
30 }
31
32 static inline uint32_t ctz(uint32_t x)
33 {
34 unsigned long result;
35 if (_BitScanForward(&result, x))
36 return result;
37 else
38 return 32;
39 }
40 }
41
42 #define leading_zeroes(x) Fling::Internal::clz(x)
43 #define trailing_zeroes(x) Fling::Internal::ctz(x)
44 #define trailing_ones(x) Fling::Internal::ctz(~(x))
45
46 #endif
47
48 //Shift the given address upwards if/as necessary to ensure it is aligned to
49 //the given number of bytes
50 inline uintptr_t AlignAddress(uintptr_t t_Addr, size_t t_Align)
51 {
52 const size_t mask = t_Align - 1;
53 assert((t_Align & mask) == 0); //power of 2
54 return (t_Addr + mask) & ~mask;
55 }
56
57 //Shift the given pointer upwards if/as necessary to ensure it is aligned
58 //to the given number of bytes
59 template<typename T>
60 inline T* AlignPointer(T* t_Ptr, size_t t_Align)
61 {
62 const uintptr_t addr = reinterpret_cast<uintptr_t>(t_Ptr);
63 const uintptr_t addrAligned = AlignAddress(addr, t_Align);
64 return reinterpret_cast<T*>(addrAligned);
65 }
66 } // namespace Fling
Fling::AlignedAlloc
void * AlignedAlloc(size_t t_Size, size_t t_Alignment)
Definition: Memory.cpp:10
Fling::AlignedFree
void AlignedFree(void *t_Data)
Definition: Memory.cpp:25
Fling::AlignAddress
uintptr_t AlignAddress(uintptr_t t_Addr, size_t t_Align)
Definition: Memory.h:50
Fling
Definition: Engine.h:29
FlingTypes.h
Fling::AlignPointer
T * AlignPointer(T *t_Ptr, size_t t_Align)
Definition: Memory.h:60