Skip to content

Commit fa781cd

Browse files
authored
build wwdebug on linux (w3dhub#78)
1 parent bdc16f6 commit fa781cd

5 files changed

Lines changed: 46 additions & 21 deletions

File tree

.github/workflows/openw3d.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
wwsaveload
5353
wwmath
5454
wwutil
55+
wwdebug
5556
wwbitpack
5657
wwtranslatedb
5758
shell: bash

Code/wwdebug/wwdebug.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@
4343

4444

4545
#include "wwdebug.h"
46+
#ifdef _WIN32
4647
#include <windows.h>
48+
#endif
4749
//#include "win.h" can use this if allowed to see wwlib
4850
#include <stdlib.h>
4951
#include <stdarg.h>
5052
#include <stdio.h>
5153
#include <assert.h>
5254
#include <string.h>
5355
#include <signal.h>
56+
#include <errno.h>
5457
#include "Except.h"
5558

5659

@@ -65,7 +68,7 @@ static ProfileFunc _CurProfileStopHandler = NULL;
6568

6669
void Convert_System_Error_To_String(int id, char* buffer, int buf_len)
6770
{
68-
#ifndef _UNIX
71+
#ifndef __unix
6972
FormatMessageA(
7073
FORMAT_MESSAGE_FROM_SYSTEM,
7174
NULL,
@@ -79,7 +82,11 @@ void Convert_System_Error_To_String(int id, char* buffer, int buf_len)
7982

8083
int Get_Last_System_Error()
8184
{
85+
#ifndef __unix
8286
return GetLastError();
87+
#else
88+
return errno;
89+
#endif
8390
}
8491

8592
/***********************************************************************************************
@@ -294,7 +301,7 @@ void WWDebug_Assert_Fail(const char * expr,const char * file, int line)
294301
_CurAssertHandler(buffer);
295302

296303
} else {
297-
304+
#if _WIN32
298305
/*
299306
// If the exception handler is try to quit the game then don't show an assert.
300307
*/
@@ -316,6 +323,11 @@ void WWDebug_Assert_Fail(const char * expr,const char * file, int line)
316323
__debugbreak();
317324
return;
318325
}
326+
#else
327+
// TODO throw up a message box and/or trigger the debugger
328+
// For now, we just abort so we can trigger the debugger
329+
abort();
330+
#endif
319331
}
320332
}
321333
#endif
@@ -442,6 +454,7 @@ void WWDebug_Profile_Stop( const char * title)
442454

443455

444456
#ifdef WWDEBUG
457+
#ifdef _WIN32
445458
/***********************************************************************************************
446459
* WWDebug_DBWin32_Message_Handler -- *
447460
* *
@@ -514,4 +527,5 @@ void WWDebug_DBWin32_Message_Handler( const char * str )
514527

515528
return;
516529
}
530+
#endif /* WIN32 */
517531
#endif // WWDEBUG

Code/wwdebug/wwdebug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ bool WWDebug_Check_Trigger(int trigger_num);
9292
void WWDebug_Profile_Start( const char * title);
9393
void WWDebug_Profile_Stop( const char * title);
9494

95+
#ifdef _WIN32
9596
/*
9697
** A message handler to display to DBWIN32
9798
*/
9899
void WWDebug_DBWin32_Message_Handler( const char * message);
100+
#endif /* WIN32 */
99101
#endif
100102

101103

Code/wwdebug/wwmemlog.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
#include "wwdebug.h"
4444
#include "vector.h"
4545
#include "FastAllocator.h"
46-
#include <windows.h>
4746
#include <algorithm>
47+
#include <thread>
48+
49+
#ifndef __unix
50+
#include <windows.h>
51+
#endif
4852

4953
#define USE_FAST_ALLOCATOR
5054

@@ -72,7 +76,11 @@
7276
** method to use.
7377
*/
7478
#define MEMLOG_USE_MUTEX 0
79+
#ifdef __unix
80+
#define MEMLOG_USE_CRITICALSECTION 0
81+
#else
7582
#define MEMLOG_USE_CRITICALSECTION 1
83+
#endif
7684
#define MEMLOG_USE_FASTCRITICALSECTION 0
7785

7886

@@ -144,7 +152,7 @@ class ActiveCategoryStackClass : public VectorClass<int>
144152
public:
145153
ActiveCategoryStackClass(void) :
146154
VectorClass<int>(MAX_CATEGORY_STACK_DEPTH),
147-
ThreadID(-1),
155+
ThreadID(),
148156
Count(0)
149157
{ }
150158

@@ -155,17 +163,17 @@ class ActiveCategoryStackClass : public VectorClass<int>
155163
bool operator == (const ActiveCategoryStackClass &) { return false; }
156164
bool operator != (const ActiveCategoryStackClass &) { return true; }
157165

158-
void Init(int thread_id) { ThreadID = thread_id; Count = 0; Push(MEM_UNKNOWN); }
159-
void Set_Thread_ID(int id) { ThreadID = id; }
160-
int Get_Thread_ID(void) { return ThreadID; }
166+
void Init( std::thread::id thread_id) { ThreadID = thread_id; Count = 0; Push(MEM_UNKNOWN); }
167+
void Set_Thread_ID( std::thread::id id) { ThreadID = id; }
168+
std::thread::id Get_Thread_ID(void) { return ThreadID; }
161169

162170
void Push(int active_category) { (*this)[Count] = active_category; Count++; }
163171
void Pop(void) { Count--; }
164172
int Current(void) { return (*this)[Count-1]; }
165173

166174
protected:
167175

168-
int ThreadID;
176+
std::thread::id ThreadID;
169177
int Count;
170178
};
171179

@@ -378,7 +386,7 @@ ActiveCategoryStackClass::operator = (const ActiveCategoryStackClass & that)
378386
***************************************************************************************************/
379387
ActiveCategoryStackClass & ActiveCategoryClass::Get_Active_Stack(void)
380388
{
381-
int current_thread = ::GetCurrentThreadId();
389+
std::thread::id current_thread = std::this_thread::get_id();
382390

383391
/*
384392
** If we already have an allocated category stack for the current thread,

Code/wwdebug/wwprofile.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353
#include "wwprofile.h"
5454
#include "FastAllocator.h"
5555
#include "wwdebug.h"
56-
#include <windows.h>
5756
//#include "systimer.h"
5857
#include "systimer.h"
5958
#include "rawfile.h"
6059
#include "ffactory.h"
6160
#include "simplevec.h"
6261
#include "cpudetect.h"
6362
#include <cstdint>
63+
#include <thread>
6464

6565
static SimpleDynVecClass<WWProfileHierachyNodeClass*> ProfileCollectVector;
6666
static double TotalFrameTimes;
@@ -296,7 +296,7 @@ WWProfileHierachyNodeClass * WWProfileManager::CurrentRootNode = &WWProfileManag
296296
int WWProfileManager::FrameCounter = 0;
297297
int64_t WWProfileManager::ResetTime = 0;
298298

299-
static unsigned int ThreadID = static_cast<unsigned int>(-1);
299+
static std::thread::id ThreadID;
300300

301301

302302
/***********************************************************************************************
@@ -319,7 +319,7 @@ static unsigned int ThreadID = static_cast<unsigned int>(-1);
319319
*=============================================================================================*/
320320
void WWProfileManager::Start_Profile( const char * name )
321321
{
322-
if (::GetCurrentThreadId() != ThreadID) {
322+
if (std::this_thread::get_id() != ThreadID) {
323323
return;
324324
}
325325

@@ -333,7 +333,7 @@ void WWProfileManager::Start_Profile( const char * name )
333333

334334
void WWProfileManager::Start_Root_Profile( const char * name )
335335
{
336-
if (::GetCurrentThreadId() != ThreadID) {
336+
if (std::this_thread::get_id() != ThreadID) {
337337
return;
338338
}
339339

@@ -359,7 +359,7 @@ void WWProfileManager::Start_Root_Profile( const char * name )
359359
*=============================================================================================*/
360360
void WWProfileManager::Stop_Profile( void )
361361
{
362-
if (::GetCurrentThreadId() != ThreadID) {
362+
if (std::this_thread::get_id() != ThreadID) {
363363
return;
364364
}
365365

@@ -372,7 +372,7 @@ void WWProfileManager::Stop_Profile( void )
372372

373373
void WWProfileManager::Stop_Root_Profile( void )
374374
{
375-
if (::GetCurrentThreadId() != ThreadID) {
375+
if (std::this_thread::get_id() != ThreadID) {
376376
return;
377377
}
378378

@@ -400,8 +400,8 @@ void WWProfileManager::Stop_Root_Profile( void )
400400
* 9/24/2000 gth : Created. *
401401
*=============================================================================================*/
402402
void WWProfileManager::Reset( void )
403-
{
404-
ThreadID = ::GetCurrentThreadId();
403+
{
404+
ThreadID = std::this_thread::get_id();
405405

406406
Root.Reset();
407407
FrameCounter = 0;
@@ -742,7 +742,7 @@ WWMemoryAndTimeLog::WWMemoryAndTimeLog(const char* name)
742742
IntermediateAllocSizeStart=AllocSizeStart;
743743
StringClass tmp(0,true);
744744
for (unsigned i=0;i<TabCount;++i) tmp+="\t";
745-
WWRELEASE_SAY(("%s%s {\n",tmp,name));
745+
WWRELEASE_SAY(("%s%s {\n",tmp.Peek_Buffer(),name));
746746
TabCount++;
747747
}
748748

@@ -751,13 +751,13 @@ WWMemoryAndTimeLog::~WWMemoryAndTimeLog()
751751
if (TabCount>0) TabCount--;
752752
StringClass tmp(0,true);
753753
for (unsigned i=0;i<TabCount;++i) tmp+="\t";
754-
WWRELEASE_SAY(("%s} ",tmp));
754+
WWRELEASE_SAY(("%s} ",tmp.Peek_Buffer()));
755755

756756
unsigned current_time=WWProfile_Get_System_Time();
757757
int current_alloc_count=FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocation_Count();
758758
int current_alloc_size=FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocated_Size();
759759
WWRELEASE_SAY(("IN TOTAL %s took %d.%3.3d s, did %d memory allocations of %d bytes\n",
760-
Name,
760+
Name.Peek_Buffer(),
761761
(current_time - TimeStart)/1000, (current_time - TimeStart)%1000,
762762
current_alloc_count - AllocCountStart,
763763
current_alloc_size - AllocSizeStart));
@@ -774,7 +774,7 @@ void WWMemoryAndTimeLog::Log_Intermediate(const char* text)
774774
StringClass tmp(0,true);
775775
for (unsigned i=0;i<TabCount;++i) tmp+="\t";
776776
WWRELEASE_SAY(("%s%s took %d.%3.3d s, did %d memory allocations of %d bytes\n",
777-
tmp,
777+
tmp.Peek_Buffer(),
778778
text,
779779
(current_time - IntermediateTimeStart)/1000, (current_time - IntermediateTimeStart)%1000,
780780
current_alloc_count - IntermediateAllocCountStart,

0 commit comments

Comments
 (0)