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
2427void *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
2989void *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}
0 commit comments