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