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
13using AllocateFunction = void *(*)(size_t inSize);
14
16using ReallocateFunction = void *(*)(void *inBlock, size_t inOldSize, size_t inNewSize);
17
19using FreeFunction = void (*)(void *inBlock);
20
22using AlignedAllocateFunction = void *(*)(size_t inSize, size_t inAlignment);
23
25using AlignedFreeFunction = void (*)(void *inBlock);
26
27// User defined allocation / free functions
33
36
37// 32-bit MinGW g++ doesn't call the correct overload for the new operator when a type is 16 bytes aligned.
38// It uses the non-aligned version, which on 32 bit platforms usually returns an 8 byte aligned block.
39// We therefore default to 16 byte aligned allocations when the regular new operator is used.
40// See: https://github.com/godotengine/godot/issues/105455#issuecomment-2824311547
41#if defined(JPH_COMPILER_MINGW) && JPH_CPU_ARCH_BITS == 32
42 #define JPH_INTERNAL_DEFAULT_ALLOCATE(size) JPH::AlignedAllocate(size, 16)
43 #define JPH_INTERNAL_DEFAULT_FREE(pointer) JPH::AlignedFree(pointer)
44#else
45 #define JPH_INTERNAL_DEFAULT_ALLOCATE(size) JPH::Allocate(size)
46 #define JPH_INTERNAL_DEFAULT_FREE(pointer) JPH::Free(pointer)
47#endif
48
50#define JPH_OVERRIDE_NEW_DELETE \
51 JPH_INLINE void *operator new (size_t inCount) { return JPH_INTERNAL_DEFAULT_ALLOCATE(inCount); } \
52 JPH_INLINE void operator delete (void *inPointer) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
53 JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] size_t inSize) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
54 JPH_INLINE void *operator new[] (size_t inCount) { return JPH_INTERNAL_DEFAULT_ALLOCATE(inCount); } \
55 JPH_INLINE void operator delete[] (void *inPointer) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
56 JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] size_t inSize) noexcept{ JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
57 JPH_INLINE void *operator new (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
58 JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
59 JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] size_t inSize, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
60 JPH_INLINE void *operator new[] (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
61 JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
62 JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] size_t inSize, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
63 JPH_INLINE void *operator new ([[maybe_unused]] size_t inCount, void *inPointer) noexcept { return inPointer; } \
64 JPH_INLINE void operator delete ([[maybe_unused]] void *inPointer, [[maybe_unused]] void *inPlace) noexcept { /* Do nothing */ } \
65 JPH_INLINE void *operator new[] ([[maybe_unused]] size_t inCount, void *inPointer) noexcept { return inPointer; } \
66 JPH_INLINE void operator delete[] ([[maybe_unused]] void *inPointer, [[maybe_unused]] void *inPlace) noexcept { /* Do nothing */ }
67
68#else
69
70// Directly define the allocation functions
71JPH_EXPORT void *Allocate(size_t inSize);
72JPH_EXPORT void *Reallocate(void *inBlock, size_t inOldSize, size_t inNewSize);
73JPH_EXPORT void Free(void *inBlock);
74JPH_EXPORT void *AlignedAllocate(size_t inSize, size_t inAlignment);
75JPH_EXPORT void AlignedFree(void *inBlock);
76
77// Don't implement allocator registering
78inline void RegisterDefaultAllocator() { }
79
80// Don't override new/delete
81#define JPH_OVERRIDE_NEW_DELETE
82
83#endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
84
#define JPH_EXPORT
Definition Core.h:275
#define JPH_NAMESPACE_END
Definition Core.h:425
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
void *(*)(void *inBlock, size_t inOldSize, size_t inNewSize) ReallocateFunction
Reallocate memory. inBlock can be nullptr in which case it must behave as a memory allocation.
Definition Memory.h:16
void *(*)(size_t inSize, size_t inAlignment) AlignedAllocateFunction
Aligned memory allocation.
Definition Memory.h:22
JPH_EXPORT AlignedAllocateFunction AlignedAllocate
Definition Memory.cpp:71
void(*)(void *inBlock) FreeFunction
Free memory. inBlock can be nullptr in which case it must do nothing.
Definition Memory.h:19
JPH_EXPORT void RegisterDefaultAllocator()
Register platform default allocation / free functions.
Definition Memory.cpp:74
JPH_EXPORT AllocateFunction Allocate
Definition Memory.cpp:68
JPH_EXPORT ReallocateFunction Reallocate
Definition Memory.cpp:69
JPH_EXPORT AlignedFreeFunction AlignedFree
Definition Memory.cpp:72
void(*)(void *inBlock) AlignedFreeFunction
Free aligned memory. inBlock can be nullptr in which case it must do nothing.
Definition Memory.h:25
void *(*)(size_t inSize) AllocateFunction
Definition Memory.h:13
JPH_EXPORT FreeFunction Free
Definition Memory.cpp:70