Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/Memory.cpp

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #include <Jolt/Jolt.h>
6
7 JPH_SUPPRESS_WARNINGS_STD_BEGIN
8 #include <cstdlib>
9 JPH_SUPPRESS_WARNINGS_STD_END
10 #include <stdlib.h>
11
12 JPH_NAMESPACE_BEGIN
13
14 #ifdef JPH_DISABLE_CUSTOM_ALLOCATOR
15 #define JPH_ALLOC_FN(x) x
16 #define JPH_ALLOC_SCOPE
17 #else
18 #define JPH_ALLOC_FN(x) x##Impl
19 #define JPH_ALLOC_SCOPE static
20 #endif
21
22 JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(Allocate)(size_t inSize)
23 {
24 JPH_ASSERT(inSize > 0);
25 return malloc(inSize);
26 }
27
28 JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(Reallocate)(void *inBlock, [[maybe_unused]] size_t inOldSize, size_t inNewSize)
29 {
30 JPH_ASSERT(inNewSize > 0);
31 return realloc(inBlock, inNewSize);
32 }
33
34 JPH_ALLOC_SCOPE void JPH_ALLOC_FN(Free)(void *inBlock)
35 {
36 free(inBlock);
37 }
38
39 JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(AlignedAllocate)(size_t inSize, size_t inAlignment)
40 {
41 JPH_ASSERT(inSize > 0 && inAlignment > 0);
42
43 #if defined(JPH_PLATFORM_WINDOWS)
44 // Microsoft doesn't implement posix_memalign
45 return _aligned_malloc(inSize, inAlignment);
46 #else
47 void *block = nullptr;
48 JPH_SUPPRESS_WARNING_PUSH
49 JPH_GCC_SUPPRESS_WARNING("-Wunused-result")
50 JPH_CLANG_SUPPRESS_WARNING("-Wunused-result")
51 posix_memalign(&block, inAlignment, inSize);
52 JPH_SUPPRESS_WARNING_POP
53 return block;
54 #endif
55 }
56
57 JPH_ALLOC_SCOPE void JPH_ALLOC_FN(AlignedFree)(void *inBlock)
58 {
59 #if defined(JPH_PLATFORM_WINDOWS)
60 _aligned_free(inBlock);
61 #else
62 free(inBlock);
63 #endif
64 }
65
66 #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
67
68 AllocateFunction Allocate = nullptr;
69 ReallocateFunction Reallocate = nullptr;
70 FreeFunction Free = nullptr;
71 AlignedAllocateFunction AlignedAllocate = nullptr;
72 AlignedFreeFunction AlignedFree = nullptr;
73
74 void RegisterDefaultAllocator()
75 {
76 Allocate = AllocateImpl;
77 Reallocate = ReallocateImpl;
78 Free = FreeImpl;
79 AlignedAllocate = AlignedAllocateImpl;
80 AlignedFree = AlignedFreeImpl;
81 }
82
83 #endif // JPH_DISABLE_CUSTOM_ALLOCATOR
84
85 JPH_NAMESPACE_END