Repository navigation
Expand file tree
/
Copy pathintern.cpp
More file actions
194 lines (157 loc) · 4.95 KB
/
Copy pathintern.cpp
File metadata and controls
194 lines (157 loc) · 4.95 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "nc_defines.h"
#include "nc_memory.h"
#include "nc_arena.h"
#include "nc_profile.h"
#include "nc_simd.h"
#include "intern.h"
INTERNAL u64
LoadBytes(u8 const* Bytes, u64 Count)
{
u64 Result = 0;
if (Count >= 8) {
MemCpy(&Result, Bytes, 8);
} else {
for (u64 Index = 0; Index < Count; ++Index)
Result |= (u64) Bytes[Index] << (Index << 3ULL);
}
return Result;
}
INTERNAL u32
Hash(Str8 String, OUT u64* Prefix)
{
u8 const* Bytes = String.Str;
u64 Remaining = String.Size;
u64 Chunk = LoadBytes(Bytes, Remaining);
u64 StringHash = (Chunk ^ String.Size) * NAME_HASH_MUL;
*Prefix = Chunk;
if (Remaining > 8)
Remaining -= 8;
else
Remaining = 0;
Bytes += 8;
while (Remaining) {
Chunk = LoadBytes(Bytes, Remaining);
StringHash = ((StringHash ^ (StringHash >> 29)) ^ Chunk) * NAME_HASH_MUL;
if (Remaining > 8)
Remaining -= 8;
else
Remaining = 0;
Bytes += 8;
}
StringHash ^= StringHash >> 32;
u32 Result = (u64) StringHash;
return Result;
}
NamePool*
NamePoolInit(void)
{
Arena* MemPool = ArenaAlloc();
NamePool* Result = ArenaPushArrayZero(MemPool, NamePool, 1);
Result->MemPool = MemPool;
Result->NamesMemPool = ArenaAllocEx(GB(2));
Result->StringMemPool = ArenaAllocEx(GB(2));
for (u32 Index = 0; Index < ARRAY_COUNT(Result->HashMemPool); ++Index)
Result->HashMemPool[Index] = ArenaAllocEx(GB(1));
Result->Names = (Name*) ArenaExBase(Result->NamesMemPool);
Result->StringData = ArenaExBase(Result->StringMemPool);
Result->HashTableMask = (NAME_POOL_HASH_SIZE - 1);
Result->HashTable = ArenaPushArray(
Result->HashMemPool[0],
NameSlot,
NAME_POOL_HASH_SIZE
);
MemSet(Result->HashTable, 0xFF, NAME_POOL_HASH_SIZE * sizeof(NameSlot));
return Result;
}
INTERNAL void
NamePoolExpand(NamePool* Pool)
{
u32 OldSize = Pool->HashTableMask + 1;
u32 NewSize = OldSize << 1;
NameSlot* OldTable = Pool->HashTable;
u32 NewPoolIndex = Pool->HashMemPoolIndex ^ 1;
Arena* NewPool = Pool->HashMemPool[NewPoolIndex];
ArenaClear(NewPool);
Pool->HashTable = ArenaPushArray(
NewPool,
NameSlot,
NewSize
);
Pool->HashTableMask = NewSize - 1;
Pool->HashMemPoolIndex = NewPoolIndex;
MemSet(Pool->HashTable, 0xFF, NewSize * sizeof(NameSlot));
for (u32 Index = 0; Index < OldSize; ++Index) {
NameSlot Slot = OldTable[Index];
if (Slot.Index.V == NAME_INDEX_INVALID_VALUE)
continue;
u32 SlotIndex = Slot.Hash & Pool->HashTableMask;
while (Pool->HashTable[SlotIndex].Index.V != NAME_INDEX_INVALID_VALUE) {
SlotIndex = (SlotIndex + 1) & Pool->HashTableMask;
}
Pool->HashTable[SlotIndex] = Slot;
}
}
NameIndex
InternName(NamePool* Pool, Str8 String)
{
NameIndex Result = {
NAME_INDEX_INVALID_VALUE
};
u64 Prefix = 5381;
u32 StringHash = Hash(String, &Prefix);
u32 SlotIndex = StringHash & Pool->HashTableMask;
NC_CPU_PREFETCH(Pool->HashTable);
for (;;) {
NameSlot Entry = Pool->HashTable[SlotIndex];
if (Entry.Index.V == NAME_INDEX_INVALID_VALUE) {
if (Pool->Count > ((Pool->HashTableMask + 1) * 7) / 10) {
NamePoolExpand(Pool);
SlotIndex = StringHash & Pool->HashTableMask;
continue;
}
Name* NewName = ArenaPushArray(Pool->NamesMemPool, Name, 1);
u8* Bytes = ArenaPushArrayEx(
Pool->StringMemPool,
u8,
String.Size,
1
);
Result.V = Pool->Count++;
NewName->Prefix = Prefix;
NewName->Length = SafeTruncateU64(String.Size);
NewName->Offset = Pool->StringDataCount;
MemCpy(Bytes, String.Str, String.Size);
Pool->StringDataCount += SafeTruncateU64(String.Size);
Pool->HashTable[SlotIndex].Index = Result;
Pool->HashTable[SlotIndex].Hash = StringHash;
break;
}
if (Entry.Hash == StringHash) {
Name* Candidate = &Pool->Names[Entry.Index.V];
if (
Candidate->Length == String.Size &&
Candidate->Prefix == Prefix
) {
if (
String.Size <= 8 ||
MemCmp(
Pool->StringData + Candidate->Offset + 8,
String.Str + 8,
String.Size - 8
)
) {
Result = Entry.Index;
break;
}
}
}
SlotIndex = (SlotIndex + 1) & Pool->HashTableMask;
}
return Result;
}
Str8
GetName(NamePool* Pool, NameIndex Index)
{
Name* N = &Pool->Names[Index.V];
return Str(Pool->StringData + N->Offset, (u64) N->Length);
}