Skip to content

Commit

Permalink
StringName: Use inline static field definitions
Browse files Browse the repository at this point in the history
Before this change StringName used regular static field
definitions for its mutex, _table, configured and debug_stringname
fields.

Since in the general case the ordering of the static variable and field
initialization and destruction is undefined, it was possible that
the destruction of StringName's static fields happened prior to
the destruction of statically allocated StringName instances.

By changing the static field definitions to inline in string_name.h,
the C++17 standard guarantees the correct initialization and destruction
ordering.
  • Loading branch information
kisg committed Jul 24, 2024
1 parent 8e36f98 commit 723878b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
9 changes: 0 additions & 9 deletions core/string/string_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,10 @@ StaticCString StaticCString::create(const char *p_ptr) {
return scs;
}

StringName::_Data *StringName::_table[STRING_TABLE_LEN];

StringName _scs_create(const char *p_chr, bool p_static) {
return (p_chr[0] ? StringName(StaticCString::create(p_chr), p_static) : StringName());
}

bool StringName::configured = false;
Mutex StringName::mutex;

#ifdef DEBUG_ENABLED
bool StringName::debug_stringname = false;
#endif

void StringName::setup() {
ERR_FAIL_COND(configured);
for (int i = 0; i < STRING_TABLE_LEN; i++) {
Expand Down
8 changes: 4 additions & 4 deletions core/string/string_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,26 @@ class StringName {
_Data() {}
};

static _Data *_table[STRING_TABLE_LEN];
static inline _Data *_table[STRING_TABLE_LEN];

_Data *_data = nullptr;

void unref();
friend void register_core_types();
friend void unregister_core_types();
friend class Main;
static Mutex mutex;
static inline Mutex mutex;
static void setup();
static void cleanup();
static bool configured;
static inline bool configured = false;
#ifdef DEBUG_ENABLED
struct DebugSortReferences {
bool operator()(const _Data *p_left, const _Data *p_right) const {
return p_left->debug_references > p_right->debug_references;
}
};

static bool debug_stringname;
static inline bool debug_stringname = false;
#endif

StringName(_Data *p_data) { _data = p_data; }
Expand Down

0 comments on commit 723878b

Please sign in to comment.