Skip to content

Commit 521473e

Browse files
committed
Win32: use CRT heap instead of our own.
Historically SolveSpace used its own heap on Windows since it gave better control and debugging options, but a lot of development these days happens on Linux, where that heap was a stub around malloc/free, and also Windows debugging tools got a lot better. In terms of immediate benefit, this commit fixes heap corruption on Windows introduced in commits b4e1ce4 and 47e8279, caused by allocating with HEAP_NO_SERIALIZE in parallel from OpenMP threads. Without HEAP_NO_SERIALIZE there's no performance benefit to keeping our own heap, either. The vl() function is also removed because for development there are better tools now, and the only place where it was permanently called from became a no-op, since temporary heap always validates after FreeAllTemporary() recreates it.
1 parent 74103ee commit 521473e

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ endif()
9898

9999
# We use OpenMP to speed up some geometric operations, but it's optional.
100100
include(FindOpenMP)
101-
if(OpenMP_FOUND)
101+
# No MinGW distribution actually ships an OpenMP runtime, but FindOpenMP detects it anyway.
102+
if(OpenMP_FOUND AND NOT MINGW)
102103
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
103104
else()
104105
message(WARNING "OpenMP not found, geometric operations will be single-threaded")

src/platform/utilwin.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <shellapi.h>
1414

1515
namespace SolveSpace {
16-
static HANDLE PermHeap, TempHeap;
16+
static HANDLE TempHeap;
1717

1818
void dbp(const char *str, ...)
1919
{
@@ -51,23 +51,15 @@ void FreeAllTemporary()
5151
{
5252
if(TempHeap) HeapDestroy(TempHeap);
5353
TempHeap = HeapCreate(HEAP_NO_SERIALIZE, 1024*1024*20, 0);
54-
// This is a good place to validate, because it gets called fairly
55-
// often.
56-
vl();
5754
}
5855

5956
void *MemAlloc(size_t n) {
60-
void *p = HeapAlloc(PermHeap, HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY, n);
57+
void *p = malloc(n);
6158
ssassert(p != NULL, "Cannot allocate memory");
6259
return p;
6360
}
6461
void MemFree(void *p) {
65-
HeapFree(PermHeap, HEAP_NO_SERIALIZE, p);
66-
}
67-
68-
void vl() {
69-
ssassert(HeapValidate(TempHeap, HEAP_NO_SERIALIZE, NULL), "Corrupted heap");
70-
ssassert(HeapValidate(PermHeap, HEAP_NO_SERIALIZE, NULL), "Corrupted heap");
62+
free(p);
7163
}
7264

7365
std::vector<std::string> InitPlatform(int argc, char **argv) {
@@ -81,8 +73,6 @@ std::vector<std::string> InitPlatform(int argc, char **argv) {
8173
}
8274
#endif
8375

84-
// Create the heap used for long-lived stuff (that gets freed piecewise).
85-
PermHeap = HeapCreate(HEAP_NO_SERIALIZE, 1024*1024*20, 0);
8676
// Create the heap that we use to store Exprs and other temp stuff.
8777
FreeAllTemporary();
8878

src/solvespace.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ void *AllocTemporary(size_t n);
152152
void FreeAllTemporary();
153153
void *MemAlloc(size_t n);
154154
void MemFree(void *p);
155-
void vl(); // debug function to validate heaps
156155

157156
// End of platform-specific functions
158157
//================

0 commit comments

Comments
 (0)