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"
9- #include < algorithm>
107#include < any>
11- #include < cstring>
128#include < memory>
13- #include < stack>
14- #include < typeindex>
159#include < vector>
1610
1711#define HEAP_SIZE 1024 * 128
@@ -22,128 +16,14 @@ std::string ARRAY_TYPE = "array";
2216OMMemoryManager::OMMemoryManager (std::any tower, std::shared_ptr<OMClassLoader> cld)
2317 : logger(" pixeltower/OMMemoryManager" , this ), cld(cld), tower(tower)
2418{
25- buffer = mem::allocator::tracedMallocVMData (HEAP_SIZE );
26- blockStatus[buffer] = true ;
27- blockStatus[(uint8_t *)buffer + HEAP_SIZE ] = false ;
2819}
2920OMMemoryManager::~OMMemoryManager ()
3021{
31- mem::allocator::tracedFreeVMData (buffer);
3222}
3323
3424void *OMMemoryManager::fetchInternal (int size)
3525{
36- alloc:
37- auto siz = size + sizeof (void *) - (size % sizeof (void *));
38-
39- auto it1 = blockStatus.begin ();
40- for (auto it2 = blockStatus.begin ();; ++it2)
41- {
42- ++it1;
43- if (it1 == blockStatus.end ())
44- {
45- break ;
46- }
47-
48- auto p = (uint8_t *)it2->first + sizeof (void *) - ((size_t )it2->first % sizeof (void *));
49-
50- if (it2->second && ((size_t )it1->first - (size_t )p) >= siz)
51- {
52- blockStatus[p] = false ;
53- blockStatus[(uint8_t *)it2->first + siz] = true ;
54- memset (it2->first , 0 , siz);
55- return p;
56- }
57- }
58-
59- auto inter = std::any_cast<std::shared_ptr<runtime::OMInterpreter>>(tower);
60- std::vector<void *> reachable;
61- for (auto h : inter->stack )
62- {
63- std::stack<std::any> d (h.second );
64-
65- while (!d.empty ())
66- {
67- if (std::type_index (d.top ().type ()) == std::type_index (typeid (void *)))
68- {
69- reachable.push_back (std::any_cast<void *>(d.top ()));
70- }
71- else if (std::type_index (d.top ().type ()) ==
72- std::type_index (typeid (std::shared_ptr<runtime::OMFrameMetadata>)))
73- {
74- for (auto l : std::any_cast<std::shared_ptr<runtime::OMFrameMetadata>>(d.top ())->local )
75- {
76- if (std::type_index (l.type ()) == std::type_index (typeid (void *)))
77- {
78- reachable.push_back (std::any_cast<void *>(l));
79- }
80- }
81- }
82- d.pop ();
83- }
84- }
85-
86- // logger.debug("{} reachable", reachable.size());
87-
88- for (auto p : blockStatus)
89- {
90- auto m = p.first ;
91- if (!p.second && m != ((uint8_t *)buffer + HEAP_SIZE ) && std::count (reachable.begin (), reachable.end (), m) == 0 )
92- {
93- deallocate (m);
94- }
95- }
96- compatBlocks ();
97- // debug();
98- goto alloc;
99- }
100-
101- void OMMemoryManager::compatBlocks ()
102- {
103- if (blockStatus.size () <= 1 )
104- {
105- throw 0 ;
106- }
107-
108- std::vector<void *> merged;
109-
110- for (auto it = blockStatus.begin ();; ++it)
111- {
112- ++it;
113- if (it == blockStatus.end ())
114- {
115- break ;
116- }
117- if (!it->second )
118- {
119- --it;
120- continue ;
121- }
122- auto next = it->first ;
123- --it;
124- if (it->second )
125- {
126- merged.push_back (next);
127- }
128- }
129-
130- for (auto m : merged)
131- {
132- blockStatus.erase (m);
133- }
134- }
135-
136- void OMMemoryManager::debug ()
137- {
138- logger.debug (" HEAP MEMORY STATUS: " );
139- for (auto it = blockStatus.begin (); it != blockStatus.end (); ++it)
140- {
141- ++it;
142- auto m = it == blockStatus.end () ? nullptr : it->first ;
143- --it;
144- logger.debug (" {} ~ {} (size {}): {}" , it->first , m, m == nullptr ? 0 : ((size_t )m - (size_t )it->first ),
145- it->second );
146- }
26+ return mem::allocator::tracedCallocVMData (1 , size);
14727}
14828
14929void *OMMemoryManager::allocate (std::shared_ptr<OMClass> cls)
@@ -284,10 +164,7 @@ void OMMemoryManager::seatchFromStatic(std::shared_ptr<OMClass> cls, std::vector
284164
285165void OMMemoryManager::deallocate (void *p)
286166{
287- if (blockStatus.count (p))
288- {
289- blockStatus[p] = true ;
290- }
167+ mem::allocator::tracedFreeVMData (p);
291168}
292169
293170} // namespace openminecraft::vm::pixeltower
0 commit comments