forked from RetiredC/RCKangaroo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
312 lines (281 loc) · 7.1 KB
/
utils.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// This file is a part of RCKangaroo software
// (c) 2024, RetiredCoder (RC)
// License: GPLv3, see "LICENSE.TXT" file
// https://github.com/RetiredC
#include "utils.h"
#include <wchar.h>
#ifdef _WIN32
#else
void _BitScanReverse64(u32* index, u64 msk)
{
*index = 63 - __builtin_clzll(msk);
}
void _BitScanForward64(u32* index, u64 msk)
{
*index = __builtin_ffsll(msk) - 1;
}
u64 _umul128(u64 m1, u64 m2, u64* hi)
{
uint128_t ab = (uint128_t)m1 * m2; *hi = (u64)(ab >> 64); return (u64)ab;
}
u64 __shiftright128 (u64 LowPart, u64 HighPart, u8 Shift)
{
u64 ret;
__asm__ ("shrd {%[Shift],%[HighPart],%[LowPart]|%[LowPart], %[HighPart], %[Shift]}"
: [ret] "=r" (ret)
: [LowPart] "0" (LowPart), [HighPart] "r" (HighPart), [Shift] "Jc" (Shift)
: "cc");
return ret;
}
u64 __shiftleft128 (u64 LowPart, u64 HighPart, u8 Shift)
{
u64 ret;
__asm__ ("shld {%[Shift],%[LowPart],%[HighPart]|%[HighPart], %[LowPart], %[Shift]}"
: [ret] "=r" (ret)
: [LowPart] "r" (LowPart), [HighPart] "0" (HighPart), [Shift] "Jc" (Shift)
: "cc");
return ret;
}
u64 GetTickCount64()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return (u64)(ts.tv_nsec / 1000000) + ((u64)ts.tv_sec * 1000ull);
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define DB_REC_LEN 32
#define DB_FIND_LEN 9
#define DB_MIN_GROW_CNT 2
//we need advanced memory management to reduce memory fragmentation
//everything will be stable up to about 8TB RAM
#define MEM_PAGE_SIZE (128 * 1024)
#define RECS_IN_PAGE (MEM_PAGE_SIZE / DB_REC_LEN)
#define MAX_PAGES_CNT (0xFFFFFFFF / RECS_IN_PAGE)
MemPool::MemPool()
{
pnt = 0;
}
MemPool::~MemPool()
{
Clear();
}
void MemPool::Clear()
{
int cnt = (int)pages.size();
for (int i = 0; i < cnt; i++)
free(pages[i]);
pages.clear();
pnt = 0;
}
void* MemPool::AllocRec(u32* cmp_ptr)
{
void* mem;
if (pages.empty() || (pnt + DB_REC_LEN > MEM_PAGE_SIZE))
{
if (pages.size() >= MAX_PAGES_CNT)
return NULL; //overflow
pages.push_back(malloc(MEM_PAGE_SIZE));
pnt = 0;
}
u32 page_ind = (u32)pages.size() - 1;
mem = (u8*)pages[page_ind] + pnt;
*cmp_ptr = (page_ind * RECS_IN_PAGE) | (pnt / DB_REC_LEN);
pnt += DB_REC_LEN;
return mem;
}
void* MemPool::GetRecPtr(u32 cmp_ptr)
{
u32 page_ind = cmp_ptr / RECS_IN_PAGE;
u32 rec_ind = cmp_ptr % RECS_IN_PAGE;
return (u8*)pages[page_ind] + DB_REC_LEN * rec_ind;
}
TFastBase::TFastBase()
{
memset(lists, 0, sizeof(lists));
memset(Header, 0, sizeof(Header));
}
TFastBase::~TFastBase()
{
Clear();
}
void TFastBase::Clear()
{
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
for (int k = 0; k < 256; k++)
{
if (lists[i][j][k].data)
free(lists[i][j][k].data);
lists[i][j][k].data = NULL;
lists[i][j][k].capacity = 0;
lists[i][j][k].cnt = 0;
}
mps[i].Clear();
}
}
u64 TFastBase::GetBlockCnt()
{
u64 blockCount = 0;
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
for (int k = 0; k < 256; k++)
blockCount += lists[i][j][k].cnt;
return blockCount;
}
// http://en.cppreference.com/w/cpp/algorithm/lower_bound
int TFastBase::lower_bound(TListRec* list, int mps_ind, u8* data)
{
int count = list->cnt;
int it, first, step;
first = 0;
while (count > 0)
{
it = first;
step = count / 2;
it += step;
void* ptr = mps[mps_ind].GetRecPtr(list->data[it]);
if (memcmp(ptr, data, DB_FIND_LEN) < 0)
{
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
u8* TFastBase::AddDataBlock(u8* data, int pos)
{
TListRec* list = &lists[data[0]][data[1]][data[2]];
if (list->cnt >= list->capacity)
{
u32 grow = list->capacity / 2;
if (grow < DB_MIN_GROW_CNT)
grow = DB_MIN_GROW_CNT;
u32 newcap = list->capacity + grow;
if (newcap > 0xFFFF)
newcap = 0xFFFF;
if (newcap <= list->capacity)
return NULL; //failed
list->data = (u32*)realloc(list->data, newcap * sizeof(u32));
list->capacity = newcap;
}
int first = (pos < 0) ? lower_bound(list, data[0], data + 3) : pos;
memmove(list->data + first + 1, list->data + first, (list->cnt - first) * sizeof(u32));
u32 cmp_ptr;
void* ptr = mps[data[0]].AllocRec(&cmp_ptr);
list->data[first] = cmp_ptr;
memcpy(ptr, data + 3, DB_REC_LEN);
list->cnt++;
return (u8*)ptr;
}
u8* TFastBase::FindDataBlock(u8* data)
{
bool res = false;
TListRec* list = &lists[data[0]][data[1]][data[2]];
int first = lower_bound(list, data[0], data + 3);
if (first == list->cnt)
return NULL;
void* ptr = mps[data[0]].GetRecPtr(list->data[first]);
if (memcmp(ptr, data + 3, DB_FIND_LEN))
return NULL;
return (u8*)ptr;
}
u8* TFastBase::FindOrAddDataBlock(u8* data)
{
void* ptr;
TListRec* list = &lists[data[0]][data[1]][data[2]];
int first = lower_bound(list, data[0], data + 3);
if (first == list->cnt)
goto label_not_found;
ptr = mps[data[0]].GetRecPtr(list->data[first]);
if (memcmp(ptr, data + 3, DB_FIND_LEN))
goto label_not_found;
return (u8*)ptr;
label_not_found:
AddDataBlock(data, first);
return NULL;
}
//slow but I hope you are not going to create huge DB with this proof-of-concept software
bool TFastBase::LoadFromFile(char* fn)
{
Clear();
FILE* fp = fopen(fn, "rb");
if (!fp)
return false;
if (fread(Header, 1, sizeof(Header), fp) != sizeof(Header))
{
fclose(fp);
return false;
}
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
for (int k = 0; k < 256; k++)
{
TListRec* list = &lists[i][j][k];
fread(&list->cnt, 1, 2, fp);
if (list->cnt)
{
u32 grow = list->cnt / 2;
if (grow < DB_MIN_GROW_CNT)
grow = DB_MIN_GROW_CNT;
u32 newcap = list->cnt + grow;
if (newcap > 0xFFFF)
newcap = 0xFFFF;
list->data = (u32*)realloc(list->data, newcap * sizeof(u32));
list->capacity = newcap;
for (int m = 0; m < list->cnt; m++)
{
u32 cmp_ptr;
void* ptr = mps[i].AllocRec(&cmp_ptr);
list->data[m] = cmp_ptr;
if (fread(ptr, 1, DB_REC_LEN, fp) != DB_REC_LEN)
{
fclose(fp);
return false;
}
}
}
}
fclose(fp);
return true;
}
bool TFastBase::SaveToFile(char* fn)
{
FILE* fp = fopen(fn, "wb");
if (!fp)
return false;
if (fwrite(Header, 1, sizeof(Header), fp) != sizeof(Header))
{
fclose(fp);
return false;
}
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
for (int k = 0; k < 256; k++)
{
TListRec* list = &lists[i][j][k];
fwrite(&list->cnt, 1, 2, fp);
for (int m = 0; m < list->cnt; m++)
{
void* ptr = mps[i].GetRecPtr(list->data[m]);
if (fwrite(ptr, 1, DB_REC_LEN, fp) != DB_REC_LEN)
{
fclose(fp);
return false;
}
}
}
fclose(fp);
return true;
}
bool IsFileExist(char* fn)
{
FILE* fp = fopen(fn, "rb");
if (!fp)
return false;
fclose(fp);
return true;
}