|
5 | 5 |
|
6 | 6 | #if defined(WIN32) |
7 | 7 | # include <Windows.h> |
| 8 | +# include <shellapi.h> |
| 9 | +#else |
| 10 | +# include <unistd.h> |
| 11 | +# include <sys/stat.h> |
| 12 | +# include <mutex> |
8 | 13 | #endif // defined(WIN32) |
9 | 14 |
|
10 | 15 | #include "util.h" |
@@ -68,34 +73,64 @@ void DebugPrint(const char *fmt, ...) { |
68 | 73 | #endif |
69 | 74 |
|
70 | 75 | //----------------------------------------------------------------------------- |
71 | | -// Temporary arena. |
| 76 | +// Temporary arena, on Windows. |
72 | 77 | //----------------------------------------------------------------------------- |
73 | 78 |
|
74 | | -struct MimallocHeap { |
75 | | - mi_heap_t *heap = NULL; |
76 | | - |
77 | | - ~MimallocHeap() { |
78 | | - if(heap != NULL) |
79 | | - mi_heap_destroy(heap); |
80 | | - } |
81 | | -}; |
| 79 | +#if defined(WIN32) |
82 | 80 |
|
83 | | -static thread_local MimallocHeap TempArena; |
| 81 | +static HANDLE TempArena = NULL; |
84 | 82 |
|
85 | | -void *AllocTemporary(size_t size) { |
86 | | - if(TempArena.heap == NULL) { |
87 | | - TempArena.heap = mi_heap_new(); |
88 | | - ssassert(TempArena.heap != NULL, "out of memory"); |
89 | | - } |
90 | | - void *ptr = mi_heap_zalloc(TempArena.heap, size); |
| 83 | +void *AllocTemporary(size_t size) |
| 84 | +{ |
| 85 | + if(!TempArena) |
| 86 | + TempArena = HeapCreate(0, 0, 0); |
| 87 | + void *ptr = HeapAlloc(TempArena, HEAP_ZERO_MEMORY, size); |
91 | 88 | ssassert(ptr != NULL, "out of memory"); |
92 | 89 | return ptr; |
93 | 90 | } |
94 | 91 |
|
95 | | -void FreeAllTemporary() { |
96 | | - MimallocHeap temp; |
97 | | - std::swap(TempArena.heap, temp.heap); |
| 92 | +void FreeAllTemporary() |
| 93 | +{ |
| 94 | + HeapDestroy(TempArena); |
| 95 | + TempArena = NULL; |
| 96 | +} |
| 97 | + |
| 98 | +#endif |
| 99 | + |
| 100 | +//----------------------------------------------------------------------------- |
| 101 | +// Temporary arena, on Linux. |
| 102 | +//----------------------------------------------------------------------------- |
| 103 | + |
| 104 | +#if !defined(WIN32) |
| 105 | + |
| 106 | +struct ArenaChunk { |
| 107 | + ArenaChunk *next; |
| 108 | +}; |
| 109 | + |
| 110 | +static std::mutex TempArenaMutex; |
| 111 | +static ArenaChunk *TempArena = NULL; |
| 112 | + |
| 113 | +void *AllocTemporary(size_t size) |
| 114 | +{ |
| 115 | + ArenaChunk *chunk = (ArenaChunk *)calloc(1, sizeof(ArenaChunk) + size); |
| 116 | + ssassert(chunk != NULL, "out of memory"); |
| 117 | + std::lock_guard<std::mutex> guard(TempArenaMutex); |
| 118 | + chunk->next = TempArena; |
| 119 | + TempArena = chunk; |
| 120 | + return (void *)(chunk + 1); |
| 121 | +} |
| 122 | + |
| 123 | +void FreeAllTemporary() |
| 124 | +{ |
| 125 | + std::lock_guard<std::mutex> guard(TempArenaMutex); |
| 126 | + while(TempArena) { |
| 127 | + ArenaChunk *chunk = TempArena; |
| 128 | + TempArena = TempArena->next; |
| 129 | + free(chunk); |
| 130 | + } |
98 | 131 | } |
99 | 132 |
|
| 133 | +#endif |
| 134 | + |
100 | 135 | } |
101 | 136 | } |
0 commit comments