Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/hxcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,22 @@ typedef PropertyAccessMode PropertyAccess;
#include <hx/Undefine.h>
#include <hx/LessThanEq.h>

#ifdef HXCPP_IMMORTAL_STATICS
// Heap-allocate static objects and never delete them. Prevents static
// destruction order issues where CRT destroys objects in unpredictable
// order. OS reclaims all heap memory on process exit.
#define HX_IMMORTAL(Type, name) static Type &name = *new Type()
#define HX_IMMORTAL_GLOBAL(Type, name) Type &name = *new Type()
#define HX_IMMORTAL_FILE_SCOPE(Type, name) FILE_SCOPE Type &name = *new Type()
// Static data member of a class: declare with this inside the class, then define
// it outside with HX_IMMORTAL_GLOBAL(Type, Owner::name)
#define HX_IMMORTAL_STATIC_DECL(Type, name) static Type &name
#else
#define HX_IMMORTAL(Type, name) static Type name
#define HX_IMMORTAL_GLOBAL(Type, name) Type name
#define HX_IMMORTAL_FILE_SCOPE(Type, name) FILE_SCOPE Type name
#define HX_IMMORTAL_STATIC_DECL(Type, name) static Type name
#endif

#endif

26 changes: 20 additions & 6 deletions src/hx/CFFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,21 @@ static int sgKinds = (int)(vtAbstractBase + 5);

typedef std::map<std::string,int> KindMap;
typedef std::map<int,std::string> ReverseKindMap;
static KindMap sgKindMap;
static ReverseKindMap sgReverseKindMap;

// Created on first use rather than at load time, so that an extension calling
// hxcpp_kind_share from a static initializer never sees an unconstructed map
// (with HXCPP_IMMORTAL_STATICS these maps are initialized dynamically)
static KindMap &GetKindMap()
{
HX_IMMORTAL(KindMap, sgKindMap);
return sgKindMap;
}

static ReverseKindMap &GetReverseKindMap()
{
HX_IMMORTAL(ReverseKindMap, sgReverseKindMap);
return sgReverseKindMap;
}


int hxcpp_alloc_kind()
Expand All @@ -165,11 +178,11 @@ int hxcpp_alloc_kind()

void hxcpp_kind_share(int &ioKind,const char *inName)
{
int &kind = sgKindMap[inName];
int &kind = GetKindMap()[inName];
if (kind==0)
kind = hxcpp_alloc_kind();
ioKind = kind;
sgReverseKindMap[kind] = inName;
GetReverseKindMap()[kind] = inName;
}

String __hxcpp_get_kind(Dynamic inObject)
Expand All @@ -179,8 +192,9 @@ String __hxcpp_get_kind(Dynamic inObject)
return null();
if (type==(int)(size_t)k_cpp_pointer)
return HX_CSTRING("cpp.Pointer");
ReverseKindMap::const_iterator it = sgReverseKindMap.find(type);
if (it==sgReverseKindMap.end())
const ReverseKindMap &reverseKindMap = GetReverseKindMap();
ReverseKindMap::const_iterator it = reverseKindMap.find(type);
if (it==reverseKindMap.end())
return null();
return String::create(it->second.c_str(), it->second.size());
}
Expand Down
4 changes: 2 additions & 2 deletions src/hx/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ namespace hx
const char* EXTERN_CLASS_NAME = "extern";

#ifdef HXCPP_STACK_IDS
static std::mutex sStackMapMutex;
HX_IMMORTAL(std::mutex, sStackMapMutex);
typedef std::unordered_map<int, StackContext *> StackMap;
static StackMap sStackMap;
HX_IMMORTAL(StackMap, sStackMap);
#endif

// User settable String->Void
Expand Down
8 changes: 5 additions & 3 deletions src/hx/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ static void read_memory_barrier()
const char *_hx_dbg_find_scriptable_class_name(String className);


static std::mutex gMutex;
static std::map<int, DebuggerContext *> gMap;
static std::list<DebuggerContext *> gList;
typedef std::map<int, DebuggerContext *> DebuggerContextMap;
typedef std::list<DebuggerContext *> DebuggerContextList;
HX_IMMORTAL(std::mutex, gMutex);
HX_IMMORTAL(DebuggerContextMap, gMap);
HX_IMMORTAL(DebuggerContextList, gList);

class Breakpoints;
Breakpoints *ReleaseBreakpointsLocked(Breakpoints *inBreakpoints);
Expand Down
8 changes: 4 additions & 4 deletions src/hx/Lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void hxFreeLibrary(Module inModule) { dlclose(inModule); }

typedef std::map<std::string,Module> LoadedModule;

static LoadedModule sgLoadedModule;
HX_IMMORTAL(LoadedModule, sgLoadedModule);
typedef std::vector<Module> ModuleList;
static ModuleList sgOrderedModules;
HX_IMMORTAL(ModuleList, sgOrderedModules);

typedef hx::Object * (*prim_0)();
typedef hx::Object * (*prim_1)(hx::Object *);
Expand Down Expand Up @@ -244,7 +244,7 @@ class ExternalPrimitive : public hx::Object
namespace
{
typedef std::map<String,ExternalPrimitive *> LoadedMap;
LoadedMap sLoadedMap;
HX_IMMORTAL_GLOBAL(LoadedMap, sLoadedMap);
}


Expand Down Expand Up @@ -382,7 +382,7 @@ RegistrationMap *sgRegisteredPrims=0;



static std::vector<String> sgLibPath;
HX_IMMORTAL(std::vector<String>, sgLibPath);

static bool sgLibPathIsInit = false;

Expand Down
4 changes: 2 additions & 2 deletions src/hx/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ class Profiler
int mT0;
std::map<const char *, ProfileEntry> mProfileStats;

static std::mutex gThreadMutex;
HX_IMMORTAL_STATIC_DECL(std::mutex, gThreadMutex);
static int gThreadRefCount;
static int gProfileClock;
};
/* static */ std::mutex Profiler::gThreadMutex;
/* static */ HX_IMMORTAL_GLOBAL(std::mutex, Profiler::gThreadMutex);
/* static */ int Profiler::gThreadRefCount;
/* static */ int Profiler::gProfileClock;

Expand Down
2 changes: 1 addition & 1 deletion src/hx/RunLibs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C"
void __hxcpp_lib_main();


std::string sgResultBuffer;
HX_IMMORTAL_GLOBAL(std::string, sgResultBuffer);

HXCPP_EXTERN_CLASS_ATTRIBUTES const HX_CHAR *hxRunLibrary()
{
Expand Down
4 changes: 2 additions & 2 deletions src/hx/StdLibs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Array<unsigned char> __hxcpp_resource_bytes(String inName)
extern "C" void __hxcpp_lib_main();
namespace hx
{
static std::string initReturnBuffer;
HX_IMMORTAL(std::string, initReturnBuffer);
const char *Init(bool stayAttached)
{
try
Expand Down Expand Up @@ -848,7 +848,7 @@ Dynamic __hxcpp_create_var_args(Dynamic &inArrayFunc)



static std::mutex sgFieldMapMutex;
HX_IMMORTAL(std::mutex, sgFieldMapMutex);

typedef std::map<std::string,int> StringToField;

Expand Down
18 changes: 10 additions & 8 deletions src/hx/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,22 @@ class Telemetry

std::vector<int> *allocation_data;

static std::recursive_mutex gStashMutex;
static std::recursive_mutex gThreadMutex;
typedef std::map<void*, Telemetry*> AllocMap;

HX_IMMORTAL_STATIC_DECL(std::recursive_mutex, gStashMutex);
HX_IMMORTAL_STATIC_DECL(std::recursive_mutex, gThreadMutex);
static int gThreadRefCount;
static int gProfileClock;

static std::recursive_mutex alloc_mutex;
static std::map<void*, Telemetry*> alloc_map;
HX_IMMORTAL_STATIC_DECL(std::recursive_mutex, alloc_mutex);
HX_IMMORTAL_STATIC_DECL(AllocMap, alloc_map);
};
/* static */ std::recursive_mutex Telemetry::gStashMutex;
/* static */ std::recursive_mutex Telemetry::gThreadMutex;
/* static */ HX_IMMORTAL_GLOBAL(std::recursive_mutex, Telemetry::gStashMutex);
/* static */ HX_IMMORTAL_GLOBAL(std::recursive_mutex, Telemetry::gThreadMutex);
/* static */ int Telemetry::gThreadRefCount;
/* static */ int Telemetry::gProfileClock;
/* static */ std::recursive_mutex Telemetry::alloc_mutex;
/* static */ std::map<void*, Telemetry*> Telemetry::alloc_map;
/* static */ HX_IMMORTAL_GLOBAL(std::recursive_mutex, Telemetry::alloc_mutex);
/* static */ HX_IMMORTAL_GLOBAL(Telemetry::AllocMap, Telemetry::alloc_map);



Expand Down
6 changes: 3 additions & 3 deletions src/hx/TelemetryTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace
const char* sohName = "Small Object Heap";
const char* lohName = "Large Object Heap";

std::vector<hx::Telemetry*> created;
hx::QuickVec<void*> largeAllocs;
std::mutex largeAllocsLock;
HX_IMMORTAL_GLOBAL(std::vector<hx::Telemetry*>, created);
HX_IMMORTAL_GLOBAL(hx::QuickVec<void*>, largeAllocs);
HX_IMMORTAL_GLOBAL(std::mutex, largeAllocsLock);

bool isLargeObject(void* ptr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/hx/cppia/CppiaModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int sgNativeNameSlotCount = 0;

Array<Dynamic> gAllCppiaModules;

std::vector<hx::Resource> scriptResources;
HX_IMMORTAL_GLOBAL(std::vector<hx::Resource>, scriptResources);



Expand Down
3 changes: 2 additions & 1 deletion src/hx/cppia/CppiaVars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ void CppiaVar::visit(hx::VisitContext *__inCtx)
// --- CppiaStackVar ----


std::map<int,int> sVarIdNameMap;
typedef std::map<int,int> VarIdNameMap;
HX_IMMORTAL_GLOBAL(VarIdNameMap, sVarIdNameMap);


int getStackVarNameId(int inVarId)
Expand Down
28 changes: 14 additions & 14 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace hx

enum { gAlwaysMove = true };
typedef std::unordered_set<void *> PointerMovedSet;
PointerMovedSet sgPointerMoved;
HX_IMMORTAL_GLOBAL(PointerMovedSet, sgPointerMoved);

#else
enum { gAlwaysMove = false };
Expand Down Expand Up @@ -575,7 +575,7 @@ enum AllocType { allocNone, allocString, allocObject, allocMarked };
struct BlockDataInfo *gBlockStack = 0;
typedef hx::QuickVec<hx::Object *> ObjectStack;

static std::mutex sThreadPoolLock;
HX_IMMORTAL(std::mutex, sThreadPoolLock);

// For threaded marking/block reclaiming
static unsigned int sRunningThreads = 0;
Expand Down Expand Up @@ -690,7 +690,7 @@ struct GroupInfo

};

hx::QuickVec<GroupInfo> gAllocGroups;
HX_IMMORTAL_GLOBAL(hx::QuickVec<GroupInfo>, gAllocGroups);



Expand Down Expand Up @@ -1779,7 +1779,7 @@ struct GlobalChunks

};

GlobalChunks sGlobalChunks;
HX_IMMORTAL_GLOBAL(GlobalChunks, sGlobalChunks);

class MarkContext
{
Expand Down Expand Up @@ -2424,7 +2424,7 @@ class WeakRef;
typedef hx::QuickVec<WeakRef *> WeakRefs;

FILE_SCOPE std::mutex *sFinalizerLock = 0;
FILE_SCOPE WeakRefs sWeakRefs;
HX_IMMORTAL_FILE_SCOPE(WeakRefs, sWeakRefs);

class WeakRef : public hx::Object
{
Expand Down Expand Up @@ -2457,26 +2457,26 @@ typedef hx::QuickVec<InternalFinalizer *> FinalizerList;
FILE_SCOPE FinalizerList *sgFinalizers = 0;

typedef std::unordered_map<hx::Object *,hx::finalizer> FinalizerMap;
FILE_SCOPE FinalizerMap sFinalizerMap;
HX_IMMORTAL_FILE_SCOPE(FinalizerMap, sFinalizerMap);

typedef void (*HaxeFinalizer)(Dynamic);
typedef std::unordered_map<hx::Object *,HaxeFinalizer> HaxeFinalizerMap;
FILE_SCOPE HaxeFinalizerMap sHaxeFinalizerMap;
HX_IMMORTAL_FILE_SCOPE(HaxeFinalizerMap, sHaxeFinalizerMap);

hx::QuickVec<int> sFreeObjectIds;
HX_IMMORTAL_GLOBAL(hx::QuickVec<int>, sFreeObjectIds);
typedef std::unordered_map<hx::Object *,int> ObjectIdMap;
typedef hx::QuickVec<hx::Object *> IdObjectMap;
FILE_SCOPE ObjectIdMap sObjectIdMap;
FILE_SCOPE IdObjectMap sIdObjectMap;
HX_IMMORTAL_FILE_SCOPE(ObjectIdMap, sObjectIdMap);
HX_IMMORTAL_FILE_SCOPE(IdObjectMap, sIdObjectMap);

typedef std::unordered_set<hx::Object *> MakeZombieSet;
FILE_SCOPE MakeZombieSet sMakeZombieSet;
HX_IMMORTAL_FILE_SCOPE(MakeZombieSet, sMakeZombieSet);

typedef hx::QuickVec<hx::Object *> ZombieList;
FILE_SCOPE ZombieList sZombieList;
HX_IMMORTAL_FILE_SCOPE(ZombieList, sZombieList);

typedef hx::QuickVec<hx::HashRoot *> WeakHashList;
FILE_SCOPE WeakHashList sWeakHashList;
HX_IMMORTAL_FILE_SCOPE(WeakHashList, sWeakHashList);


InternalFinalizer::InternalFinalizer(hx::Object *inObj, finalizer inFinalizer)
Expand Down Expand Up @@ -2604,7 +2604,7 @@ struct Finalizable
}
};
typedef hx::QuickVec< Finalizable > FinalizableList;
FILE_SCOPE FinalizableList sFinalizableList;
HX_IMMORTAL_FILE_SCOPE(FinalizableList, sFinalizableList);


static double tFinalizers;
Expand Down
12 changes: 8 additions & 4 deletions src/hx/thread/ThreadLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

struct hx::thread::ThreadLocal_obj::Impl
{
static std::mutex slotLock;
static std::list<Impl*> slots;
typedef std::list<Impl*> SlotList;

// finalise() runs from the GC, which may still collect while the process exits:
// these must outlive the static destructors (see HXCPP_IMMORTAL_STATICS)
HX_IMMORTAL_STATIC_DECL(std::mutex, slotLock);
HX_IMMORTAL_STATIC_DECL(SlotList, slots);
static int counter;

const int slot;
Expand Down Expand Up @@ -45,9 +49,9 @@ struct hx::thread::ThreadLocal_obj::Impl
}
};

std::mutex hx::thread::ThreadLocal_obj::Impl::slotLock;
HX_IMMORTAL_GLOBAL(std::mutex, hx::thread::ThreadLocal_obj::Impl::slotLock);

std::list<hx::thread::ThreadLocal_obj::Impl*> hx::thread::ThreadLocal_obj::Impl::slots;
HX_IMMORTAL_GLOBAL(hx::thread::ThreadLocal_obj::Impl::SlotList, hx::thread::ThreadLocal_obj::Impl::slots);

int hx::thread::ThreadLocal_obj::Impl::counter = 0;

Expand Down
1 change: 1 addition & 0 deletions toolchain/common-defines.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<flag value="-DHXCPP_VISIT_ALLOCS" tag="haxe" />
<flag value="-DHXCPP_FLOAT32" if="HXCPP_FLOAT32" tag="haxe" />
<flag value="-DHXCPP_CHECK_POINTER" if="HXCPP_CHECK_POINTER" tag="haxe" />
<flag value="-DHXCPP_IMMORTAL_STATICS" if="HXCPP_IMMORTAL_STATICS" tag="haxe" />
<flag value="-DHXCPP_STACK_TRACE" if="HXCPP_STACK_TRACE" tag="haxe" />
<flag value="-DHXCPP_TELEMETRY" if="HXCPP_TELEMETRY" tag="haxe" />
<flag value="-DHXCPP_PROFILER" if="HXCPP_PROFILER" tag="haxe" />
Expand Down
Loading