CommonLib
Loading...
Searching...
No Matches
Memory.inl
Go to the documentation of this file.
1#include <string>
2
3namespace hedgedev::csl::mem
4{
5 template <typename T>
6 inline T Read(void* in_pAddress)
7 {
8 return *(T*)in_pAddress;
9 }
10
11 template <typename T, size_t Count>
12 inline std::array<T, Count> Read(void* in_pAddress)
13 {
14 std::array<T, Count> result{};
15
16 for (size_t i = 0; i < Count; i++)
17 result[i] = ((T*)in_pAddress)[i];
18
19 return result;
20 }
21
22 template <typename T>
23 inline bool Write(void* in_pAddress, const T& in_rData, size_t in_count)
24 {
25 if (!in_pAddress)
26 return false;
27
28 uint32_t oldProtectionFlags{};
29
30 ASSERT_RETURN_FALSE(Protect(in_pAddress, sizeof(in_rData), GetProtectionFlags(PageProtection::RW), &oldProtectionFlags));
31
32 for (size_t i = 0; i < in_count; i++)
33 ((T*)in_pAddress)[i] = in_rData;
34
35 ASSERT_RETURN_FALSE(Protect(in_pAddress, sizeof(in_rData), oldProtectionFlags));
36
37 return true;
38 }
39
40 template <typename T>
41 inline bool Write(void* in_pAddress, const std::vector<T>& in_rData)
42 {
43 if (!in_pAddress)
44 return false;
45
46 const auto length = sizeof(T) * in_rData.size();
47
48 uint32_t oldProtectionFlags{};
49
50 ASSERT_RETURN_FALSE(Protect(in_pAddress, length, GetProtectionFlags(PageProtection::RW), &oldProtectionFlags));
51 ASSERT_RETURN_FALSE(memcpy_s(in_pAddress, length, in_rData.data(), length) == 0);
52 ASSERT_RETURN_FALSE(Protect(in_pAddress, length, oldProtectionFlags));
53
54 return true;
55 }
56
57 template <hedgedev::csl::ut::expr::AnyString T>
58 inline bool WriteString(void* in_pAddress, const T& in_rStr)
59 {
60 if (!in_pAddress)
61 return false;
62
63 using TChar = hedgedev::csl::ut::expr::GetCharType_t<T>;
64
65 return WriteStringFixedLength(in_pAddress, in_rStr, std::basic_string_view<TChar>(in_rStr).size());
66 }
67
68 template <hedgedev::csl::ut::expr::AnyString T>
69 inline bool WriteStringFixedLength(void* in_pAddress, const T& in_rStr, size_t in_length)
70 {
71 if (!in_pAddress)
72 return false;
73
74 using TChar = hedgedev::csl::ut::expr::GetCharType_t<T>;
75
76 auto view = std::basic_string_view<TChar>(in_rStr);
77
78 auto srcLength = in_length;
79 auto dstLength = srcLength;
80
81 if (dstLength <= 0)
82 {
83 // Started at null terminator, abort.
84 if (!*(TChar*)in_pAddress)
85 return false;
86
87 dstLength = std::basic_string_view<TChar>((const TChar*)in_pAddress).size() * sizeof(TChar);
88
89 if (!dstLength)
90 return false;
91
92 srcLength = view.size() * sizeof(TChar);
93
94 if (!srcLength)
95 return false;
96
97 // Use the smallest length to fit
98 // within existing string boundaries.
99 srcLength = std::min(srcLength, dstLength);
100 }
101
102 uint32_t oldProtectionFlags{};
103
104 ASSERT_RETURN_FALSE(Protect(in_pAddress, dstLength, GetProtectionFlags(PageProtection::RW), &oldProtectionFlags));
105 ASSERT_RETURN_FALSE(memcpy_s(in_pAddress, dstLength, view.data(), srcLength) == 0);
106 memset((char*)(size_t(in_pAddress) + srcLength), 0, sizeof(TChar));
107 ASSERT_RETURN_FALSE(Protect(in_pAddress, dstLength, oldProtectionFlags));
108
109 return true;
110 }
111}
Definition Memory.h:25
bool Write(void *in_pAddress, const std::vector< T > &in_rData)
Definition Memory.inl:41
bool Write(void *in_pAddress, const T &in_rData, size_t in_count=1)
Definition Memory.inl:23
std::array< T, Count > Read(void *in_pAddress)
Definition Memory.inl:12
bool WriteString(void *in_pAddress, const T &in_rStr)
Definition Memory.inl:58
bool WriteStringFixedLength(void *in_pAddress, const T &in_rStr, size_t in_length=0)
Definition Memory.inl:69
T Read(void *in_pAddress)
Definition Memory.inl:6