aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/memory/llm.cpp
blob: 61fa29a6609ac91950d9c31542e790272ef2a63a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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 int32_t		 ParentTags[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 && ParentTags[TagIndex] == m_ParentTag)
			{
				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;
			ParentTags[TagIndex]	= m_ParentTag;
		}
		else
		{
			// should really let user know there's an overflow
		}
	}

	MemoryTrace_AnnounceCustomTag(m_Tag, m_ParentTag, TagName);
}

}  // namespace zen