feat(util): impl memory alloc/free

This commit is contained in:
Changhua 2025-02-26 21:45:26 +08:00
parent 1e703f0f64
commit b09c236833
2 changed files with 40 additions and 0 deletions

View File

@ -279,4 +279,31 @@ std::vector<WxString> parse_wxids(const std::string &wxids)
return wx_members;
}
WxString *CreateWxString(const std::string &s)
{
std::wstring ws = util::s2w(s);
WxString *wxStr = reinterpret_cast<WxString *>(HeapAlloc(GetProcessHeap(), 8, sizeof(WxString)));
if (!wxStr) return nullptr;
size_t len = ws.length();
wchar_t *ptr = reinterpret_cast<wchar_t *>(HeapAlloc(GetProcessHeap(), 8, (len + 1) * sizeof(wchar_t)));
if (!ptr) {
HeapFree(GetProcessHeap(), 8, wxStr);
return nullptr;
}
wmemcpy(ptr, ws.c_str(), len + 1);
wxStr->wptr = ptr;
wxStr->size = static_cast<DWORD>(ws.size());
wxStr->length = static_cast<DWORD>(ws.length());
wxStr->clen = 0;
wxStr->ptr = nullptr;
return wxStr;
}
void FreeWxString(WxString *wxStr)
{
if (wxStr) {
if (wxStr->wptr) HeapFree(GetProcessHeap(), 8, const_cast<wchar_t *>(wxStr->wptr));
HeapFree(GetProcessHeap(), 8, wxStr);
}
}
} // namespace util

View File

@ -65,6 +65,19 @@ inline std::wstring get_pp_len_wstring(uint64_t addr)
return (addr && len) ? std::wstring(*reinterpret_cast<const wchar_t **>(addr), len) : L"";
}
inline std::string get_str_by_wstr_addr(uint64_t addr) { return w2s(get_pp_len_wstring(addr)); }
inline void *AllocFromHeap(size_t size) { return HeapAlloc(GetProcessHeap(), 8, size); }
inline void FreeBuffer(void *buffer)
{
if (buffer) HeapFree(GetProcessHeap(), 8, buffer);
}
template <typename T> static T *AllocBuffer(size_t count)
{
return reinterpret_cast<T *>(HeapAlloc(GetProcessHeap(), 8, sizeof(T) * count));
}
WxString *CreateWxString(const std::string &s);
void FreeWxString(WxString *wxStr);
std::unique_ptr<WxString> new_wx_string(const char *str);
std::unique_ptr<WxString> new_wx_string(const wchar_t *wstr);