Skip to content

Commit 8634d04

Browse files
committed
VM: 堆内存管理器
1 parent cc5246e commit 8634d04

4 files changed

Lines changed: 187 additions & 85 deletions

File tree

include/openminecraft/vm/pixeltower/om_pixeltower_memorymanager.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "openminecraft/vm/pixeltower/om_pixeltower_classloader.hpp"
88
#include <memory>
99
#include <mutex>
10+
#include <unordered_map>
1011
#include <vector>
1112

1213
namespace openminecraft::vm::pixeltower
@@ -16,7 +17,7 @@ namespace openminecraft::vm::pixeltower
1617
class OMMemoryManager
1718
{
1819
public:
19-
OMMemoryManager(std::shared_ptr<OMClassLoader> cld);
20+
OMMemoryManager(std::any tower, std::shared_ptr<OMClassLoader> cld);
2021
~OMMemoryManager();
2122

2223
void *allocate(std::shared_ptr<OMClass> cls);
@@ -26,13 +27,21 @@ class OMMemoryManager
2627
void searchFromInstance(void *b, std::vector<void *> &buf);
2728
void seatchFromStatic(std::shared_ptr<OMClass> cls, std::vector<void *> &buf);
2829

29-
void deallocate(std::vector<void *> &p);
30+
void deallocate(void *p);
31+
32+
void debug();
33+
void compatBlocks();
3034

3135
private:
36+
void registerBlock(void *b);
37+
void *fetchInternal(int size);
38+
3239
log::OMLogger logger;
33-
std::vector<void *> blockCache;
40+
void *buffer;
41+
std::unordered_map<void *, bool> blockStatus;
3442
std::mutex cacheLock;
3543
std::shared_ptr<OMClassLoader> cld;
44+
std::any tower;
3645
};
3746
} // namespace openminecraft::vm::pixeltower
3847

src/boot/entrypoint.cpp

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <memory>
1515
#include <stack>
1616
#include <stdexcept>
17+
#include <string>
1718
#include <thread>
1819
#include <typeindex>
1920
#include <variant>
@@ -128,54 +129,6 @@ int boot(std::vector<std::string> args)
128129
std::shared_ptr<OMClassFileParser> parser;
129130
bool recovermode = false;
130131

131-
std::thread t([&]() {
132-
while (true)
133-
{
134-
mem::castorice::printres();
135-
std::this_thread::sleep_for(std::chrono::milliseconds(500));
136-
137-
std::vector<void *> buf;
138-
139-
for (auto cls : pt->classloader->classes)
140-
{
141-
pt->mm->seatchFromStatic(cls.second, buf);
142-
}
143-
144-
auto i = std::any_cast<std::shared_ptr<runtime::OMInterpreter>>(pt->interpreter);
145-
146-
for (auto stks : i->stack)
147-
{
148-
std::stack<std::any> stk(stks.second);
149-
150-
while (!stk.empty())
151-
{
152-
if (std::type_index(stk.top().type()) == std::type_index(typeid(void *)))
153-
{
154-
pt->mm->searchFromInstance(std::any_cast<void *>(stk.top()), buf);
155-
}
156-
else if (std::type_index(stk.top().type()) ==
157-
std::type_index(typeid(std::shared_ptr<runtime::OMFrameMetadata>)))
158-
{
159-
auto frame = std::any_cast<std::shared_ptr<runtime::OMFrameMetadata>>(stk.top());
160-
for (auto fr : frame->local)
161-
{
162-
if (std::type_index(fr.type()) == std::type_index(typeid(void *)))
163-
{
164-
pt->mm->searchFromInstance(std::any_cast<void *>(fr), buf);
165-
}
166-
}
167-
}
168-
169-
stk.pop();
170-
}
171-
}
172-
173-
std::sort(buf.begin(), buf.end());
174-
buf.erase(std::unique(buf.begin(), buf.end()), buf.end());
175-
pt->mm->deallocate(buf);
176-
}
177-
});
178-
179132
if (setjmp(recoverBuffer) == 0)
180133
{
181134
program:
@@ -210,7 +163,6 @@ int boot(std::vector<std::string> args)
210163
{
211164
if (commandBuffer[1] == "loadcls")
212165
{
213-
214166
try
215167
{
216168
pt->loadClass(std::make_shared<std::ifstream>(commandBuffer[2], std::ios::binary));
@@ -238,6 +190,28 @@ int boot(std::vector<std::string> args)
238190

239191
commandBuffer.clear();
240192
}
193+
else if (commandBuffer[1] == "memtest")
194+
{
195+
std::vector<void *> pp;
196+
try
197+
{
198+
while (true)
199+
{
200+
pp.push_back(pt->mm->allocate(pt->fetchClass("java/lang/String")));
201+
}
202+
}
203+
catch (std::logic_error e)
204+
{
205+
pt->mm->debug();
206+
}
207+
208+
pt->mm->deallocate(pp[0]);
209+
pt->mm->deallocate(pp[2]);
210+
pt->mm->deallocate(pp[1]);
211+
pt->mm->debug();
212+
213+
commandBuffer.clear();
214+
}
241215
else if (commandBuffer[1] == "init")
242216
{
243217
std::vector<std::string> m;

src/vm/pixeltower/v0/om_pixeltower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ OMPixelTower::OMPixelTower() : logger("OMPixelTower", this)
2626
{
2727
interpreter = std::make_shared<runtime::OMInterpreter>(*this);
2828
classloader = std::make_shared<OMClassLoader>(interpreter);
29-
mm = std::make_shared<OMMemoryManager>(classloader);
29+
mm = std::make_shared<OMMemoryManager>(interpreter, classloader);
3030
}
3131
OMPixelTower::~OMPixelTower()
3232
{

0 commit comments

Comments
 (0)