Skip to content

Commit 7c4f67a

Browse files
author
Robert Lindner
committed
malloc tools for VM
1 parent e7ff80c commit 7c4f67a

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

Programs/DynamicAlloc/DynAlloc.bca

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
LITERAL 15
2+
ALLOC
3+
LITERAL #a
4+
STORE
5+
6+
LITERAL 8
7+
ALLOC
8+
LITERAL #b
9+
STORE
10+
11+
LITERAL #a
12+
LOAD
13+
FREE
14+
15+
LITERAL 5
16+
ALLOC
17+
LITERAL #a
18+
STORE
19+
20+
LITERAL 7
21+
ALLOC
22+
LITERAL #c
23+
STORE
24+
25+
LITERAL #b
26+
LOAD
27+
FREE
28+
29+
LITERAL #a
30+
LOAD
31+
FREE
32+
33+
LITERAL #c
34+
LOAD
35+
FREE

build/genie.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ project "Bytecode"
2727
objdir "obj/debug"
2828
defines { "_DEBUG" }
2929
flags { "Symbols" }
30-
debugargs { "cRun", "./Programs/Functions/Functions.bca" }
30+
debugargs { "cRun", "./Programs/DynamicAlloc/DynAlloc.bca" }
3131
configuration "Release"
3232
targetdir "../bin/release/"
3333
objdir "obj/debug"

source/VirtualMachine.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ void VirtualMachine::SetProgram(std::vector<uint8> bytecode)
6767
m_HeapBase = m_FirstSegmentPtr+sizeof(uint32);
6868
Pack<uint32>(m_FirstSegmentPtr, m_HeapBase);
6969
Pack<uint32>(m_HeapBase, MAX_RAM - m_HeapBase);
70-
Pack<uint32>(m_HeapBase+sizeof(uint32), 0);
70+
Pack<uint32>(m_HeapBase + sizeof(uint32), 0);
71+
72+
#ifdef VM_DEBUG_HEAP
73+
PrintHeap();
74+
#endif
7175

7276
ProgramLoaded = true;
7377
}
@@ -87,7 +91,7 @@ void VirtualMachine::Interpret()
8791

8892
Opcode operation = static_cast<Opcode>(m_RAM[m_ProgramCounter]);
8993

90-
//std::cout << "[DBG] operation: " << GetOpString(operation) << std::endl;
94+
std::cout << "[DBG] operation: " << GetOpString(operation) << std::endl;
9195

9296
switch(operation)
9397
{
@@ -200,6 +204,11 @@ void VirtualMachine::Interpret()
200204
Pack<uint32>(prevNextPtr, Unpack<uint32>(bestFitPtr+sizeof(uint32)));//Link the previous segment to next segment
201205
}
202206
Push(bestFitPtr+sizeof(uint32));
207+
++m_ProgramCounter;
208+
209+
#ifdef VM_DEBUG_HEAP
210+
PrintHeap();
211+
#endif
203212
}
204213
continue;
205214
//Mark the space at (a) as unused
@@ -210,7 +219,7 @@ void VirtualMachine::Interpret()
210219

211220
uint32 existingNextPtr = m_FirstSegmentPtr;
212221
uint32 nextSegment = Unpack<uint32>(existingNextPtr);
213-
uint32 existingSegment;
222+
uint32 existingSegment = existingNextPtr;
214223

215224
bool earlyOut = false;
216225
while (nextSegment != 0 && !earlyOut)
@@ -219,7 +228,7 @@ void VirtualMachine::Interpret()
219228
{
220229
//insert
221230
bool standalone = true;
222-
if ((existingNextPtr != m_FirstSegmentPtr) && (existingSegment + Unpack<uint32>(existingSegment) == segmentPtr))//Merge EXISTING+INSERTED
231+
if ((nextSegment != m_FirstSegmentPtr) && (existingSegment + Unpack<uint32>(existingSegment) == segmentPtr))//Merge EXISTING+INSERTED
223232
{
224233
//simply expand the existing segment to accomodate our size
225234
segmentPtr = existingSegment;
@@ -248,7 +257,13 @@ void VirtualMachine::Interpret()
248257
if (!earlyOut)
249258
{
250259
std::cerr << "[VM] Failed to free memory at " << segmentPtr << "; " << segmentSize << " bytes" << std::endl;
260+
return;
251261
}
262+
++m_ProgramCounter;
263+
264+
#ifdef VM_DEBUG_HEAP
265+
PrintHeap();
266+
#endif
252267
}
253268
continue;
254269

@@ -464,3 +479,18 @@ void VirtualMachine::Pack(uint32 address, T value)
464479
m_RAM[address+3] = n & 0xFF;
465480
#endif
466481
}
482+
483+
void VirtualMachine::PrintHeap(bool baseOffset)
484+
{
485+
uint32 offset = baseOffset ? m_HeapBase : 0;
486+
uint32 nextSegment = Unpack<uint32>(m_FirstSegmentPtr);
487+
488+
std::cout << "[DBG Heap]: "<< m_FirstSegmentPtr-offset << " first: " << nextSegment-offset << " \t";
489+
while (nextSegment != 0)
490+
{
491+
std::cout << "@" << nextSegment-offset << "{s: " << Unpack<uint32>(nextSegment);
492+
nextSegment = Unpack<uint32>(nextSegment + sizeof(uint32));
493+
std::cout << "; n: " << nextSegment-offset << "} ";
494+
}
495+
std::cout << std::endl;
496+
}

source/VirtualMachine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "AtomicTypes.h"
77

8+
#define VM_DEBUG_HEAP
9+
810
class VirtualMachine
911
{
1012
public:
@@ -29,6 +31,8 @@ class VirtualMachine
2931
template<typename T>
3032
void Pack(uint32 address, T value);
3133

34+
void PrintHeap(bool baseOffset = false);
35+
3236
private:
3337
//Static Sizes
3438
static const uint32 MAX_RAM = 536870912; //500 MB

0 commit comments

Comments
 (0)