1119 lines
28 KiB
C++
1119 lines
28 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
|
|
#pragma warning(push)
|
|
#pragma warning(disable : 4311 4302 4146)
|
|
|
|
namespace stl::preserved
|
|
{
|
|
inline void* memcpy(void* dest, const void* src, size_t len)
|
|
{
|
|
char* d = (char*)dest;
|
|
const char* s = (const char*)src;
|
|
|
|
while (len--)
|
|
*d++ = *s++;
|
|
|
|
return dest;
|
|
}
|
|
|
|
inline void* memset2(void* dest, int val, size_t len)
|
|
{
|
|
__stosb((unsigned char*)dest, val, len);
|
|
|
|
return dest;
|
|
}
|
|
|
|
inline int memcmp(void* s1, void* s2, size_t n)
|
|
{
|
|
|
|
if (n == 0 || (s1 == s2)) {
|
|
return 0;
|
|
}
|
|
|
|
__m128i* ptr1 = reinterpret_cast<__m128i*>(s1);
|
|
__m128i* ptr2 = reinterpret_cast<__m128i*>(s2);
|
|
|
|
const uint8_t mode =
|
|
_SIDD_UBYTE_OPS |
|
|
_SIDD_CMP_EQUAL_EACH |
|
|
_SIDD_NEGATIVE_POLARITY |
|
|
_SIDD_LEAST_SIGNIFICANT;
|
|
|
|
for (/**/; n != 0; ptr1++, ptr2++) {
|
|
|
|
const __m128i a = _mm_loadu_si128(ptr1);
|
|
const __m128i b = _mm_loadu_si128(ptr2);
|
|
|
|
if (_mm_cmpestrc(a, (int)n, b, (int)n, mode)) {
|
|
|
|
const auto idx = _mm_cmpestri(a, (int)n, b, (int)n, mode);
|
|
|
|
const uint8_t b1 = (reinterpret_cast<char*>(ptr1))[idx];
|
|
const uint8_t b2 = (reinterpret_cast<char*>(ptr2))[idx];
|
|
|
|
if (b1 < b2) {
|
|
return -1;
|
|
}
|
|
else if (b1 > b2) {
|
|
return +1;
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (n > 16) {
|
|
n -= 16;
|
|
}
|
|
else {
|
|
n = 0;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
namespace stl
|
|
{
|
|
inline size_t strnlen(const char* s, size_t maxlen)
|
|
{
|
|
size_t len;
|
|
|
|
for (len = 0; len < maxlen; len++, s++) {
|
|
if (!*s)
|
|
break;
|
|
}
|
|
return (len);
|
|
}
|
|
|
|
inline size_t strlen(const char* s) {
|
|
|
|
size_t result = 0;
|
|
|
|
__m128i* mem = reinterpret_cast<__m128i*>(const_cast<char*>(s));
|
|
const __m128i zeros = _mm_setzero_si128();
|
|
|
|
for (/**/; /**/; mem++, result += 16) {
|
|
|
|
const __m128i data = _mm_loadu_si128(mem);
|
|
|
|
const uint8_t mode =
|
|
_SIDD_UBYTE_OPS |
|
|
_SIDD_CMP_EQUAL_EACH |
|
|
_SIDD_LEAST_SIGNIFICANT;
|
|
|
|
if (_mm_cmpistrc(data, zeros, mode)) {
|
|
const auto idx = _mm_cmpistri(data, zeros, mode);
|
|
|
|
return result + idx;
|
|
}
|
|
}
|
|
}
|
|
|
|
inline bool is_xdigit(const char* s) {
|
|
|
|
if (s == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
// 3 ranges
|
|
const __m128i ranges = _mm_setr_epi8(
|
|
'0', '9', 'a', 'f', 'A', 'F', 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0
|
|
);
|
|
|
|
__m128i* mem = reinterpret_cast<__m128i*>(const_cast<char*>(s));
|
|
|
|
const uint8_t mode =
|
|
_SIDD_UBYTE_OPS |
|
|
_SIDD_CMP_RANGES |
|
|
_SIDD_MASKED_NEGATIVE_POLARITY |
|
|
_SIDD_LEAST_SIGNIFICANT |
|
|
_SIDD_BIT_MASK;
|
|
|
|
for (/**/; /**/; mem++) {
|
|
|
|
const __m128i chunk = _mm_loadu_si128(mem);
|
|
|
|
if (_mm_cmpistrc(ranges, chunk, mode)) {
|
|
// there are some characters outside the given ranges in a chunk
|
|
return false;
|
|
}
|
|
else if (_mm_cmpistrz(ranges, chunk, mode)) {
|
|
// there is zero byte in a chunk
|
|
if (*s == 0) {
|
|
// empty string
|
|
return false;
|
|
}
|
|
else {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
inline int strcmp(const char* s1, const char* s2)
|
|
{
|
|
while (*s1 == *s2++)
|
|
if (*s1++ == 0)
|
|
return (0);
|
|
return (*(unsigned char*)s1 - *(unsigned char*)--s2);
|
|
}
|
|
|
|
inline int strncmp(const char* s1, const char* s2, size_t n)
|
|
{
|
|
if (n == 0)
|
|
return (0);
|
|
do {
|
|
if (*s1 != *s2++)
|
|
return (*(unsigned char*)s1 - *(unsigned char*)--s2);
|
|
if (*s1++ == 0)
|
|
break;
|
|
} while (--n != 0);
|
|
|
|
return (0);
|
|
}
|
|
|
|
inline char* strstr(const char* s, const char* find)
|
|
{
|
|
char c, sc;
|
|
size_t len;
|
|
if ((c = *find++) != 0) {
|
|
len = strlen(find);
|
|
do {
|
|
do {
|
|
if ((sc = *s++) == 0)
|
|
return (NULL);
|
|
} while (sc != c);
|
|
} while (strncmp(s, find, len) != 0);
|
|
s--;
|
|
}
|
|
return ((char*)s);
|
|
}
|
|
|
|
inline char* strcpy(char* d, const char* s)
|
|
{
|
|
char* saved = d;
|
|
|
|
while (*s)
|
|
{
|
|
*d++ = *s++;
|
|
}
|
|
|
|
*d = 0;
|
|
|
|
return saved;
|
|
}
|
|
|
|
inline size_t
|
|
wcslen(const wchar_t* s)
|
|
{
|
|
size_t len = 0;
|
|
while (s[len] != L'\0')
|
|
{
|
|
if (s[++len] == L'\0')
|
|
return len;
|
|
if (s[++len] == L'\0')
|
|
return len;
|
|
if (s[++len] == L'\0')
|
|
return len;
|
|
++len;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
inline void init_unicode_string(PUNICODE_STRING DestinationString, PCWSTR SourceString)
|
|
{
|
|
__int64 v2; // rax
|
|
unsigned __int64 v3; // rax
|
|
|
|
DestinationString->Buffer = (wchar_t*)SourceString;
|
|
*(DWORD*)&DestinationString->Length = 0;
|
|
|
|
if (SourceString)
|
|
{
|
|
v2 = -1i64;
|
|
|
|
do
|
|
++v2;
|
|
while (SourceString[v2]);
|
|
|
|
v3 = 2 * v2;
|
|
|
|
if (v3 >= 0xFFFE)
|
|
LOWORD(v3) = -4;
|
|
|
|
DestinationString->Length = v3;
|
|
DestinationString->MaximumLength = v3 + 2;
|
|
}
|
|
}
|
|
|
|
inline wchar_t* wcscpy(wchar_t* __restrict s1, const wchar_t* __restrict s2)
|
|
{
|
|
wchar_t* cp;
|
|
|
|
cp = s1;
|
|
|
|
while ((*cp++ = *s2++) != L'\0')
|
|
;
|
|
|
|
return (s1);
|
|
}
|
|
|
|
inline bool memeq(const void* first, const void* second, size_t length)
|
|
{
|
|
return !memcmp( (void*)first, (void*)second, length );
|
|
}
|
|
|
|
inline auto strcmp_partial(const char* first, const char* second, size_t accuracy = 0)
|
|
{
|
|
if (first == nullptr || second == nullptr)
|
|
return false;
|
|
|
|
if (first[0] != second[0])
|
|
return false;
|
|
|
|
const auto length_first = stl::strlen(first);
|
|
const auto length_second = stl::strlen(second);
|
|
|
|
if (length_first != length_second)
|
|
return false;
|
|
|
|
for (auto i = 0ull; i < length_first; i += (accuracy + 1))
|
|
{
|
|
if (first[i] != second[i])
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
inline char* __strchr(const char* s, int c_in)
|
|
{
|
|
const unsigned char* char_ptr;
|
|
const unsigned long int* longword_ptr;
|
|
unsigned long int longword, magic_bits, charmask;
|
|
unsigned char c;
|
|
c = (unsigned char)c_in;
|
|
/* Handle the first few characters by reading one character at a time.
|
|
Do this until CHAR_PTR is aligned on a longword boundary. */
|
|
for (char_ptr = (const unsigned char*)s;
|
|
((unsigned long int) char_ptr & (sizeof(longword) - 1)) != 0;
|
|
++char_ptr)
|
|
|
|
if (*char_ptr == c)
|
|
return (char*)char_ptr;
|
|
|
|
else if (*char_ptr == '\0')
|
|
return NULL;
|
|
/* All these elucidatory comments refer to 4-byte longwords,
|
|
but the theory applies equally well to 8-byte longwords. */
|
|
longword_ptr = (unsigned long int*) char_ptr;
|
|
/* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
|
|
the "holes." Note that there is a hole just to the left of
|
|
each byte, with an extra at the end:
|
|
bits: 01111110 11111110 11111110 11111111
|
|
bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
|
|
The 1-bits make sure that carries propagate to the next 0-bit.
|
|
The 0-bits provide holes for carries to fall into. */
|
|
magic_bits = -1;
|
|
magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1;
|
|
/* Set up a longword, each of whose bytes is C. */
|
|
charmask = c | (c << 8);
|
|
charmask |= charmask << 16;
|
|
if (sizeof(longword) > 4)
|
|
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
|
|
charmask |= (charmask << 16) << 16;
|
|
if (sizeof(longword) > 8)
|
|
abort();
|
|
/* Instead of the traditional loop which tests each character,
|
|
we will test a longword at a time. The tricky part is testing
|
|
if *any of the four* bytes in the longword in question are zero. */
|
|
for (;;)
|
|
{
|
|
/* We tentatively exit the loop if adding MAGIC_BITS to
|
|
LONGWORD fails to change any of the hole bits of LONGWORD.
|
|
1) Is this safe? Will it catch all the zero bytes?
|
|
Suppose there is a byte with all zeros. Any carry bits
|
|
propagating from its left will fall into the hole at its
|
|
least significant bit and stop. Since there will be no
|
|
carry from its most significant bit, the LSB of the
|
|
byte to the left will be unchanged, and the zero will be
|
|
detected.
|
|
2) Is this worthwhile? Will it ignore everything except
|
|
zero bytes? Suppose every byte of LONGWORD has a bit set
|
|
somewhere. There will be a carry into bit 8. If bit 8
|
|
is set, this will carry into bit 16. If bit 8 is clear,
|
|
one of bits 9-15 must be set, so there will be a carry
|
|
into bit 16. Similarly, there will be a carry into bit
|
|
24. If one of bits 24-30 is set, there will be a carry
|
|
into bit 31, so all of the hole bits will be changed.
|
|
The one misfire occurs when bits 24-30 are clear and bit
|
|
31 is set; in this case, the hole at bit 31 is not
|
|
changed. If we had access to the processor carry flag,
|
|
we could close this loophole by putting the fourth hole
|
|
at bit 32!
|
|
So it ignores everything except 128's, when they're aligned
|
|
properly.
|
|
3) But wait! Aren't we looking for C as well as zero?
|
|
Good point. So what we do is XOR LONGWORD with a longword,
|
|
each of whose bytes is C. This turns each byte that is C
|
|
into a zero. */
|
|
longword = *longword_ptr++;
|
|
/* Add MAGIC_BITS to LONGWORD. */
|
|
if ((((longword + magic_bits)
|
|
/* Set those bits that were unchanged by the addition. */
|
|
^ ~longword)
|
|
/* Look at only the hole bits. If any of the hole bits
|
|
are unchanged, most likely one of the bytes was a
|
|
zero. */
|
|
& ~magic_bits) != 0
|
|
/* That caught zeroes. Now test for C. */
|
|
|| ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
|
|
& ~magic_bits) != 0)
|
|
{
|
|
/* Which of the bytes was C or zero?
|
|
If none of them were, it was a misfire; continue the search. */
|
|
const unsigned char* cp = (const unsigned char*)(longword_ptr - 1);
|
|
if (*cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (sizeof(longword) > 4)
|
|
{
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
if (*++cp == c)
|
|
return (char*)cp;
|
|
else if (*cp == '\0')
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
inline char* __strrchr(const char* s, int c)
|
|
{
|
|
const char* found, * p;
|
|
c = (unsigned char)c;
|
|
/* Since strchr is fast, we use it rather than the obvious loop. */
|
|
if (c == '\0')
|
|
return __strchr(s, '\0');
|
|
found = NULL;
|
|
while ((p = __strchr(s, c)) != NULL)
|
|
{
|
|
found = p;
|
|
s = p + 1;
|
|
}
|
|
return (char*)found;
|
|
}
|
|
|
|
inline char* basename(const char* filename)
|
|
{
|
|
char* p = __strrchr(filename, '/');
|
|
return p ? p + 1 : (char*)filename;
|
|
}
|
|
|
|
template <typename t>
|
|
t clamp(t x, t min, t max)
|
|
{
|
|
if (x < min) x = min;
|
|
if (x > max) x = max;
|
|
|
|
return x;
|
|
}
|
|
|
|
template < typename T >
|
|
inline T mem_alloc(size_t length, ulong protect = PAGE_READWRITE)
|
|
{
|
|
auto result = win_api::heap_alloc(
|
|
win_api::get_process_heap(), 0, length
|
|
);
|
|
|
|
if (result != nullptr)
|
|
return (T)result;
|
|
|
|
return T{};
|
|
}
|
|
|
|
inline auto mem_free(void* p)
|
|
{
|
|
return LI_FN(HeapFree)(win_api::get_process_heap(), 0, p);
|
|
}
|
|
|
|
inline char* strndup(const char* s, size_t n)
|
|
{
|
|
size_t len = strnlen(s, n);
|
|
char* _new = mem_alloc<char*>(len + 1);
|
|
|
|
if (_new == NULL)
|
|
return NULL;
|
|
|
|
_new[len] = '\0';
|
|
|
|
return (char*)memcpy(_new, s, len);
|
|
}
|
|
|
|
#ifndef ULONG_MAX
|
|
#define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
|
|
#endif
|
|
#ifndef LONG_MAX
|
|
#define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
|
|
#endif
|
|
#ifndef LONG_MIN
|
|
#define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
|
|
#endif
|
|
|
|
inline long strtol(const char* nptr, char** endptr, int base)
|
|
{
|
|
const char* s = nptr;
|
|
unsigned long acc;
|
|
int c;
|
|
unsigned long cutoff;
|
|
int neg = 0, any, cutlim;
|
|
|
|
do {
|
|
c = *s++;
|
|
} while (math::isspace(c));
|
|
if (c == '-') {
|
|
neg = 1;
|
|
c = *s++;
|
|
}
|
|
else if (c == '+')
|
|
c = *s++;
|
|
if ((base == 0 || base == 16) &&
|
|
c == '0' && (*s == 'x' || *s == 'X')) {
|
|
c = s[1];
|
|
s += 2;
|
|
base = 16;
|
|
}
|
|
if (base == 0)
|
|
base = c == '0' ? 8 : 10;
|
|
|
|
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
|
|
cutlim = cutoff % (unsigned long)base;
|
|
cutoff /= (unsigned long)base;
|
|
for (acc = 0, any = 0;; c = *s++) {
|
|
if (math::isdigit(c))
|
|
c -= '0';
|
|
else if (math::isalpha(c))
|
|
c -= math::isupper(c) ? 'A' - 10 : 'a' - 10;
|
|
else
|
|
break;
|
|
if (c >= base)
|
|
break;
|
|
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
|
|
any = -1;
|
|
else {
|
|
any = 1;
|
|
acc *= base;
|
|
acc += c;
|
|
}
|
|
}
|
|
if (any < 0) {
|
|
acc = neg ? LONG_MIN : LONG_MAX;
|
|
}
|
|
else if (neg)
|
|
acc = -acc;
|
|
if (endptr != 0)
|
|
*endptr = (char*)(any ? s - 1 : nptr);
|
|
return (acc);
|
|
}
|
|
|
|
inline double strtod(char* str, char** ptr)
|
|
{
|
|
char* p;
|
|
if (ptr == (char**)0)
|
|
return math::atof(str);
|
|
|
|
p = str;
|
|
|
|
while (math::isspace(*p))
|
|
++p;
|
|
|
|
if (*p == '+' || *p == '-')
|
|
++p;
|
|
/* INF or INFINITY. */
|
|
if ((p[0] == 'i' || p[0] == 'I')
|
|
&& (p[1] == 'n' || p[1] == 'N')
|
|
&& (p[2] == 'f' || p[2] == 'F'))
|
|
{
|
|
if ((p[3] == 'i' || p[3] == 'I')
|
|
&& (p[4] == 'n' || p[4] == 'N')
|
|
&& (p[5] == 'i' || p[5] == 'I')
|
|
&& (p[6] == 't' || p[6] == 'T')
|
|
&& (p[7] == 'y' || p[7] == 'Y'))
|
|
{
|
|
*ptr = p + 8;
|
|
return math::atof(str);
|
|
}
|
|
else
|
|
{
|
|
*ptr = p + 3;
|
|
return math::atof(str);
|
|
}
|
|
}
|
|
/* NAN or NAN(foo). */
|
|
if ((p[0] == 'n' || p[0] == 'N')
|
|
&& (p[1] == 'a' || p[1] == 'A')
|
|
&& (p[2] == 'n' || p[2] == 'N'))
|
|
{
|
|
p += 3;
|
|
if (*p == '(')
|
|
{
|
|
++p;
|
|
while (*p != '\0' && *p != ')')
|
|
++p;
|
|
if (*p == ')')
|
|
++p;
|
|
}
|
|
*ptr = p;
|
|
return math::atof(str);
|
|
}
|
|
/* digits, with 0 or 1 periods in it. */
|
|
if (math::isdigit(*p) || *p == '.')
|
|
{
|
|
int got_dot = 0;
|
|
while (math::isdigit(*p) || (!got_dot && *p == '.'))
|
|
{
|
|
if (*p == '.')
|
|
got_dot = 1;
|
|
++p;
|
|
}
|
|
/* Exponent. */
|
|
if (*p == 'e' || *p == 'E')
|
|
{
|
|
int i;
|
|
i = 1;
|
|
if (p[i] == '+' || p[i] == '-')
|
|
++i;
|
|
if (math::isdigit(p[i]))
|
|
{
|
|
while (math::isdigit(p[i]))
|
|
++i;
|
|
*ptr = p + i;
|
|
return math::atof(str);
|
|
}
|
|
}
|
|
*ptr = p;
|
|
return math::atof(str);
|
|
}
|
|
/* Didn't find any digits. Doesn't look like a number. */
|
|
*ptr = str;
|
|
return 0.0;
|
|
}
|
|
|
|
class c_pool_buffer
|
|
{
|
|
public:
|
|
c_pool_buffer() = delete;
|
|
~c_pool_buffer()
|
|
{
|
|
if (m_data)
|
|
mem_free(m_data);
|
|
}
|
|
|
|
c_pool_buffer(size_t length, bool zero = true)
|
|
{
|
|
m_length = length;
|
|
m_data = mem_alloc<void*>(m_length);
|
|
|
|
if (m_data && zero)
|
|
RtlSecureZeroMemory(m_data, length);
|
|
}
|
|
|
|
auto length() const { return m_length; }
|
|
auto valid() const { return !(m_data == nullptr); }
|
|
auto ptr() const { return m_data; }
|
|
private:
|
|
size_t m_length{};
|
|
void* m_data{};
|
|
};
|
|
|
|
template < typename T >
|
|
struct entry_t
|
|
{
|
|
bool m_is_head;
|
|
T m_data;
|
|
entry_t<T>* m_next;
|
|
};
|
|
|
|
template < typename T >
|
|
class c_linked_list
|
|
{
|
|
public:
|
|
const auto count() const { return m_count; }
|
|
size_t index_of(T& value);
|
|
|
|
void insert(const T& value);
|
|
void remove(const T& value);
|
|
bool remove_ex(const T& value);
|
|
void remove_all_occurrences(const T& value);
|
|
|
|
T* first();
|
|
T* last();
|
|
entry_t<T>* first_entry();
|
|
entry_t<T>* last_entry();
|
|
|
|
T* operator[](size_t index)
|
|
{
|
|
size_t current = 0;
|
|
|
|
for (auto* data = first_entry(); data != nullptr; data = data->m_next)
|
|
{
|
|
if (data->m_is_head)
|
|
continue;
|
|
|
|
auto& entry = data->m_data;
|
|
|
|
if (current == index)
|
|
return &entry;
|
|
|
|
++current;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
private:
|
|
size_t m_count;
|
|
entry_t<T>* m_head;
|
|
};
|
|
|
|
template < typename T >
|
|
size_t c_linked_list<T>::index_of(T& value)
|
|
{
|
|
size_t index = 0;
|
|
size_t result = 0;
|
|
|
|
for (auto* entry = first_entry(); entry != nullptr; entry = entry->m_next)
|
|
{
|
|
if (entry->m_is_head)
|
|
continue;
|
|
|
|
if (memeq(&entry->m_data, &value, sizeof(value)))
|
|
{
|
|
result = index;
|
|
break;
|
|
}
|
|
|
|
++index;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template < typename T >
|
|
void c_linked_list<T>::insert(const T& value)
|
|
{
|
|
auto* le = last_entry();
|
|
|
|
if (le == nullptr)
|
|
{
|
|
m_head = mem_alloc< entry_t<T>* >(sizeof(entry_t<T>));
|
|
|
|
if (m_head == nullptr)
|
|
return;
|
|
|
|
memset(m_head, 0, sizeof(entry_t<T>));
|
|
|
|
m_head->m_is_head = true;
|
|
|
|
le = last_entry();
|
|
|
|
if (le == nullptr)
|
|
return;
|
|
}
|
|
|
|
auto*& new_entry = le->m_next;
|
|
|
|
new_entry = mem_alloc< entry_t<T>* >(sizeof(entry_t<T>));
|
|
|
|
if (new_entry == nullptr)
|
|
return;
|
|
|
|
memset(new_entry, 0, sizeof(entry_t<T>));
|
|
memcpy(&new_entry->m_data, &value, sizeof(value));
|
|
|
|
++m_count;
|
|
}
|
|
|
|
template < typename T >
|
|
void c_linked_list<T>::remove(const T& value)
|
|
{
|
|
entry_t<T>* previous = nullptr;
|
|
entry_t<T>* current = nullptr;
|
|
entry_t<T>* next = nullptr;
|
|
|
|
for (auto* entry = first_entry(); entry != nullptr; entry = entry->m_next)
|
|
{
|
|
next = entry->m_next;
|
|
current = entry;
|
|
|
|
if (memeq(&entry->m_data, &value, sizeof(value)))
|
|
{
|
|
mem_free(entry);
|
|
previous->m_next = next;
|
|
|
|
--m_count;
|
|
break;
|
|
}
|
|
|
|
previous = entry;
|
|
}
|
|
}
|
|
|
|
template < typename T >
|
|
bool c_linked_list<T>::remove_ex(const T& value)
|
|
{
|
|
auto result = false;
|
|
|
|
entry_t<T>* previous = nullptr;
|
|
entry_t<T>* current = nullptr;
|
|
entry_t<T>* next = nullptr;
|
|
|
|
for (auto* entry = first_entry(); entry != nullptr; entry = entry->m_next)
|
|
{
|
|
next = entry->m_next;
|
|
current = entry;
|
|
|
|
if (!memcmp(&entry->m_data, &value, sizeof(value)))
|
|
{
|
|
mem_free(entry);
|
|
previous->m_next = next;
|
|
|
|
result = true;
|
|
--m_count;
|
|
break;
|
|
}
|
|
|
|
previous = entry;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template < typename T >
|
|
void c_linked_list<T>::remove_all_occurrences(const T& value)
|
|
{
|
|
do
|
|
{
|
|
if (!remove_ex(value))
|
|
break;
|
|
} while (true);
|
|
}
|
|
|
|
template < typename T >
|
|
T* c_linked_list<T>::first()
|
|
{
|
|
return m_head->m_data;
|
|
}
|
|
|
|
template < typename T >
|
|
T* c_linked_list<T>::last()
|
|
{
|
|
auto* result = m_head;
|
|
|
|
if (result == nullptr)
|
|
return nullptr;
|
|
|
|
while (result != nullptr && result->next != nullptr)
|
|
result = result->next;
|
|
|
|
return result->m_data;
|
|
}
|
|
|
|
template < typename T >
|
|
entry_t<T>* c_linked_list<T>::first_entry()
|
|
{
|
|
return m_head;
|
|
}
|
|
|
|
template < typename T >
|
|
entry_t<T>* c_linked_list<T>::last_entry()
|
|
{
|
|
auto* result = m_head;
|
|
|
|
if (result == nullptr)
|
|
return nullptr;
|
|
|
|
while (result != nullptr && result->m_next != nullptr)
|
|
result = result->m_next;
|
|
|
|
return result;
|
|
}
|
|
|
|
class spinlock
|
|
{
|
|
public:
|
|
//
|
|
// Manually fine-tuned.
|
|
//
|
|
static constexpr unsigned max_wait = 65536;
|
|
|
|
bool try_lock() noexcept
|
|
{
|
|
return !lock_.test_and_set(std::memory_order_acquire);
|
|
}
|
|
|
|
void lock() noexcept
|
|
{
|
|
unsigned wait = 1;
|
|
|
|
while (!try_lock())
|
|
{
|
|
for (unsigned i = 0; i < wait; ++i)
|
|
{
|
|
YieldProcessor();
|
|
}
|
|
|
|
//
|
|
// Don't call "pause" too many times. If the wait becomes too big,
|
|
// clamp it to the max_wait.
|
|
//
|
|
wait = min(wait * 2, max_wait);
|
|
}
|
|
}
|
|
|
|
void unlock() noexcept
|
|
{
|
|
lock_.clear(std::memory_order_release);
|
|
}
|
|
|
|
private:
|
|
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
|
|
};
|
|
|
|
class my_lock_guard
|
|
{
|
|
public:
|
|
my_lock_guard() = delete;
|
|
my_lock_guard(spinlock* lock)
|
|
{
|
|
m_lock = lock;
|
|
|
|
if (m_lock)
|
|
m_lock->lock();
|
|
}
|
|
|
|
~my_lock_guard()
|
|
{
|
|
if (m_lock)
|
|
m_lock->unlock();
|
|
}
|
|
private:
|
|
spinlock* m_lock;
|
|
};
|
|
|
|
class c_handle_guard
|
|
{
|
|
public:
|
|
c_handle_guard() = delete;
|
|
c_handle_guard(HANDLE handle)
|
|
{
|
|
m_handle = handle;
|
|
}
|
|
|
|
~c_handle_guard()
|
|
{
|
|
if (m_handle != nullptr)
|
|
{
|
|
LI_FN(CloseHandle).cached()(m_handle);
|
|
}
|
|
}
|
|
private:
|
|
HANDLE m_handle = nullptr;
|
|
};
|
|
|
|
template < typename T, size_t S >
|
|
class c_fixed_list
|
|
{
|
|
public:
|
|
void initialize()
|
|
{
|
|
memset(m_data, 0, sizeof(m_data));
|
|
|
|
m_ready = 1;
|
|
}
|
|
|
|
const auto capacity() { return S; }
|
|
const auto count() { return m_count; }
|
|
|
|
void insert(const T& element) noexcept
|
|
{
|
|
if (!m_ready)
|
|
return;
|
|
|
|
if ((m_count + 1) > S)
|
|
return;
|
|
|
|
m_data[m_count] = element;
|
|
++m_count;
|
|
}
|
|
|
|
T* at(size_t i) noexcept
|
|
{
|
|
if (!m_ready)
|
|
return nullptr;
|
|
|
|
if (i < 0 || i >= capacity() || i >= count())
|
|
return nullptr;
|
|
|
|
return &m_data[i];
|
|
}
|
|
private:
|
|
uint8_t m_ready{};
|
|
size_t m_count{};
|
|
T m_data[S]{ 0 };
|
|
};
|
|
|
|
template < typename T>
|
|
class c_heap_list
|
|
{
|
|
public:
|
|
bool initialize(size_t count)
|
|
{
|
|
const auto length = (count * sizeof(T));
|
|
|
|
m_capacity = count;
|
|
m_data = mem_alloc<T*>(length);
|
|
|
|
if (m_data == nullptr)
|
|
return false;
|
|
|
|
memset(m_data, 0, length);
|
|
|
|
m_ready = 1;
|
|
|
|
return true;
|
|
}
|
|
|
|
const auto capacity() { return m_capacity; }
|
|
const auto count() { return m_count; }
|
|
|
|
void insert(const T& element) noexcept
|
|
{
|
|
if (!m_ready)
|
|
return;
|
|
|
|
if ((m_count + 1) > m_capacity)
|
|
return;
|
|
|
|
m_data[m_count] = element;
|
|
++m_count;
|
|
}
|
|
|
|
T* at(size_t i) noexcept
|
|
{
|
|
if (!m_ready)
|
|
return nullptr;
|
|
|
|
if (i < 0 || i >= capacity() || i >= count())
|
|
return nullptr;
|
|
|
|
return &m_data[i];
|
|
}
|
|
private:
|
|
uint8_t m_ready{};
|
|
size_t m_capacity{};
|
|
size_t m_count{};
|
|
T* m_data{};
|
|
};
|
|
|
|
class c_string_table
|
|
{
|
|
public:
|
|
void insert(const char* text);
|
|
const char* get(int i);
|
|
|
|
auto count() { return m_data.count(); }
|
|
private:
|
|
using entry = char[MAX_PATH];
|
|
private:
|
|
c_linked_list<entry> m_data{ };
|
|
};
|
|
|
|
inline void c_string_table::insert(const char* text)
|
|
{
|
|
if (text == nullptr)
|
|
return;
|
|
|
|
auto length = stl::strlen(text);
|
|
|
|
if (length > sizeof(entry))
|
|
return;
|
|
|
|
char data[sizeof(entry)];
|
|
|
|
memset(data, 0, sizeof(data));
|
|
stl::strcpy(data, text);
|
|
|
|
m_data.insert(data);
|
|
}
|
|
|
|
inline const char* c_string_table::get(int i)
|
|
{
|
|
if (i > (m_data.count() - 1))
|
|
return nullptr;
|
|
|
|
auto entry = m_data[i];
|
|
|
|
if (entry == nullptr)
|
|
return nullptr;
|
|
|
|
return *entry;
|
|
}
|
|
|
|
class c_string
|
|
{
|
|
public:
|
|
c_string(const char* contents)
|
|
{
|
|
memset(m_buffer, 0, sizeof(m_buffer));
|
|
strcpy(m_buffer, contents);
|
|
}
|
|
|
|
const char* c_str() noexcept { return m_buffer; }
|
|
char* data() noexcept { return m_buffer; }
|
|
private:
|
|
char m_buffer[256];
|
|
};
|
|
}
|
|
|
|
#pragma warning(pop)
|
|
|
|
extern void* operator new(size_t size);
|
|
extern void* operator new[](size_t size);
|
|
|
|
extern void operator delete(void* ptr);
|
|
extern void operator delete[](void* ptr);
|
|
|
|
#include "stl/vector.h"
|