Skip to content

Commit d0c188a

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 0df1ba7 commit d0c188a

1 file changed

Lines changed: 54 additions & 20 deletions

File tree

src/platform/platformbase.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include "solvespace.h"
2-
#include <mimalloc.h>
32

43
#if defined(WIN32)
54
# include <Windows.h>
5+
# include <shellapi.h>
6+
#else
7+
# include <unistd.h>
8+
# include <sys/stat.h>
9+
# include <mutex>
610
#endif // defined(WIN32)
711

812
namespace SolveSpace {
@@ -63,34 +67,64 @@ void DebugPrint(const char *fmt, ...) {
6367
#endif
6468

6569
//-----------------------------------------------------------------------------
66-
// Temporary arena.
70+
// Temporary arena, on Windows.
6771
//-----------------------------------------------------------------------------
6872

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

78-
static thread_local MimallocHeap TempArena;
75+
static HANDLE TempArena = NULL;
7976

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);
8682
ssassert(ptr != NULL, "out of memory");
8783
return ptr;
8884
}
8985

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+
}
93125
}
94126

127+
#endif
128+
95129
}
96130
}

0 commit comments

Comments
 (0)