Skip to content

Commit e6bdd73

Browse files
committed
Keep track of memory allocated on linux
1 parent 957c983 commit e6bdd73

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

wasmpy/x86/lib/lib.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "lib.hpp"
22

33
std::vector<void *> registeredPages = {};
4+
std::vector<size_t> registeredPageSizes = {};
45
bytes globalTable = {};
56
bytes globalTypes = {};
67
std::vector<int> globalMut = {};
78
bytes localTypes = {};
89
uint64_t globalTableAddr;
910
uint64_t errorPageAddr;
11+
uint32_t page_size;
1012

1113
bytes errorPage = {
1214
0, 0, 0, 0, 0, 0, 0, 0, 0, // successful result struct
@@ -32,17 +34,15 @@ std::vector<T> concat(std::vector<T> v0, std::vector<std::vector<T>> vn)
3234
void *writePage(bytes data)
3335
{
3436
void *buf;
37+
size_t size = page_size * (1 + data.size() / page_size);
3538
#ifdef __linux__
36-
size_t size = sysconf(_SC_PAGE_SIZE) * (1 + data.size() / sysconf(_SC_PAGE_SIZE));
3739
buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
3840
#elif _WIN32
39-
SYSTEM_INFO sysinfo;
40-
GetSystemInfo(&sysinfo);
41-
size_t size = sysinfo.dwPageSize * (1 + data.size() / sysinfo.dwPageSize);
4241
buf = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE);
4342
#endif
4443
memcpy(buf, data.data(), data.size());
4544
registeredPages.push_back(buf);
45+
registeredPageSizes.push_back(size);
4646
return buf;
4747
}
4848

@@ -51,7 +51,7 @@ void freePages()
5151
for (size_t i = 0; i < registeredPages.size(); i++)
5252
{
5353
#ifdef __linux__
54-
munmap(registeredPages.at(i), sysconf(_SC_PAGE_SIZE));
54+
munmap(registeredPages.at(i), registeredPageSizes.at(i));
5555
#elif _WIN32
5656
VirtualFree(registeredPages.at(i), 0, MEM_RELEASE);
5757
#endif
@@ -217,15 +217,15 @@ bytes regParam64(const char *argbuf, Py_ssize_t arglen)
217217
return code;
218218
}
219219

220-
struct operation
220+
typedef struct operation
221221
{
222222
uint8_t opcode;
223223
std::vector<wchar_t *> arguments;
224224
std::vector<wchar_t *> results;
225225
long consumes;
226226
bytes code;
227227
operation(uint8_t opcode, std::vector<wchar_t *> arguments, std::vector<wchar_t *> results) : opcode(opcode), arguments(arguments), results(results), consumes(0){};
228-
};
228+
} operation;
229229

230230
static PyObject *createFunction(PyObject *self, PyObject *args)
231231
{
@@ -634,6 +634,14 @@ static PyModuleDef module = {
634634

635635
PyMODINIT_FUNC PyInit_nativelib()
636636
{
637+
#ifdef __linux__
638+
page_size = sysconf(_SC_PAGE_SIZE);
639+
#elif _WIN32
640+
SYSTEM_INFO sysinfo;
641+
GetSystemInfo(&sysinfo);
642+
page_size = sysinfo.dwPageSize;
643+
#endif
644+
637645
errorPageAddr = (uint64_t)writePage(errorPage);
638646
Py_AtExit(&freePages);
639647
return PyModule_Create(&module);

0 commit comments

Comments
 (0)