1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/intmath.h>
#include <zencore/memory/align.h>
#include <zencore/memory/mallocrpmalloc.h>
#if ZEN_RPMALLOC_ENABLED
# include "rpmalloc.h"
/** Value we fill a memory block with after it is free, in UE_BUILD_DEBUG **/
# define DEBUG_FILL_FREED (0xdd)
/** Value we fill a new memory block with, in UE_BUILD_DEBUG **/
# define DEBUG_FILL_NEW (0xcd)
# define ZEN_ENABLE_DEBUG_FILL 1
namespace zen {
FMallocRpmalloc::FMallocRpmalloc()
{
rpmalloc_initialize(nullptr);
}
FMallocRpmalloc::~FMallocRpmalloc()
{
rpmalloc_finalize();
}
void*
FMallocRpmalloc::TryMalloc(size_t Size, uint32_t Alignment)
{
void* NewPtr = nullptr;
if (Alignment != DEFAULT_ALIGNMENT)
{
Alignment = Max(uint32_t(Size >= 16 ? 16 : 8), Alignment);
NewPtr = rpaligned_alloc(Alignment, Size);
}
else
{
NewPtr = rpaligned_alloc(uint32_t(Size >= 16 ? 16 : 8), Size);
}
# if ZEN_BUILD_DEBUG && ZEN_ENABLE_DEBUG_FILL
if (Size && NewPtr != nullptr)
{
memset(NewPtr, DEBUG_FILL_NEW, rpmalloc_usable_size(NewPtr));
}
# endif
return NewPtr;
}
void*
FMallocRpmalloc::Malloc(size_t Size, uint32_t Alignment)
{
void* Result = TryMalloc(Size, Alignment);
if (Result == nullptr && Size)
{
OutOfMemory(Size, Alignment);
}
return Result;
}
void*
FMallocRpmalloc::Realloc(void* Ptr, size_t NewSize, uint32_t Alignment)
{
void* Result = TryRealloc(Ptr, NewSize, Alignment);
if (Result == nullptr && NewSize)
{
OutOfMemory(NewSize, Alignment);
}
return Result;
}
void*
FMallocRpmalloc::TryRealloc(void* Ptr, size_t NewSize, uint32_t Alignment)
{
# if ZEN_BUILD_DEBUG && ZEN_ENABLE_DEBUG_FILL
size_t OldSize = 0;
if (Ptr)
{
OldSize = rpmalloc_usable_size(Ptr);
if (NewSize < OldSize)
{
memset((uint8_t*)Ptr + NewSize, DEBUG_FILL_FREED, OldSize - NewSize);
}
}
# endif
void* NewPtr = nullptr;
if (NewSize == 0)
{
rpfree(Ptr);
return nullptr;
}
# if ZEN_PLATFORM_MAC
// macOS expects all allocations to be aligned to 16 bytes, so on Mac we always have to use mi_realloc_aligned
Alignment = AlignArbitrary(Max((uint32_t)16, Alignment), (uint32_t)16);
NewPtr = rpaligned_realloc(Ptr, Alignment, NewSize, /* OldSize */ 0, /* flags */ 0);
# else
if (Alignment != DEFAULT_ALIGNMENT)
{
Alignment = Max(NewSize >= 16 ? (uint32_t)16 : (uint32_t)8, Alignment);
NewPtr = rpaligned_realloc(Ptr, Alignment, NewSize, /* OldSize */ 0, /* flags */ 0);
}
else
{
NewPtr = rprealloc(Ptr, NewSize);
}
# endif
# if ZEN_BUILD_DEBUG && ZEN_ENABLE_DEBUG_FILL
if (NewPtr && NewSize > OldSize)
{
memset((uint8_t*)NewPtr + OldSize, DEBUG_FILL_NEW, rpmalloc_usable_size(NewPtr) - OldSize);
}
# endif
return NewPtr;
}
void
FMallocRpmalloc::Free(void* Ptr)
{
if (!Ptr)
{
return;
}
# if ZEN_BUILD_DEBUG && ZEN_ENABLE_DEBUG_FILL
memset(Ptr, DEBUG_FILL_FREED, rpmalloc_usable_size(Ptr));
# endif
rpfree(Ptr);
}
void*
FMallocRpmalloc::MallocZeroed(size_t Size, uint32_t Alignment)
{
void* Result = TryMallocZeroed(Size, Alignment);
if (Result == nullptr && Size)
{
OutOfMemory(Size, Alignment);
}
return Result;
}
void*
FMallocRpmalloc::TryMallocZeroed(size_t Size, uint32_t Alignment)
{
void* NewPtr = nullptr;
if (Alignment != DEFAULT_ALIGNMENT)
{
Alignment = Max(uint32_t(Size >= 16 ? 16 : 8), Alignment);
NewPtr = rpaligned_zalloc(Alignment, Size);
}
else
{
NewPtr = rpaligned_zalloc(uint32_t(Size >= 16 ? 16 : 8), Size);
}
return NewPtr;
}
bool
FMallocRpmalloc::GetAllocationSize(void* Original, size_t& SizeOut)
{
// this is not the same as the allocation size - is that ok?
SizeOut = rpmalloc_usable_size(Original);
return true;
}
void
FMallocRpmalloc::Trim(bool bTrimThreadCaches)
{
ZEN_UNUSED(bTrimThreadCaches);
}
} // namespace zen
#endif
|