-
Notifications
You must be signed in to change notification settings - Fork 553
Expand file tree
/
Copy pathplatformbase.cpp
More file actions
101 lines (78 loc) · 2.19 KB
/
platformbase.cpp
File metadata and controls
101 lines (78 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <cstdarg>
#include <cstdio>
#include <mimalloc.h>
#if defined(WIN32)
# include <Windows.h>
#endif // defined(WIN32)
#include "util.h"
#include "platform.h"
namespace SolveSpace {
namespace Platform {
//-----------------------------------------------------------------------------
// Debug output, on Windows.
//-----------------------------------------------------------------------------
#if defined(WIN32)
#if !defined(_alloca)
// Fix for compiling with MinGW.org GCC-6.3.0-1
#define _alloca alloca
#include <malloc.h>
#endif
void DebugPrint(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
int len = _vscprintf(fmt, va) + 1;
va_end(va);
va_start(va, fmt);
char *buf = (char *)_alloca(len);
_vsnprintf(buf, len, fmt, va);
va_end(va);
// The native version of OutputDebugString, unlike most others,
// is OutputDebugStringA.
OutputDebugStringA(buf);
OutputDebugStringA("\n");
#ifndef NDEBUG
// Duplicate to stderr in debug builds, but not in release; this is slow.
fputs(buf, stderr);
fputc('\n', stderr);
#endif
}
#endif
//-----------------------------------------------------------------------------
// Debug output, on *nix.
//-----------------------------------------------------------------------------
#if !defined(WIN32)
void DebugPrint(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
fputc('\n', stderr);
va_end(va);
}
#endif
//-----------------------------------------------------------------------------
// Temporary arena.
//-----------------------------------------------------------------------------
struct MimallocHeap {
mi_heap_t *heap = NULL;
~MimallocHeap() {
if(heap != NULL)
mi_heap_destroy(heap);
}
};
static thread_local MimallocHeap TempArena;
void *AllocTemporary(size_t size) {
if(TempArena.heap == NULL) {
TempArena.heap = mi_heap_new();
ssassert(TempArena.heap != NULL, "out of memory");
}
void *ptr = mi_heap_zalloc(TempArena.heap, size);
ssassert(ptr != NULL, "out of memory");
return ptr;
}
void FreeAllTemporary() {
MimallocHeap temp;
std::swap(TempArena.heap, temp.heap);
}
}
}