src/dataBuffer.cpp

changeset 135
8b9132fea327
parent 134
eca2fc0acaa2
child 138
a426c1039655
equal deleted inserted replaced
134:eca2fc0acaa2 135:8b9132fea327
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <cstring>
29 #include "dataBuffer.h" 30 #include "dataBuffer.h"
30 31
31 // ------------------------------------------------------------------------------------------------- 32 // -------------------------------------------------------------------------------------------------
32 // 33 //
33 DataBuffer::DataBuffer (int size) : 34 DataBuffer::DataBuffer (int size) :
34 m_buffer (new char[size]), 35 m_buffer (new char[size]),
35 m_position (&buffer()[0]), 36 m_allocatedSize (size),
36 m_allocatedSize (size) {} 37 m_position (&buffer()[0]) {}
37 38
38 // ------------------------------------------------------------------------------------------------- 39 // -------------------------------------------------------------------------------------------------
39 // 40 //
40 DataBuffer::~DataBuffer() 41 DataBuffer::~DataBuffer()
41 { 42 {
160 // Writes a push of the index of the given string. 8 bytes will be written and the string index 161 // Writes a push of the index of the given string. 8 bytes will be written and the string index
161 // will be pushed to stack. 162 // will be pushed to stack.
162 // 163 //
163 void DataBuffer::writeStringIndex (const String& a) 164 void DataBuffer::writeStringIndex (const String& a)
164 { 165 {
165 writeDWord (DataHeader::PushStringIndex); 166 writeHeader (DataHeader::PushStringIndex);
166 writeDWord (getStringTableIndex (a)); 167 writeDWord (getStringTableIndex (a));
168 }
169
170 // -------------------------------------------------------------------------------------------------
171 //
172 // Writes a data header. 4 bytes.
173 //
174 void DataBuffer::writeHeader (DataHeader data)
175 {
176 writeDWord (static_cast<int32_t> (data));
167 } 177 }
168 178
169 // ------------------------------------------------------------------------------------------------- 179 // -------------------------------------------------------------------------------------------------
170 // 180 //
171 // Prints the buffer to stdout. 181 // Prints the buffer to stdout.
190 200
191 // We don't have enough space in the buffer to write 201 // We don't have enough space in the buffer to write
192 // the stuff - thus resize. First, store the old 202 // the stuff - thus resize. First, store the old
193 // buffer temporarily: 203 // buffer temporarily:
194 char* copy = new char[allocatedSize()]; 204 char* copy = new char[allocatedSize()];
195 memcpy (copy, buffer(), allocatedSize()); 205 std::memcpy (copy, buffer(), allocatedSize());
196 206
197 // Remake the buffer with the new size. Have enough space 207 // Remake the buffer with the new size. Have enough space
198 // for the stuff we're going to write, as well as a bit 208 // for the stuff we're going to write, as well as a bit
199 // of leeway so we don't have to resize immediately again. 209 // of leeway so we don't have to resize immediately again.
200 int newsize = allocatedSize() + bytes + 512; 210 int newsize = allocatedSize() + bytes + 512;
202 delete buffer(); 212 delete buffer();
203 setBuffer (new char[newsize]); 213 setBuffer (new char[newsize]);
204 setAllocatedSize (newsize); 214 setAllocatedSize (newsize);
205 215
206 // Now, copy the stuff back. 216 // Now, copy the stuff back.
207 memcpy (m_buffer, copy, allocatedSize()); 217 std::memcpy (m_buffer, copy, allocatedSize());
208 setPosition (buffer() + writesize); 218 setPosition (buffer() + writesize);
209 delete copy; 219 delete copy;
210 } 220 }
211 221
212 // ------------------------------------------------------------------------------------------------- 222 // -------------------------------------------------------------------------------------------------

mercurial