src/dataBuffer.cpp

changeset 134
eca2fc0acaa2
parent 133
dbbdb870c835
child 135
8b9132fea327
--- a/src/dataBuffer.cpp	Sat Jul 12 23:04:46 2014 +0300
+++ b/src/dataBuffer.cpp	Sun Jul 20 17:25:36 2014 +0300
@@ -28,16 +28,14 @@
 
 #include "dataBuffer.h"
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
 //
-DataBuffer::DataBuffer (int size)
-{
-	setBuffer (new char[size]);
-	setPosition (&buffer()[0]);
-	setAllocatedSize (size);
-}
+DataBuffer::DataBuffer (int size) :
+	m_buffer (new char[size]),
+	m_position (&buffer()[0]),
+	m_allocatedSize (size) {}
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
 //
 DataBuffer::~DataBuffer()
 {
@@ -46,7 +44,10 @@
 	delete buffer();
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Copies the contents of the given buffer into this buffer. The other buffer's marks and 
+//	references will be moved along and the buffer is then destroyed.
 //
 void DataBuffer::mergeAndDestroy (DataBuffer* other)
 {
@@ -61,10 +62,10 @@
 	delete other;
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
 //
-// Clones this databuffer to a new one and returns it. Note that the original
-// transfers its marks and references and loses them in the process.
+//	Clones this databuffer to a new one and returns it. Note that the original transfers its marks
+//	and references and loses them in the process.
 //
 DataBuffer* DataBuffer::clone()
 {
@@ -105,7 +106,9 @@
 	m_references.clear();
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Adds a new mark to the current position with the given name
 //
 ByteMark* DataBuffer::addMark (const String& name)
 {
@@ -116,7 +119,10 @@
 	return mark;
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Adds a new reference to the given mark at the current position. This function will write 4 
+//	bytes to the buffer whose value will be determined at final output writing.
 //
 MarkReference* DataBuffer::addReference (ByteMark* mark)
 {
@@ -131,29 +137,38 @@
 	return ref;
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Moves the given mark to the current bytecode position.
 //
 void DataBuffer::adjustMark (ByteMark* mark)
 {
 	mark->pos = writtenSize();
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Shifts the given mark by the amount of bytes
 //
-void DataBuffer::offsetMark (ByteMark* mark, int position)
+void DataBuffer::offsetMark (ByteMark* mark, int bytes)
 {
-	mark->pos += position;
+	mark->pos += bytes;
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Writes a push of the index of the given string. 8 bytes will be written and the string index
+//	will be pushed to stack.
 //
 void DataBuffer::writeStringIndex (const String& a)
 {
-	writeDWord (DH_PushStringIndex);
+	writeDWord (DataHeader::PushStringIndex);
 	writeDWord (getStringTableIndex (a));
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Prints the buffer to stdout.
 //
 void DataBuffer::dump()
 {
@@ -161,7 +176,10 @@
 		printf ("%d. [0x%X]\n", i, buffer()[i]);
 }
 
-// ============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+// Ensures there's at least the given amount of bytes left in the buffer. Will resize if necessary,
+// no-op if not. On resize, 512 extra bytes are allocated to reduce the amount of resizes.
 //
 void DataBuffer::checkSpace (int bytes)
 {
@@ -191,7 +209,9 @@
 	delete copy;
 }
 
-// =============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Writes the given byte into the buffer.
 //
 void DataBuffer::writeByte (int8_t data)
 {
@@ -199,7 +219,9 @@
 	*m_position++ = data;
 }
 
-// =============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Writes the given word into the buffer. 2 bytes will be written.
 //
 void DataBuffer::writeWord (int16_t data)
 {
@@ -209,7 +231,9 @@
 		*m_position++ = (data >> (i * 8)) & 0xFF;
 }
 
-// =============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Writes the given dword into the buffer. 4bytes will be written.
 //
 void DataBuffer::writeDWord (int32_t data)
 {
@@ -219,7 +243,10 @@
 		*m_position++ = (data >> (i * 8)) & 0xFF;
 }
 
-// =============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Writes the given string to the databuffer. The string will be written as-is without using string
+//	indices. This will write 4 + length bytes. No header will be written.
 //
 void DataBuffer::writeString (const String& a)
 {
@@ -230,14 +257,17 @@
 		writeByte (c);
 }
 
-
-// =============================================================================
+// -------------------------------------------------------------------------------------------------
+//
+//	Tries to locate the mark by the given name. Returns null if not found.
 //
 ByteMark* DataBuffer::findMarkByName (const String& name)
 {
 	for (ByteMark* mark : marks())
+	{
 		if (mark->name == name)
 			return mark;
+	}
 
 	return null;
 }

mercurial