-
Notifications
You must be signed in to change notification settings - Fork 6
/
stl_ext.cpp
executable file
·373 lines (315 loc) · 8.49 KB
/
stl_ext.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "StdAfx.h"
#include "stl_ext.h"
dummy_ref_t the_dummy_ref = dummy_ref_t();
int findLeadingOne(uint v, int i)
{
if (v&0xffff0000) { i += 16; v >>= 16; }
if (v&0xff00) { i += 8; v >>= 8; }
if (v&0xf0) { i += 4; v >>= 4; }
if (v&0xc) { i += 2; v >>= 2; }
if (v&0x2) { return i + 1; }
if (v&0x1) { return i; }
return -1;
}
int findLeadingOne(uint64 v)
{
return (v&0xffffffff00000000L) ? findLeadingOne((uint) (v>>32), 32) : findLeadingOne((uint)v, 0);
}
void watch_ptr_base::unlink()
{
if (next)
next->prev = prev;
if (prev)
prev->next = next;
next = NULL;
prev = NULL;
ptr = NULL;
}
void watch_ptr_base::link(const Watchable* p)
{
ptr = p;
ASSERT(prev == NULL);
ASSERT(next == NULL);
if (p != NULL)
{
prev = &p->watch_list;
next = p->watch_list.next;
if (p->watch_list.next)
p->watch_list.next->prev = this;
p->watch_list.next = this;
}
}
void Watchable::nullReferencesTo()
{
for (watch_ptr_base* watcher=watch_list.next; watcher != NULL; watcher = watcher->next)
{
watcher->ptr = NULL;
if (watcher->prev)
watcher->prev->next = NULL;
watcher->prev = NULL;
}
}
std::mutex& _thread_name_mutex()
{
static std::mutex *m = new std::mutex;
return *m;
}
OL_ThreadNames &_thread_name_map()
{
static OL_ThreadNames *names = new OL_ThreadNames();
return *names;
}
#if _WIN32
//
// Usage: SetThreadName (-1, "MainThread");
//
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetThreadName(DWORD dwThreadID, const char* threadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
}
#endif
static void myTerminateHandler()
{
Report("terminate handler called");
string message = "<no info>";
#if !(defined(__c2__) && defined(__clang__))
std::exception_ptr eptr = std::current_exception();
try {
if (eptr) {
std::rethrow_exception(eptr);
}
} catch(const std::exception& e) {
message = e.what();
}
#endif
ASSERT_FAILED("Terminate Handler", "Exception: %s", message.c_str());
OL_Terminate(message.c_str());
}
static uint64 thread_getid()
{
uint64 tid = 0;
#if _WIN32
tid = GetCurrentThreadId();
#elif __APPLE__
pthread_threadid_np(pthread_self(), &tid);
#else // linux
tid = pthread_self();
#endif
return tid;
}
void thread_setup(const char* name)
{
if (OLG_EnableCrashHandler())
std::set_terminate(myTerminateHandler);
// random number generator per-thread
my_random_device() = new std::mt19937(random_seed());
DEBUG_RAND(("create seed %d", random_seed()));
const uint64 tid = thread_getid();
#if _WIN32
SetThreadName(tid, name);
#elif __APPLE__
pthread_setname_np(name);
#else // linux
static_assert(sizeof(pthread_t) <= sizeof(tid), "");
int status = 0;
// 16 character maximum!
if ((status = pthread_setname_np(pthread_self(), name)))
Reportf("pthread_setname_np(pthread_t, const char*) failed: %s", strerror(status));
#endif
{
std::lock_guard<std::mutex> l(_thread_name_mutex());
_thread_name_map()[tid] = name;
}
Reportf("Thread %#llx is named '%s'", tid, name);
}
void thread_cleanup()
{
deleteNull(my_random_device());
std::lock_guard<std::mutex> l(_thread_name_mutex());
_thread_name_map().erase(thread_getid());
}
const char* thread_current_name()
{
const uint64 tid = thread_getid();
std::lock_guard<std::mutex> l(_thread_name_mutex());
return _thread_name_map()[tid];
}
#if OL_USE_PTHREADS
OL_Thread thread_create(void *(*start_routine)(void *), void *arg)
{
int err = 0;
pthread_attr_t attr;
pthread_t thread;
err = pthread_attr_init(&attr);
if (err)
Reportf("pthread_attr_init error: %s", strerror(err));
err = pthread_attr_setstacksize(&attr, 8 * 1024 * 1024);
if (err)
Reportf("pthread_attr_setstacksize error: %s", strerror(err));
err = pthread_create(&thread, &attr, start_routine, arg);
if (err)
Reportf("pthread_create error: %s", strerror(err));
return thread;
}
void thread_join(OL_Thread thread)
{
if (!thread)
return;
int status = pthread_join(thread, NULL);
ASSERTF(status == 0, "pthread_join: %s", strerror(status));
}
#else
OL_Thread thread_create(void *(*start_routine)(void *), void *arg)
{
return new std::thread(start_routine, arg);
}
void thread_join(OL_Thread thread)
{
if (!thread || !thread->joinable())
return;
try {
thread->join();
} catch (std::exception &e) {
ASSERT_FAILED("std::thread::join()", "%s", e.what());
}
}
#endif
static DEFINE_CVAR(int, kMempoolMaxChain, 15);
size_t MemoryPool::create(size_t cnt)
{
if (pool)
return count;
count = cnt;
do {
#if _WIN32
pool = (char*)VirtualAlloc(NULL, count * element_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!pool)
ReportWin32Err1("VirtualAlloc", GetLastError(), __FILE__, __LINE__);
#else
pool = (char*)malloc(count * element_size);
if (!pool)
Reportf("malloc(%#x) failed: %s", (int) (count * element_size), strerror(errno));
#endif
Reportf("Allocating MemoryPool[%d](%db, %d) %.1fMB: %s",
index, (int)element_size, (int)count,
(element_size * count) / (1024.0 * 1024.0),
pool ? "OK" : "FAILED");
if (!pool)
count /= 2;
} while (count && !pool);
ASSERT(count);
if (!count)
return 0;
first = (Chunk*) pool;
for (int i=0; i<count-1; i++) {
((Chunk*) &pool[i * element_size])->next = (Chunk*) &pool[(i+1) * element_size];
}
((Chunk*) &pool[(count-1) * element_size])->next = NULL;
return count;
}
void MemoryPool::release()
{
#if _WIN32
if (pool && !VirtualFree(pool, 0, MEM_RELEASE))
ReportWin32Err1("VirtualFree", GetLastError(), __FILE__, __LINE__);
#else
free(pool);
#endif
delete next;
pool = NULL;
next = NULL;
}
MemoryPool::~MemoryPool()
{
release();
}
bool MemoryPool::isInPool(const void *pt) const
{
if (!pool)
return false;
const char *ptr = (char*) pt;
const size_t idx = (ptr - pool) / element_size;
if (idx >= count)
return next ? next->isInPool(pt) : false;
ASSERT(pool + (idx * element_size) == ptr);
return true;
}
void* MemoryPool::allocate()
{
std::lock_guard<std::mutex> l(mutex);
if (!first) {
ASSERT(pool);
if (!next) {
if (index+1 >= kMempoolMaxChain) {
ASSERT_FAILED("Memory Pool", "%d/%d pools allocated! No memory available",
index+1, kMempoolMaxChain);
OL_Terminate("Out of block memory! Pool limit hit.");
}
next = new MemoryPool(element_size);
next->index = index+1;
if (!next->create(count)) {
delete next;
next = NULL;
OL_Terminate("Out of block memory! Allocation failed.");
}
}
return next->allocate();
}
Chunk *chunk = first;
first = first->next;
used++;
return (void*) chunk;
}
void MemoryPool::deallocate(void *ptr)
{
std::lock_guard<std::mutex> l(mutex);
if (!isInPool(ptr))
{
ASSERT(next);
if (next)
next->deallocate(ptr);
return;
}
Chunk *chunk = (Chunk*) ptr;
chunk->next = first;
first = chunk;
used--;
}
// SerialCore.h
EnumType::EnumType(std::initializer_list<Pair> el) : elems(el)
{
foreach (const Pair &it, el)
{
s2v[it.first.str()] = it.second;
v2s[it.second] = it.first;
}
// its a bitset if the first 4 values don't overlap
uint64 bit = 0;
for (int i=0; i<min((int)elems.size(), 4); i++)
{
if (elems[i].second&bit)
m_isBitset = false;
bit |= elems[i].second;
}
}