-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost_win.cpp
More file actions
64 lines (53 loc) · 1.63 KB
/
host_win.cpp
File metadata and controls
64 lines (53 loc) · 1.63 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
#include "host.hpp"
#if defined(HOST_SYSTEM_WINDOWS)
#include "errors.hpp"
// Winapi implementation
#include <Windows.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4191) // warning C4191: 'reinterpret_cast': unsafe conversion from 'FARPROC' to 'propane::method_handle'
#endif
namespace propane
{
hostmem host::allocate(size_t len)
{
ASSERT(len, "Allocation length cannot be zero");
// Get page size
SYSTEM_INFO system_info = { 0 };
GetSystemInfo(&system_info);
const size_t page_size = static_cast<size_t>(system_info.dwPageSize);
ASSERT(page_size, "Page size is zero");
// Round up to page size
const size_t full_size = ceil_page_size(len, page_size);
// Allocate
void* const address = VirtualAlloc(nullptr, full_size, MEM_COMMIT, PAGE_READWRITE);
return hostmem{ address, full_size };
}
bool host::protect(hostmem mem)
{
DWORD old_protect;
const BOOL result = VirtualProtect(mem.address, mem.size, PAGE_READONLY, &old_protect);
return result;
}
void host::free(hostmem mem)
{
const BOOL result = VirtualFree(mem.address, 0, MEM_RELEASE);
ASSERT(result, "Failed to release memory");
}
void* host::openlib(const char* path)
{
return LoadLibraryA(path);
}
void host::closelib(void* handle)
{
FreeLibrary((HMODULE)handle);
}
method_handle host::loadsym(void* handle, const char* name)
{
return reinterpret_cast<method_handle>(GetProcAddress((HMODULE)handle, name));
}
}
#ifdef _MSC_VER
#pragma warning (pop)
#endif
#endif