Skip to content

Commit d9a8b86

Browse files
committed
VM: 堆内存管理器
1 parent 71573ec commit d9a8b86

5 files changed

Lines changed: 85 additions & 24 deletions

File tree

include/openminecraft/vm/pixeltower/om_pixeltower_memorymanager.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <map>
1010
#include <memory>
1111
#include <mutex>
12+
#include <unordered_map>
1213
#include <vector>
1314

1415
namespace openminecraft::vm::pixeltower
@@ -25,15 +26,17 @@ class OMMemoryManager
2526
void *allocateArray(std::shared_ptr<OMClass> cls, int *lengths, int dim);
2627
void *allocateArray(OMArrayType type, int *lengths, int dim);
2728

28-
void searchFromInstance(void *b, std::vector<void *> &buf);
29-
void seatchFromStatic(std::shared_ptr<OMClass> cls, std::vector<void *> &buf);
29+
void searchFromInstance(void *b);
30+
void searchFromStatic(std::shared_ptr<OMClass> cls);
3031

3132
void deallocate(void *p);
33+
void gc();
3234

3335
private:
3436
void *fetchInternal(int size);
3537

3638
log::OMLogger logger;
39+
std::unordered_map<void *, bool> blockStatus;
3740
std::mutex cacheLock;
3841
std::shared_ptr<OMClassLoader> cld;
3942
std::any tower;

src/boot/entrypoint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ int boot(std::vector<std::string> args)
235235
pt->registerNativeFunc("vmstd/internal/SystemPrintStream", "println", "(Ljava/lang/Object;)V",
236236
[&](std::any s) {
237237
auto argl = std::any_cast<std::list<std::any> *>(s);
238-
// logger->debug("[stdout] {}", std::any_cast<void
239-
// *>(*(++argl->begin())));
238+
// logger->debug("[stdout] {}",
239+
// std::any_cast<void*>(*(++argl->begin())));
240240
return nullptr;
241241
});
242242
pt->registerNativeFunc(

src/vm/bytecode/om_bytecode_descriptor.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ OMResult<std::string, std::string> decodeType(std::string raw, int *p)
3737
case 'Z':
3838
return OMResult<std::string, std::string>::ok("boolean");
3939
case 'L': {
40-
auto ends = raw.find_first_of(';', *p - 1);
40+
auto ends = raw.find_first_of(';', *p - 1) - (*p - 1);
4141
if (ends == std::variant_npos)
4242
{
4343
return OMResult<std::string, std::string>::err("nonstop object type!");
4444
}
4545
// inplace split
46-
raw[ends] = '\0';
47-
auto type = std::string(raw.substr(*p - 1, ends).c_str());
46+
auto type = std::string(raw.c_str()).substr(*p - 1, ends);
4847
auto d = OMResult<std::string, std::string>::ok(type);
49-
raw[ends] = ';';
50-
*p = ends + 1;
48+
*p += ends;
5149
return d;
5250
}
5351
case '[': {

src/vm/pixeltower/v0/om_pixeltower_memorymanager.cpp

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#include "openminecraft/vm/classfile/om_class_file.hpp"
55
#include "openminecraft/vm/pixeltower/om_pixeltower_array_structdef.hpp"
66
#include "openminecraft/vm/pixeltower/om_pixeltower_class_structdef.hpp"
7+
#include "openminecraft/vm/pixeltower/om_pixeltower_frame_structdef.hpp"
8+
#include "openminecraft/vm/pixeltower/om_pixeltower_interpreter.hpp"
79
#include <any>
810
#include <memory>
9-
#include <vector>
11+
#include <stack>
12+
#include <typeindex>
1013

1114
#define HEAP_SIZE 1024 * 128
1215

@@ -23,7 +26,64 @@ OMMemoryManager::~OMMemoryManager()
2326

2427
void *OMMemoryManager::fetchInternal(int size)
2528
{
26-
return mem::allocator::tracedCallocVMData(1, size);
29+
auto p = mem::allocator::tracedMallocVMData(size);
30+
memset(p, 0, size);
31+
blockStatus[p] = false;
32+
if (blockStatus.size() > 16384)
33+
{
34+
gc();
35+
}
36+
return p;
37+
}
38+
39+
void OMMemoryManager::gc()
40+
{
41+
logger.debug("serial gc begin");
42+
auto interpr = std::any_cast<std::shared_ptr<runtime::OMInterpreter>>(tower);
43+
for (auto p : interpr->stack)
44+
{
45+
std::stack<std::any> stkb(p.second);
46+
47+
while (!stkb.empty())
48+
{
49+
auto i = std::type_index(stkb.top().type());
50+
if (i == std::type_index(typeid(void *)))
51+
{
52+
searchFromInstance(std::any_cast<void *>(stkb.top()));
53+
}
54+
else if (i == std::type_index(typeid(std::shared_ptr<runtime::OMFrameMetadata>)))
55+
{
56+
auto fr = std::any_cast<std::shared_ptr<runtime::OMFrameMetadata>>(stkb.top());
57+
for (auto r : fr->local)
58+
{
59+
if (std::type_index(r.type()) == std::type_index(typeid(void *)))
60+
{
61+
searchFromInstance(std::any_cast<void *>(r));
62+
}
63+
}
64+
}
65+
stkb.pop();
66+
}
67+
}
68+
69+
for (auto l : cld->classes)
70+
{
71+
searchFromStatic(l.second);
72+
}
73+
74+
for (auto p = blockStatus.begin(); p != blockStatus.end();)
75+
{
76+
if (p->second)
77+
{
78+
p->second = false;
79+
++p;
80+
}
81+
else
82+
{
83+
deallocate(p->first);
84+
p = blockStatus.erase(p);
85+
}
86+
}
2787
}
2888

2989
void *OMMemoryManager::allocate(std::shared_ptr<OMClass> cls)
@@ -114,11 +174,11 @@ void *OMMemoryManager::allocateArray(OMArrayType type, int *lengths, int dim)
114174
return result;
115175
}
116176

117-
void OMMemoryManager::searchFromInstance(void *b, std::vector<void *> &buf)
177+
void OMMemoryManager::searchFromInstance(void *b)
118178
{
119179
if (b != nullptr)
120180
{
121-
buf.push_back(b);
181+
blockStatus[b] = true;
122182
}
123183
auto arrh = (OMArrayHeader *)b;
124184
if (b == nullptr)
@@ -136,7 +196,7 @@ void OMMemoryManager::searchFromInstance(void *b, std::vector<void *> &buf)
136196
auto arr = ARRAY_ACCESS(b, void *);
137197
for (int i = 0; i < arrh->length; i++)
138198
{
139-
searchFromInstance(arr[i], buf);
199+
searchFromInstance(arr[i]);
140200
}
141201
}
142202
else
@@ -146,18 +206,18 @@ void OMMemoryManager::searchFromInstance(void *b, std::vector<void *> &buf)
146206
{
147207
if (fi->type == OMFieldType::BytesP && (fi->accessFlag & JVM_Acc_Static) == 0)
148208
{
149-
searchFromInstance(*(void **)OBJECT_ACCESS(b, fi->offset), buf);
209+
searchFromInstance(*(void **)OBJECT_ACCESS(b, fi->offset));
150210
}
151211
}
152212
}
153213
}
154-
void OMMemoryManager::seatchFromStatic(std::shared_ptr<OMClass> cls, std::vector<void *> &buf)
214+
void OMMemoryManager::searchFromStatic(std::shared_ptr<OMClass> cls)
155215
{
156216
for (auto fi : cls->fields)
157217
{
158218
if (fi->type == OMFieldType::BytesP && (fi->accessFlag & JVM_Acc_Static))
159219
{
160-
searchFromInstance(*(void **)OBJECT_ACCESS(cls->staticFieldBlock, fi->offset), buf);
220+
searchFromInstance(*(void **)OBJECT_ACCESS(cls->staticFieldBlock, fi->offset));
161221
}
162222
}
163223
}

src/vm/pixeltower/v1/om_pixeltower_debugger.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ std::string OMDebugger::serializeAny(std::any data, int objDepth)
136136
return fmt::format("{}{}", prefix, fmt::styled("null", fmt::fg(fmt::color::gray)));
137137
}
138138

139+
if (objDepth == 0)
140+
{
141+
objDepth++;
142+
updatePref;
143+
objDepth--;
144+
}
145+
139146
if (arrh->classifierPointer == &ARRAY_TYPE)
140147
{
141148
std::string type;
@@ -177,13 +184,6 @@ std::string OMDebugger::serializeAny(std::any data, int objDepth)
177184
fmt::format("{}array at {} with type {}", prefix, fmt::styled(baseptr, fmt::fg(fmt::color::red)),
178185
fmt::styled(type, fmt::fg(fmt::color::light_green)));
179186

180-
if (objDepth == 0)
181-
{
182-
objDepth++;
183-
updatePref;
184-
objDepth--;
185-
}
186-
187187
for (int i = 0; i < arrh->length; i++)
188188
{
189189
target +=

0 commit comments

Comments
 (0)