Skip to content

Commit ec42112

Browse files
committed
VM (new pixeltower): Windows lazy 堆内存分配
1 parent f4b0811 commit ec42112

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

plat/windows/om_mem_plat.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
#include "openminecraft/mem/om_mem_prealloc.hpp"
22
#include "openminecraft/mem/om_mem_record.hpp"
3+
#include <Windows.h>
4+
#include <errhandlingapi.h>
35
#include <malloc.h>
6+
#include <memoryapi.h>
7+
#include <new>
8+
#include <oleauto.h>
9+
#include <winnt.h>
410

511
namespace openminecraft::mem
612
{
713
OMHeap::OMHeap(uint64_t minSize, uint64_t maxSize) : logger("OMHeap", this), maxSize(maxSize), minSize(minSize)
814
{
15+
block = VirtualAlloc(nullptr, maxSize, MEM_RESERVE, PAGE_NOACCESS);
16+
if (block == nullptr)
17+
{
18+
logger.error("[windows] VirtualAlloc fail ({})", GetLastError());
19+
throw std::bad_alloc();
20+
}
921
}
1022

1123
OMHeap::~OMHeap()
1224
{
25+
VirtualFree(block, 0, MEM_RELEASE);
1326
}
1427

1528
void OMHeap::activate(void *p, uint64_t length)
1629
{
30+
if (!VirtualAlloc(p, length, MEM_COMMIT, PAGE_READWRITE))
31+
{
32+
logger.error("[windows] VirtualAlloc fail ({})", GetLastError());
33+
throw std::bad_alloc();
34+
}
1735
}
1836

1937
void OMHeap::deactivate(void *p, uint64_t length)
2038
{
39+
if (!VirtualFree(p, length, MEM_DECOMMIT))
40+
{
41+
logger.error("[windows] VirtualFree fail ({})", GetLastError());
42+
throw std::bad_alloc();
43+
}
2144
}
2245
} // namespace openminecraft::mem
2346

0 commit comments

Comments
 (0)