-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocation_functions.cpp
More file actions
220 lines (165 loc) · 6.08 KB
/
Copy pathallocation_functions.cpp
File metadata and controls
220 lines (165 loc) · 6.08 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
namespace memo
{
#ifndef MEMO_ONLY_DEFAULT_ALLOCATOR
#error MEMO_ONLY_DEFAULT_ALLOCATOR must be defined in memo_externals.h
#endif
#if MEMO_ONLY_DEFAULT_ALLOCATOR
void * alloc( size_t i_size, size_t i_alignment, size_t i_alignment_offset )
{
return DefaultAllocator::s_alloc( i_size, i_alignment, i_alignment_offset );
}
void * alloc( IAllocator & /*i_allocator*/, size_t i_size, size_t i_alignment, size_t i_alignment_offset )
{
return DefaultAllocator::s_alloc( i_size, i_alignment, i_alignment_offset );
}
void * realloc( void * i_address, size_t i_new_size, size_t i_alignment, size_t i_alignment_offset )
{
return DefaultAllocator::s_realloc( i_address, i_new_size, i_alignment, i_alignment_offset );
}
void free( void * i_address )
{
DefaultAllocator::s_free( i_address );
}
void dbg_check( void * i_address )
{
DefaultAllocator::s_dbg_check( i_address );
}
void * unaligned_alloc( size_t i_size )
{
return DefaultAllocator::s_unaligned_alloc( i_size );
}
void * unaligned_alloc( IAllocator & /*i_allocator*/, size_t i_size )
{
return DefaultAllocator::s_unaligned_alloc( i_size );
}
void * unaligned_realloc( void * i_address, size_t i_new_size )
{
return DefaultAllocator::s_unaligned_realloc( i_address, i_new_size );
}
void unaligned_free( void * i_address )
{
DefaultAllocator::s_unaligned_free( i_address );
}
void unaligned_dbg_check( void * i_address )
{
DefaultAllocator::s_unaligned_dbg_check( i_address );
}
#else
struct _AllocationHeader
{
IAllocator * m_allocator;
};
void * alloc( IAllocator & i_allocator, size_t i_size, size_t i_alignment, size_t i_alignment_offset )
{
_AllocationHeader * header = static_cast<_AllocationHeader*>(
i_allocator.alloc( i_size + sizeof(_AllocationHeader), i_alignment, i_alignment_offset + sizeof(_AllocationHeader) ) );
if( header != nullptr )
{
header->m_allocator = &i_allocator;
return header + 1;
}
else
return nullptr;
}
// alloc
void * alloc( size_t i_size, size_t i_alignment, size_t i_alignment_offset )
{
IAllocator * current_allocator = memo_externals::get_current_thread_allocator();
return alloc( *current_allocator, i_size, i_alignment, i_alignment_offset );
}
// realloc
void * realloc( void * i_address, size_t i_new_size, size_t i_alignment, size_t i_alignment_offset )
{
MEMO_ASSERT( i_address != nullptr ); // realloc with null address is not allowed, use alloc instead
_AllocationHeader * header = static_cast<_AllocationHeader*>( i_address ) - 1;
_AllocationHeader * new_header = static_cast<_AllocationHeader*>(
header->m_allocator->realloc( header, i_new_size + sizeof(_AllocationHeader), i_alignment, i_alignment_offset + sizeof(_AllocationHeader) ) );
if( new_header != nullptr )
return new_header + 1;
else
return nullptr;
}
// free
void free( void * i_address )
{
MEMO_ASSERT( i_address != nullptr ); // free with null address is not allowed
_AllocationHeader * header = static_cast<_AllocationHeader*>( i_address ) - 1;
header->m_allocator->free( header );
}
// dbg_check
void dbg_check( void * i_address )
{
MEMO_ASSERT( i_address != nullptr ); // dbg_check with null address is not allowed
_AllocationHeader * header = static_cast<_AllocationHeader*>( i_address ) - 1;
header->m_allocator->dbg_check( header );
}
// unaligned_alloc
void * unaligned_alloc( size_t i_size )
{
IAllocator * current_allocator = memo_externals::get_current_thread_allocator();
return unaligned_alloc( *current_allocator, i_size );
}
// unaligned_alloc
void * unaligned_alloc( IAllocator & i_allocator, size_t i_size )
{
_AllocationHeader * header = static_cast<_AllocationHeader*>(i_allocator.unaligned_alloc( i_size + sizeof(_AllocationHeader) ) );
if( header != nullptr )
{
header->m_allocator = &i_allocator;
return header + 1;
}
else
return nullptr;
}
// unaligned_realloc
void * unaligned_realloc( void * i_address, size_t i_new_size )
{
MEMO_ASSERT( i_address != nullptr ); // unaligned_realloc with null address is not allowed, use alloc instead
_AllocationHeader * header = static_cast<_AllocationHeader*>( i_address ) - 1;
_AllocationHeader * new_header = static_cast<_AllocationHeader*>(
header->m_allocator->unaligned_realloc( header, i_new_size + sizeof(_AllocationHeader) ) );
if( new_header != nullptr )
return new_header + 1;
else
return nullptr;
}
// unaligned_free
void unaligned_free( void * i_address )
{
MEMO_ASSERT( i_address != nullptr ); // unaligned_free with null address is not allowed
_AllocationHeader * header = static_cast<_AllocationHeader*>( i_address ) - 1;
header->m_allocator->unaligned_free( header );
}
// unaligned_dbg_check
void unaligned_dbg_check( void * i_address )
{
MEMO_ASSERT( i_address != nullptr ); // unaligned_dbg_check with null address is not allowed
_AllocationHeader * header = static_cast<_AllocationHeader*>( i_address ) - 1;
header->m_allocator->unaligned_dbg_check( header );
}
#endif
IAllocator & get_current_allocator()
{
IAllocator * current_allocator = memo_externals::get_current_thread_allocator();
return *current_allocator;
}
ObjectStack & get_lifo_allocator()
{
ThreadRoot * thread_context = memo_externals::get_thread_root();
MEMO_ASSERT( thread_context != nullptr ); // create a memo::ThreadRoot object on the call stack of the thread procedure
return thread_context->lifo_allocator();
}
// lifo_alloc
void * lifo_alloc( size_t i_size, size_t i_alignment, size_t i_alignment_offset, DeallocationCallback i_deallocation_callback )
{
ThreadRoot * thread_context = memo_externals::get_thread_root();
MEMO_ASSERT( thread_context != nullptr );
return thread_context->lifo_allocator().alloc( i_size, i_alignment, i_alignment_offset, i_deallocation_callback );
}
// lifo_free
void lifo_free( void * i_address )
{
ThreadRoot * thread_context = memo_externals::get_thread_root();
return thread_context->lifo_allocator().free( i_address );
}
} // namespace memo