Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Memory.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
9#ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
10
11// Normal memory allocation, must be at least 8 byte aligned on 32 bit platform and 16 byte aligned on 64 bit platform
12using AllocateFunction = void *(*)(size_t inSize);
13using FreeFunction = void (*)(void *inBlock);
14
15// Aligned memory allocation
16using AlignedAllocateFunction = void *(*)(size_t inSize, size_t inAlignment);
17using AlignedFreeFunction = void (*)(void *inBlock);
18
19// User defined allocation / free functions
24
27
29#define JPH_OVERRIDE_NEW_DELETE \
30 JPH_INLINE void *operator new (size_t inCount) { return JPH::Allocate(inCount); } \
31 JPH_INLINE void operator delete (void *inPointer) noexcept { JPH::Free(inPointer); } \
32 JPH_INLINE void *operator new[] (size_t inCount) { return JPH::Allocate(inCount); } \
33 JPH_INLINE void operator delete[] (void *inPointer) noexcept { JPH::Free(inPointer); } \
34 JPH_INLINE void *operator new (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
35 JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
36 JPH_INLINE void *operator new[] (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
37 JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); }
38
39#else
40
41// Directly define the allocation functions
42JPH_EXPORT void *Allocate(size_t inSize);
43JPH_EXPORT void Free(void *inBlock);
44JPH_EXPORT void *AlignedAllocate(size_t inSize, size_t inAlignment);
45JPH_EXPORT void AlignedFree(void *inBlock);
46
47// Don't implement allocator registering
48inline void RegisterDefaultAllocator() { }
49
50// Don't override new/delete
51#define JPH_OVERRIDE_NEW_DELETE
52
53#endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
54
#define JPH_EXPORT
Definition: Core.h:227
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
void *(*)(size_t inSize, size_t inAlignment) AlignedAllocateFunction
Definition: Memory.h:16
JPH_EXPORT AlignedAllocateFunction AlignedAllocate
Definition: Memory.cpp:61
void(*)(void *inBlock) FreeFunction
Definition: Memory.h:13
JPH_EXPORT void RegisterDefaultAllocator()
Register platform default allocation / free functions.
Definition: Memory.cpp:64
JPH_EXPORT AllocateFunction Allocate
Definition: Memory.cpp:59
JPH_EXPORT AlignedFreeFunction AlignedFree
Definition: Memory.cpp:62
void(*)(void *inBlock) AlignedFreeFunction
Definition: Memory.h:17
void *(*)(size_t inSize) AllocateFunction
Definition: Memory.h:12
JPH_EXPORT FreeFunction Free
Definition: Memory.cpp:60