Skip to content

Commit e9ffd39

Browse files
committed
Revert "Replace {Alloc,FreeAll}Temporary() with mimalloc"
This reverts commit c4ca4be. Removing the `mimalloc` dependency will make it easier to build Windows XP (maybe even 2000) compatible versions of SolveSpace.
1 parent 619d6bd commit e9ffd39

1 file changed

Lines changed: 54 additions & 19 deletions

File tree

src/platform/platformbase.cpp

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
#if defined(WIN32)
77
# include <Windows.h>
8+
# include <shellapi.h>
9+
#else
10+
# include <unistd.h>
11+
# include <sys/stat.h>
12+
# include <mutex>
813
#endif // defined(WIN32)
914

1015
#include "util.h"
@@ -68,34 +73,64 @@ void DebugPrint(const char *fmt, ...) {
6873
#endif
6974

7075
//-----------------------------------------------------------------------------
71-
// Temporary arena.
76+
// Temporary arena, on Windows.
7277
//-----------------------------------------------------------------------------
7378

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)
8280

83-
static thread_local MimallocHeap TempArena;
81+
static HANDLE TempArena = NULL;
8482

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);
9188
ssassert(ptr != NULL, "out of memory");
9289
return ptr;
9390
}
9491

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+
}
98131
}
99132

133+
#endif
134+
100135
}
101136
}

0 commit comments

Comments
 (0)