-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.h
316 lines (259 loc) · 8.83 KB
/
memory.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef MEMORY_INCLUDE_H
#define MEMORY_INCLUDE_H
#include <stdint.h>
#include "utils.h"
typedef struct MemoryArena MemoryArena;
struct MemoryArena
{
u8 *base;
size_t size;
size_t previous_used;
size_t used;
int temporaryMemoryCount;
};
void initializeMemoryArena( MemoryArena *arena, u8 *base, size_t size );
inline b32 isMemoryArenaInitialized( MemoryArena *arena )
{
return arena->base != 0;
}
void resetMemoryArena( MemoryArena *arena );
b32 addressIsInsideArena( MemoryArena *arena, void *address );
b32 isPowerOfTwo( size_t number );
size_t getAlignmentOffset( size_t top, size_t alignment );
void *pushSize( MemoryArena *arena, size_t size, size_t alignment );
void *pushSizeZeroed( MemoryArena *arena, size_t size, size_t alignment );
void *resizeAllocationInArena( MemoryArena *arena, void *oldAddress, size_t oldSize, size_t newSize, size_t alignment );
void freeAllocationInArena( MemoryArena *arena, void *address );
typedef struct TemporaryMemory TemporaryMemory;
struct TemporaryMemory
{
MemoryArena *arena;
size_t previous_used;
size_t used;
};
TemporaryMemory beginTemporaryMemory( MemoryArena *arena );
void endTemporaryMemory( TemporaryMemory temporaryMemory );
#ifdef __cplusplus
struct TemporaryMemoryScoped
{
TemporaryMemoryScoped( MemoryArena *arena )
{
temporaryMemory = beginTemporaryMemory( arena );
}
~TemporaryMemoryScoped()
{
endTemporaryMemory( temporaryMemory );
}
TemporaryMemory temporaryMemory;
};
#endif // __cplusplus
// #define pushStruct(arena, type) (type *) pushSizeZeroed( arena, sizeof(type), alignof(type) )
// #define pushArray(arena, count, type) (type *) pushSizeZeroed( arena, sizeof(type) * (count), alignof(type) )
// TODO(irwin):
#define TEMP_DEFAULT_ALIGNMENT 8
#define pushStruct(arena, type) (type *) pushSizeZeroed( arena, sizeof(type), TEMP_DEFAULT_ALIGNMENT )
#define pushArray(arena, count, type) (type *) pushSizeZeroed( arena, sizeof(type) * (count), TEMP_DEFAULT_ALIGNMENT )
// #define resizeArray(arena, oldAddress, oldCount, newCount)
const char *copyStringToArena( MemoryArena *arena, const char *stringData, size_t stringLength );
MemoryArena *DEBUG_getDebugArena();
#endif // MEMORY_INCLUDE_H
#ifdef MEMORY_IMPLEMENTATION
#include <string.h> // memset, strlen, memmove
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#pragma comment(lib, "clang_rt.asan-x86_64.lib")
#include <sanitizer/asan_interface.h>
#if 0
static char *formatted_shadow_memory_print_str =
"%0p: %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx\n";
size_t shadow_memory_scale, shadow_memory_offset;
void __declspec(no_sanitize_address)print_shadow_memory( void *address )
{
uint8_t *shadow_memory = (uint8_t *)((size_t)address >> shadow_memory_scale) +
shadow_memory_offset;
fprintf( stderr, formatted_shadow_memory_print_str, address, shadow_memory[0],
shadow_memory[1], shadow_memory[2], shadow_memory[3], shadow_memory[4],
shadow_memory[5], shadow_memory[6], shadow_memory[7] );
}
#define PRINT_SHADOW_MEMORY(address) \
fprintf(stderr, "Shadow Memory at " __FILE__ ":%d\n", __LINE__); \
print_shadow_memory(address);
#endif
static u8 debug_arena_buffer_2[Megabytes( 64 )];
static MemoryArena DEBUG_debug_arena_2 = {0};
MemoryArena *DEBUG_getDebugArena()
{
if ( DEBUG_debug_arena_2.base )
{
// NOTE(irwin): initialized
return &DEBUG_debug_arena_2;
}
else
{
// NOTE(irwin): not initialized
//__asan_get_shadow_mapping( &shadow_memory_scale, &shadow_memory_offset );
#if 1
u64 size = Megabytes( 512 );
void *address = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
initializeMemoryArena( &DEBUG_debug_arena_2, address, size );
#else
initializeMemoryArena( &DEBUG_debug_arena_2, &debug_arena_buffer_2[0], sizeof( debug_arena_buffer_2 ) );
#endif
return &DEBUG_debug_arena_2;
}
}
void initializeMemoryArena( MemoryArena *arena, u8 *base, size_t size )
{
arena->base = base;
arena->size = size;
arena->previous_used = 0;
arena->used = 0;
arena->temporaryMemoryCount = 0;
ASAN_POISON_MEMORY_REGION( base, size );
}
void resetMemoryArena( MemoryArena *arena )
{
arena->previous_used = 0;
arena->used = 0;
arena->temporaryMemoryCount = 0;
ASAN_POISON_MEMORY_REGION( arena->base, arena->size );
}
b32 addressIsInsideArena( MemoryArena *arena, void *address )
{
u8 *addressChar = (u8 *)address;
return (arena->base <= addressChar) && (addressChar < arena->base + arena->size);
}
b32 isPowerOfTwo( size_t number )
{
return (number & (number - 1)) == 0;
}
size_t getAlignmentOffset( size_t top, size_t alignment )
{
size_t alignmentMask = alignment - 1;
size_t alignmentOffset = 0;
if ( top & alignmentMask )
{
alignmentOffset = alignment - (top & alignmentMask);
}
return alignmentOffset;
}
void *pushSize( MemoryArena *arena, size_t size, size_t alignment )
{
Assert( isPowerOfTwo( alignment ) );
size_t top = (size_t)(arena->base + arena->used);
size_t alignmentOffset = getAlignmentOffset( top, alignment );
size += alignmentOffset;
if ( size <= (arena->size - arena->used) )
{
void *address = arena->base + arena->used + alignmentOffset;
arena->previous_used = arena->used + alignmentOffset;
arena->used += size;
ASAN_UNPOISON_MEMORY_REGION( address, size );
return address;
}
else
{
AssertFail( "Requested chunk size exceeds arena capacity!" );
return 0;
}
}
void *pushSizeZeroed( MemoryArena *arena, size_t size, size_t alignment )
{
void *result = pushSize( arena, size, alignment );
if ( result != 0 )
{
memset( result, 0, size );
}
return result;
}
void freeAllocationInArena( MemoryArena *arena, void *address )
{
if ( address != 0 )
{
if ( addressIsInsideArena( arena, address ) )
{
// TODO(irwin):
}
}
}
void *resizeAllocationInArena( MemoryArena *arena, void *oldAddress, size_t oldSize, size_t newSize, size_t alignment )
{
u8 *oldAddressChar = (u8 *)oldAddress;
if ( oldAddress == 0 || oldSize == 0 )
{
return pushSizeZeroed( arena, newSize, alignment );
}
else if ( addressIsInsideArena( arena, oldAddress ) )
{
// NOTE(irwin): if oldAddress was the last allocation we can extend it
if ( arena->base + arena->previous_used == oldAddressChar )
{
if ( arena->previous_used + newSize <= arena->size )
{
AssertMessage( getAlignmentOffset( (size_t)oldAddress, alignment ) == 0, "Trying to extend previous allocation with different alignment" );
arena->used = arena->previous_used + newSize;
if ( oldSize < newSize )
{
ASAN_UNPOISON_MEMORY_REGION( oldAddressChar + oldSize, newSize - oldSize );
memset( oldAddressChar + oldSize, 0, newSize - oldSize );
}
else
{
ASAN_POISON_MEMORY_REGION( oldAddressChar + newSize, oldSize - newSize );
}
return oldAddress;
}
else
{
AssertFail( "ERROR: new size exceeds arena capacity!" );
return 0;
}
// NOTE(irwin): there are allocations after oldAddress, so we can't extend it.
// Reallocate and copy instead.
}
else
{
void *newAddress = pushSizeZeroed( arena, newSize, alignment );
if ( newAddress != 0 )
{
memmove( newAddress, oldAddress, oldSize < newSize ? oldSize : newSize );
}
return newAddress;
}
}
else
{
AssertFail( "ERROR: address lies outside the arena chunk!" );
return 0;
}
}
TemporaryMemory beginTemporaryMemory( MemoryArena *arena )
{
TemporaryMemory temporaryMemory = {0};
temporaryMemory.arena = arena;
temporaryMemory.previous_used = arena->previous_used;
temporaryMemory.used = arena->used;
++arena->temporaryMemoryCount;
return temporaryMemory;
}
void endTemporaryMemory( TemporaryMemory temporaryMemory )
{
Assert( temporaryMemory.arena->temporaryMemoryCount > 0 );
temporaryMemory.arena->previous_used = temporaryMemory.previous_used;
temporaryMemory.arena->used = temporaryMemory.used;
ASAN_POISON_MEMORY_REGION( temporaryMemory.arena->base + temporaryMemory.arena->used, temporaryMemory.arena->size - temporaryMemory.arena->used );
--temporaryMemory.arena->temporaryMemoryCount;
}
const char *copyStringToArena( MemoryArena *arena, const char *stringData, size_t stringLength )
{
if ( stringLength == 0 )
stringLength = strlen( stringData );
void *copied = pushSizeZeroed( arena, stringLength + 1, TEMP_DEFAULT_ALIGNMENT );
if ( copied != 0 )
{
memmove( copied, stringData, stringLength );
}
return (const char *)copied;
}
#endif // MEMORY_IMPLEMENTATION