diff options
Diffstat (limited to 'src/zencore/memory/llm.cpp')
| -rw-r--r-- | src/zencore/memory/llm.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/zencore/memory/llm.cpp b/src/zencore/memory/llm.cpp new file mode 100644 index 000000000..fe4853d49 --- /dev/null +++ b/src/zencore/memory/llm.cpp @@ -0,0 +1,77 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include <zencore/memory/llm.h> + +#include <zencore/string.h> +#include <zencore/thread.h> + +#include <atomic> + +namespace zen { + +static std::atomic<int32_t> CustomTagCounter = 257; // NOTE: hard-coded TRACE_TAG = 257 + +static const int32_t TagNamesBaseIndex = 256; +static const int32_t TrackedTagNameCount = 256; +static const char* TagNames[TrackedTagNameCount]; +static uint32_t TagNameHashes[TrackedTagNameCount]; + +static RwLock TableLock; + +FLLMTag::FLLMTag(const char* TagName) +{ + // NOTE: should add verification to prevent multiple definitions of same name? + + AssignAndAnnounceNewTag(TagName); +} + +FLLMTag::FLLMTag(const char* TagName, const FLLMTag& ParentTag) +{ + // NOTE: should add verification to prevent multiple definitions of same name? + + m_ParentTag = ParentTag.GetTag(); + + AssignAndAnnounceNewTag(TagName); +} + +void +FLLMTag::AssignAndAnnounceNewTag(const char* TagName) +{ + const uint32_t TagNameHash = HashStringDjb2(TagName); + + { + RwLock::ExclusiveLockScope _(TableLock); + + const int32_t CurrentMaxTagIndex = CustomTagCounter - TagNamesBaseIndex; + + for (int TagIndex = 0; TagIndex <= CurrentMaxTagIndex; ++TagIndex) + { + if (TagNameHashes[TagIndex] == TagNameHash) + { + m_Tag = TagIndex + TagNamesBaseIndex; + // could verify the string matches here to catch hash collisions + + // return early, no need to announce the tag as it is already known + return; + } + } + + m_Tag = ++CustomTagCounter; + + const int TagIndex = m_Tag - TagNamesBaseIndex; + + if (TagIndex < TrackedTagNameCount) + { + TagNameHashes[TagIndex] = TagNameHash; + TagNames[TagIndex] = TagName; + } + else + { + // should really let user know there's an overflow + } + } + + MemoryTrace_AnnounceCustomTag(m_Tag, m_ParentTag, TagName); +} + +} // namespace zen |