123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- /**************************************************************************/
- /* string_name.cpp */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "string_name.h"
- #include "core/os/os.h"
- #include "core/string/print_string.h"
- StaticCString StaticCString::create(const char *p_ptr) {
- StaticCString scs;
- scs.ptr = 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++) {
- _table[i] = nullptr;
- }
- configured = true;
- }
- void StringName::cleanup() {
- MutexLock lock(mutex);
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- Vector<_Data *> data;
- for (int i = 0; i < STRING_TABLE_LEN; i++) {
- _Data *d = _table[i];
- while (d) {
- data.push_back(d);
- d = d->next;
- }
- }
- print_line("\nStringName reference ranking (from most to least referenced):\n");
- data.sort_custom<DebugSortReferences>();
- int unreferenced_stringnames = 0;
- int rarely_referenced_stringnames = 0;
- for (int i = 0; i < data.size(); i++) {
- print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references));
- if (data[i]->debug_references == 0) {
- unreferenced_stringnames += 1;
- } else if (data[i]->debug_references < 5) {
- rarely_referenced_stringnames += 1;
- }
- }
- print_line(vformat("\nOut of %d StringNames, %d StringNames were never referenced during this run (0 times) (%.2f%%).", data.size(), unreferenced_stringnames, unreferenced_stringnames / float(data.size()) * 100));
- print_line(vformat("Out of %d StringNames, %d StringNames were rarely referenced during this run (1-4 times) (%.2f%%).", data.size(), rarely_referenced_stringnames, rarely_referenced_stringnames / float(data.size()) * 100));
- }
- #endif
- int lost_strings = 0;
- for (int i = 0; i < STRING_TABLE_LEN; i++) {
- while (_table[i]) {
- _Data *d = _table[i];
- if (d->static_count.get() != d->refcount.get()) {
- lost_strings++;
- if (OS::get_singleton()->is_stdout_verbose()) {
- String dname = String(d->cname ? d->cname : d->name);
- print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", dname, d->static_count.get(), d->refcount.get()));
- }
- }
- _table[i] = _table[i]->next;
- memdelete(d);
- }
- }
- if (lost_strings) {
- print_verbose(vformat("StringName: %d unclaimed string names at exit.", lost_strings));
- }
- configured = false;
- }
- void StringName::unref() {
- ERR_FAIL_COND(!configured);
- if (_data && _data->refcount.unref()) {
- MutexLock lock(mutex);
- if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
- if (_data->cname) {
- ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->cname));
- } else {
- ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->name));
- }
- }
- if (_data->prev) {
- _data->prev->next = _data->next;
- } else {
- if (_table[_data->idx] != _data) {
- ERR_PRINT("BUG!");
- }
- _table[_data->idx] = _data->next;
- }
- if (_data->next) {
- _data->next->prev = _data->prev;
- }
- memdelete(_data);
- }
- _data = nullptr;
- }
- bool StringName::operator==(const String &p_name) const {
- if (!_data) {
- return (p_name.length() == 0);
- }
- return (_data->get_name() == p_name);
- }
- bool StringName::operator==(const char *p_name) const {
- if (!_data) {
- return (p_name[0] == 0);
- }
- return (_data->get_name() == p_name);
- }
- bool StringName::operator!=(const String &p_name) const {
- return !(operator==(p_name));
- }
- bool StringName::operator!=(const char *p_name) const {
- return !(operator==(p_name));
- }
- bool StringName::operator!=(const StringName &p_name) const {
- // the real magic of all this mess happens here.
- // this is why path comparisons are very fast
- return _data != p_name._data;
- }
- void StringName::operator=(const StringName &p_name) {
- if (this == &p_name) {
- return;
- }
- unref();
- if (p_name._data && p_name._data->refcount.ref()) {
- _data = p_name._data;
- }
- }
- StringName::StringName(const StringName &p_name) {
- _data = nullptr;
- ERR_FAIL_COND(!configured);
- if (p_name._data && p_name._data->refcount.ref()) {
- _data = p_name._data;
- }
- }
- void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
- mutex.lock();
- if (*ptr == StringName()) {
- *ptr = StringName(p_name, true);
- }
- mutex.unlock();
- }
- StringName::StringName(const char *p_name, bool p_static) {
- _data = nullptr;
- ERR_FAIL_COND(!configured);
- if (!p_name || p_name[0] == 0) {
- return; //empty, ignore
- }
- MutexLock lock(mutex);
- uint32_t hash = String::hash(p_name);
- uint32_t idx = hash & STRING_TABLE_MASK;
- _data = _table[idx];
- while (_data) {
- // compare hash first
- if (_data->hash == hash && _data->get_name() == p_name) {
- break;
- }
- _data = _data->next;
- }
- if (_data && _data->refcount.ref()) {
- // exists
- if (p_static) {
- _data->static_count.increment();
- }
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- _data->debug_references++;
- }
- #endif
- return;
- }
- _data = memnew(_Data);
- _data->name = p_name;
- _data->refcount.init();
- _data->static_count.set(p_static ? 1 : 0);
- _data->hash = hash;
- _data->idx = idx;
- _data->cname = nullptr;
- _data->next = _table[idx];
- _data->prev = nullptr;
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- // Keep in memory, force static.
- _data->refcount.ref();
- _data->static_count.increment();
- }
- #endif
- if (_table[idx]) {
- _table[idx]->prev = _data;
- }
- _table[idx] = _data;
- }
- StringName::StringName(const StaticCString &p_static_string, bool p_static) {
- _data = nullptr;
- ERR_FAIL_COND(!configured);
- ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
- MutexLock lock(mutex);
- uint32_t hash = String::hash(p_static_string.ptr);
- uint32_t idx = hash & STRING_TABLE_MASK;
- _data = _table[idx];
- while (_data) {
- // compare hash first
- if (_data->hash == hash && _data->get_name() == p_static_string.ptr) {
- break;
- }
- _data = _data->next;
- }
- if (_data && _data->refcount.ref()) {
- // exists
- if (p_static) {
- _data->static_count.increment();
- }
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- _data->debug_references++;
- }
- #endif
- return;
- }
- _data = memnew(_Data);
- _data->refcount.init();
- _data->static_count.set(p_static ? 1 : 0);
- _data->hash = hash;
- _data->idx = idx;
- _data->cname = p_static_string.ptr;
- _data->next = _table[idx];
- _data->prev = nullptr;
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- // Keep in memory, force static.
- _data->refcount.ref();
- _data->static_count.increment();
- }
- #endif
- if (_table[idx]) {
- _table[idx]->prev = _data;
- }
- _table[idx] = _data;
- }
- StringName::StringName(const String &p_name, bool p_static) {
- _data = nullptr;
- ERR_FAIL_COND(!configured);
- if (p_name.is_empty()) {
- return;
- }
- MutexLock lock(mutex);
- uint32_t hash = p_name.hash();
- uint32_t idx = hash & STRING_TABLE_MASK;
- _data = _table[idx];
- while (_data) {
- if (_data->hash == hash && _data->get_name() == p_name) {
- break;
- }
- _data = _data->next;
- }
- if (_data && _data->refcount.ref()) {
- // exists
- if (p_static) {
- _data->static_count.increment();
- }
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- _data->debug_references++;
- }
- #endif
- return;
- }
- _data = memnew(_Data);
- _data->name = p_name;
- _data->refcount.init();
- _data->static_count.set(p_static ? 1 : 0);
- _data->hash = hash;
- _data->idx = idx;
- _data->cname = nullptr;
- _data->next = _table[idx];
- _data->prev = nullptr;
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- // Keep in memory, force static.
- _data->refcount.ref();
- _data->static_count.increment();
- }
- #endif
- if (_table[idx]) {
- _table[idx]->prev = _data;
- }
- _table[idx] = _data;
- }
- StringName StringName::search(const char *p_name) {
- ERR_FAIL_COND_V(!configured, StringName());
- ERR_FAIL_NULL_V(p_name, StringName());
- if (!p_name[0]) {
- return StringName();
- }
- MutexLock lock(mutex);
- uint32_t hash = String::hash(p_name);
- uint32_t idx = hash & STRING_TABLE_MASK;
- _Data *_data = _table[idx];
- while (_data) {
- // compare hash first
- if (_data->hash == hash && _data->get_name() == p_name) {
- break;
- }
- _data = _data->next;
- }
- if (_data && _data->refcount.ref()) {
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- _data->debug_references++;
- }
- #endif
- return StringName(_data);
- }
- return StringName(); //does not exist
- }
- StringName StringName::search(const char32_t *p_name) {
- ERR_FAIL_COND_V(!configured, StringName());
- ERR_FAIL_NULL_V(p_name, StringName());
- if (!p_name[0]) {
- return StringName();
- }
- MutexLock lock(mutex);
- uint32_t hash = String::hash(p_name);
- uint32_t idx = hash & STRING_TABLE_MASK;
- _Data *_data = _table[idx];
- while (_data) {
- // compare hash first
- if (_data->hash == hash && _data->get_name() == p_name) {
- break;
- }
- _data = _data->next;
- }
- if (_data && _data->refcount.ref()) {
- return StringName(_data);
- }
- return StringName(); //does not exist
- }
- StringName StringName::search(const String &p_name) {
- ERR_FAIL_COND_V(p_name.is_empty(), StringName());
- MutexLock lock(mutex);
- uint32_t hash = p_name.hash();
- uint32_t idx = hash & STRING_TABLE_MASK;
- _Data *_data = _table[idx];
- while (_data) {
- // compare hash first
- if (_data->hash == hash && p_name == _data->get_name()) {
- break;
- }
- _data = _data->next;
- }
- if (_data && _data->refcount.ref()) {
- #ifdef DEBUG_ENABLED
- if (unlikely(debug_stringname)) {
- _data->debug_references++;
- }
- #endif
- return StringName(_data);
- }
- return StringName(); //does not exist
- }
- bool operator==(const String &p_name, const StringName &p_string_name) {
- return p_name == p_string_name.operator String();
- }
- bool operator!=(const String &p_name, const StringName &p_string_name) {
- return p_name != p_string_name.operator String();
- }
- bool operator==(const char *p_name, const StringName &p_string_name) {
- return p_name == p_string_name.operator String();
- }
- bool operator!=(const char *p_name, const StringName &p_string_name) {
- return p_name != p_string_name.operator String();
- }
|