77#include " GcRegCapture.h"
88#include < hx/Unordered.h>
99
10+ #ifdef EMSCRIPTEN
11+ #include < emscripten/stack.h>
12+ #ifdef HXCPP_SINGLE_THREADED_APP
13+ // Use provided tools to measure stack extent
14+ #define HXCPP_EXPLICIT_STACK_EXTENT
15+ #endif
16+ #endif
17+
1018#include < string>
1119#include < stdlib.h>
1220
@@ -28,7 +36,6 @@ int gSlowPath = 0;
2836using hx::gByteMarkID ;
2937using hx::gRememberedByteMarkID ;
3038
31- // #define HXCPP_SINGLE_THREADED_APP
3239
3340namespace hx
3441{
@@ -108,10 +115,6 @@ static size_t sgMaximumFreeSpace = 1024*1024*1024;
108115static size_t sgMaximumFreeSpace = 1024 *1024 *1024 ;
109116#endif
110117
111- #ifdef EMSCRIPTEN
112- // #define HXCPP_STACK_UP
113- #endif
114-
115118
116119// #define HXCPP_GC_DEBUG_LEVEL 1
117120
@@ -5762,8 +5765,10 @@ class LocalAllocator : public hx::StackContext
57625765
57635766 bool mMoreHoles ;
57645767
5768+ #ifndef HXCPP_EXPLICIT_STACK_EXTENT
57655769 int *mTopOfStack ;
57665770 int *mBottomOfStack ;
5771+ #endif
57675772
57685773 hx::RegisterCaptureBuffer mRegisterBuf ;
57695774 int mRegisterBufSize ;
@@ -5801,7 +5806,9 @@ class LocalAllocator : public hx::StackContext
58015806
58025807 void AttachThread (int *inTopOfStack)
58035808 {
5809+ #ifndef HXCPP_EXPLICIT_STACK_EXTENT
58045810 mTopOfStack = mBottomOfStack = inTopOfStack;
5811+ #endif
58055812
58065813 mRegisterBufSize = 0 ;
58075814 mStackLocks = 0 ;
@@ -5859,7 +5866,9 @@ class LocalAllocator : public hx::StackContext
58595866 }
58605867 #endif
58615868
5869+ #ifndef HXCPP_EXPLICIT_STACK_EXTENT
58625870 mTopOfStack = mBottomOfStack = 0 ;
5871+ #endif
58635872
58645873 sGlobalAlloc ->RemoveLocalLocked (this );
58655874
@@ -5891,7 +5900,7 @@ class LocalAllocator : public hx::StackContext
58915900 // Other places may call this to ensure the the current thread is registered
58925901 // indefinitely (until forcefully revoked)
58935902 //
5894- // Normally liraries /mains will then let this dangle.
5903+ // Normally libraries /mains will then let this dangle.
58955904 //
58965905 // However after the main, on android it calls SetTopOfStack(0,true), to unregister the thread,
58975906 // because it is likely to be the ui thread, and the remaining call will be from
@@ -5916,13 +5925,28 @@ class LocalAllocator : public hx::StackContext
59165925 // SetTopOfStack(top,true) -> add stack lock
59175926 // SetTopOfStack(0,_) -> pop stack lock. If all gone, clear global stack lock
59185927 //
5928+
5929+ #ifdef HXCPP_EXPLICIT_STACK_EXTENT // {
5930+
5931+ void SetTopOfStack (int *inTop,bool inPush) { }
5932+ void PushTopOfStack (void *inTop) { }
5933+ void PopTopOfStack () { }
5934+ void SetBottomOfStack (int *inBottom) { }
5935+ void PauseForCollect () { }
5936+ void EnterGCFreeZone () { }
5937+ bool TryGCFreeZone () { return true ; }
5938+ bool TryExitGCFreeZone () { return false ; }
5939+ void ExitGCFreeZoneLocked () { }
5940+
5941+
5942+ #else // } !HXCPP_EXPLICIT_STACK_EXTENT {
59195943 void SetTopOfStack (int *inTop,bool inPush)
59205944 {
59215945 if (inTop)
59225946 {
59235947 if (!mTopOfStack )
59245948 mTopOfStack = inTop;
5925- // EMSCRIPTEN the stack grows upwards
5949+ // EMSCRIPTEN the stack grows upwards - not wasm.
59265950 // It could be that the main routine was called from deep with in the stack,
59275951 // then some callback was called from a higher location on the stack
59285952 #ifdef HXCPP_STACK_UP
@@ -5987,39 +6011,6 @@ class LocalAllocator : public hx::StackContext
59876011 #endif
59886012 }
59896013
5990- virtual void SetupStackAndCollect (bool inMajor, bool inForceCompact, bool inLocked=false ,bool inFreeIsFragged=false )
5991- {
5992- #ifndef HXCPP_SINGLE_THREADED_APP
5993- #if HXCPP_DEBUG
5994- if (mGCFreeZone )
5995- CriticalGCError (" Collecting from a GC-free thread" );
5996- #endif
5997- #endif
5998-
5999- volatile int dummy = 1 ;
6000- mBottomOfStack = (int *)&dummy;
6001-
6002- CAPTURE_REGS;
6003-
6004- if (!mTopOfStack )
6005- mTopOfStack = mBottomOfStack ;
6006- // EMSCRIPTEN the stack grows upwards
6007- #ifdef HXCPP_STACK_UP
6008- if (mBottomOfStack < mTopOfStack )
6009- mTopOfStack = mBottomOfStack ;
6010- #else
6011- if (mBottomOfStack > mTopOfStack )
6012- mTopOfStack = mBottomOfStack ;
6013- #endif
6014-
6015- #ifdef VerifyStackRead
6016- VerifyStackRead (mBottomOfStack , mTopOfStack )
6017- #endif
6018-
6019-
6020- sGlobalAlloc ->Collect (inMajor, inForceCompact, inLocked, inFreeIsFragged);
6021- }
6022-
60236014
60246015 void PauseForCollect ()
60256016 {
@@ -6145,6 +6136,58 @@ class LocalAllocator : public hx::StackContext
61456136 #endif
61466137 }
61476138
6139+ #endif // } HXCPP_EXPLICIT_STACK_EXTENT
6140+
6141+
6142+ void SetupStackAndCollect (bool inMajor, bool inForceCompact, bool inLocked=false ,bool inFreeIsFragged=false )
6143+ {
6144+ #ifndef HXCPP_SINGLE_THREADED_APP
6145+ #if HXCPP_DEBUG
6146+ if (mGCFreeZone )
6147+ CriticalGCError (" Collecting from a GC-free thread" );
6148+ #endif
6149+ #endif
6150+
6151+ #ifndef HXCPP_EXPLICIT_STACK_EXTENT
6152+ volatile int dummy = 1 ;
6153+ mBottomOfStack = (int *)&dummy;
6154+
6155+ CAPTURE_REGS;
6156+
6157+ if (!mTopOfStack )
6158+ mTopOfStack = mBottomOfStack ;
6159+
6160+ // EMSCRIPTEN the stack grows upwards
6161+ #ifdef HXCPP_STACK_UP
6162+ if (mBottomOfStack < mTopOfStack )
6163+ mTopOfStack = mBottomOfStack ;
6164+ #else
6165+ if (mBottomOfStack > mTopOfStack )
6166+ mTopOfStack = mBottomOfStack ;
6167+ #endif
6168+
6169+ #ifdef VerifyStackRead
6170+ VerifyStackRead (mBottomOfStack , mTopOfStack )
6171+ #endif
6172+
6173+ #endif
6174+
6175+
6176+ sGlobalAlloc ->Collect (inMajor, inForceCompact, inLocked, inFreeIsFragged);
6177+ }
6178+
6179+
6180+
6181+
6182+
6183+
6184+
6185+
6186+
6187+
6188+
6189+
6190+
61486191 void ExpandAlloc (int &ioSize)
61496192 {
61506193 #ifdef HXCPP_GC_NURSERY
@@ -6331,11 +6374,13 @@ class LocalAllocator : public hx::StackContext
63316374
63326375 void Mark (hx::MarkContext *__inCtx)
63336376 {
6377+ #ifndef HXCPP_SINGLE_THREADED_APP
63346378 if (!mTopOfStack )
63356379 {
63366380 Reset ();
63376381 return ;
63386382 }
6383+ #endif
63396384
63406385 #ifdef SHOW_MEM_EVENTS
63416386 // int here = 0;
@@ -6345,19 +6390,33 @@ class LocalAllocator : public hx::StackContext
63456390 #ifdef HXCPP_DEBUG
63466391 MarkPushClass (" Stack" ,__inCtx);
63476392 MarkSetMember (" Stack" ,__inCtx);
6393+
6394+ #ifdef HXCPP_EXPLICIT_STACK_EXTENT
6395+ hx::MarkConservative ( (int *)emscripten_stack_get_current (),(int *)emscripten_stack_get_base (), __inCtx);
6396+ #else
63486397 if (mTopOfStack && mBottomOfStack )
63496398 hx::MarkConservative (mBottomOfStack , mTopOfStack , __inCtx);
6399+ #endif
6400+
63506401 #ifdef HXCPP_SCRIPTABLE
63516402 MarkSetMember (" ScriptStack" ,__inCtx);
63526403 hx::MarkConservative ((int *)(stack), (int *)(pointer),__inCtx);
63536404 #endif
63546405 MarkSetMember (" Registers" ,__inCtx);
63556406 hx::MarkConservative (CAPTURE_REG_START, CAPTURE_REG_END, __inCtx);
6407+
6408+
63566409 MarkPopClass (__inCtx);
63576410 #else
6358- if (mTopOfStack && mBottomOfStack )
6359- hx::MarkConservative (mBottomOfStack , mTopOfStack , __inCtx);
6360- hx::MarkConservative (CAPTURE_REG_START, CAPTURE_REG_END, __inCtx);
6411+
6412+ #ifdef HXCPP_EXPLICIT_STACK_EXTENT
6413+ hx::MarkConservative ( (int *)emscripten_stack_get_current (), (int *) emscripten_stack_get_base (), __inCtx);
6414+ #else
6415+ if (mTopOfStack && mBottomOfStack )
6416+ hx::MarkConservative (mBottomOfStack , mTopOfStack , __inCtx);
6417+ hx::MarkConservative (CAPTURE_REG_START, CAPTURE_REG_END, __inCtx);
6418+ #endif
6419+
63616420 #ifdef HXCPP_SCRIPTABLE
63626421 hx::MarkConservative ((int *)(stack), (int *)(pointer),__inCtx);
63636422 #endif
@@ -6390,6 +6449,7 @@ inline LocalAllocator *GetLocalAlloc(bool inAllowEmpty=false)
63906449 #endif
63916450}
63926451
6452+ #ifndef HXCPP_SINGLE_THREADED_APP
63936453void WaitForSafe (LocalAllocator *inAlloc)
63946454{
63956455 inAlloc->WaitForSafe ();
@@ -6399,6 +6459,7 @@ void ReleaseFromSafe(LocalAllocator *inAlloc)
63996459{
64006460 inAlloc->ReleaseFromSafe ();
64016461}
6462+ #endif
64026463
64036464void MarkLocalAlloc (LocalAllocator *inAlloc,hx::MarkContext *__inCtx)
64046465{
0 commit comments