-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrtrepl.h
More file actions
112 lines (89 loc) · 3.02 KB
/
crtrepl.h
File metadata and controls
112 lines (89 loc) · 3.02 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
#pragma once
#include <emmintrin.h>
// zeromem: template function for zeroing the memory area
// Requires 16-byte aligning in SSE mode and 4-byte aligning in non-SSE
#if (_M_IX86_FP == 2) || defined _M_X64
// All 64-bit processors have SSE2 support
template <class T>
__forceinline void zeromem(T* dst)
{
zeromem_sz<(sizeof(T) + 15) / 16>(dst);
}
template<size_t sz>
__forceinline void zeromem_sz(void* dst)
{
_mm_store_si128((__m128i*)dst, _mm_setzero_si128());
zeromem_sz<sz - 1>((BYTE*)dst + 16);
}
template<>
__forceinline void zeromem_sz<0>(void* dst)
{
UNREFERENCED_PARAMETER(dst);
}
#elif (_M_IX86_FP == 0)
template <class T>
__forceinline void zeromem(T* dst)
{
__asm
{
mov edi, dst
mov ecx, SIZE T / 4
xor eax, eax
rep stosd
}
}
#else
#error Unsupported SSE option.
#endif
// Class for wrapping into blocks with size multiple of 16-byte
#pragma pack(1)
template <class T>
class AlignWrapper
{
public:
T data;
BYTE align[16 - sizeof(T) % 16];
operator T*() { return &data; }
};
#pragma pack()
// Allocation replacement functions
extern HANDLE ProcessHeap;
__declspec(restrict) __forceinline void* __cdecl malloc(size_t n)
{
return HeapAlloc(ProcessHeap, 0, n);
}
__declspec(restrict) __forceinline void* __cdecl realloc(void* p, size_t n)
{
if (p == NULL)
return malloc(n);
return HeapReAlloc(ProcessHeap, 0, p, n);
}
__forceinline void __cdecl free(void* p)
{
if (p == NULL)
return;
HeapFree(ProcessHeap, 0, p);
}
void* __cdecl operator new(size_t n);
void* __cdecl operator new[](size_t n);
void __cdecl operator delete(void* p);
void __cdecl operator delete(void* p, size_t);
void __cdecl operator delete[](void* p);
// Copy a string and return its length
size_t cf_wcscpylen_s(WCHAR *strDestination, size_t numberOfElements, const WCHAR *strSource);
// Scan a string for the last occurrence of a character.
// Return index of the next position or 0 if no such character.
size_t cf_wcsrchr_pos(const WCHAR* str, size_t start_pos, WCHAR c);
// [lib] Searches for the first occurrence of any of 'control' characters in a string
size_t cf_wcscspn(const wchar_t* string, const wchar_t* control);
// [lib] Copies bytes between buffers
errno_t cf_memcpy_s(void* dest, size_t numberOfBytes, const void* src, size_t count);
// [lib] Compare the specified number of characters of two strings
int cf_wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count);
// [lib] Compare the specified number of characters of two strings, case ignored
int cf_wcsnicmp(const WCHAR* string1, const WCHAR* string2, size_t count);
// Concatenates the two strings and one argument (limited swprintf replacement)
template <class T>
int cf_swprintf_s(WCHAR* buffer, size_t sizeOfBuffer, const WCHAR* str1, T arg1, const WCHAR* str2);
// Special version for cf_swprintf_s<ULONG> with hex format instead of decimal
int cf_swprintf_s_hex(WCHAR* buffer, size_t sizeOfBuffer, const WCHAR* str1, ULONG arg1, const WCHAR* str2);