openMSX
Public Member Functions | List of all members
openmsx::OutputBuffer Class Reference

Memory output buffer. More...

#include <SerializeBuffer.hh>

Public Member Functions

 OutputBuffer ()
 Create an empty output buffer. More...
 
void insert (const void *data, size_t len)
 Insert data at the end of this buffer. More...
 
void insertN (const void *data, size_t len)
 
void insertAt (size_t pos, const void *data, size_t len)
 Insert data at a given position. More...
 
byteallocate (size_t len)
 Reserve space to insert the given number of bytes. More...
 
void deallocate (byte *pos)
 Free part of a previously allocated buffer. More...
 
size_t getPosition () const
 Get the current size of the buffer. More...
 
MemBuffer< byterelease (size_t &size)
 Release ownership of the buffer. More...
 

Detailed Description

Memory output buffer.

Acts as a replacement for std::vector<byte>. You can insert data in the buffer and the buffer will automatically grow. Like std::vector it manages an internal memory buffer that will automatically reallocate and grow exponentially.

This class is much less general than std::vector and optimized for the case of lots of (small) inserts at the end of the buffer (the main use case of in-memory savestates). This makes it more efficient than std::vector. std::vector is far from inefficient, but for savestates this is used A LOT, so even small improvements matter a lot.

Definition at line 25 of file SerializeBuffer.hh.

Constructor & Destructor Documentation

◆ OutputBuffer()

openmsx::OutputBuffer::OutputBuffer ( )

Create an empty output buffer.

Definition at line 12 of file SerializeBuffer.cc.

References data(), insertN(), and likely.

Member Function Documentation

◆ allocate()

byte* openmsx::OutputBuffer::allocate ( size_t  len)
inline

Reserve space to insert the given number of bytes.

The returned pointer is only valid until the next internal reallocate, so till the next call to insert() or allocate().

If you don't know yet exactly how much memory to allocate (e.g. when the buffer will be used for gzip output data), you can request the maximum size and deallocate the unused space later.

Definition at line 75 of file SerializeBuffer.hh.

References openmsx::MemBuffer< T, ALIGNMENT >::data(), and gl::max().

◆ deallocate()

void openmsx::OutputBuffer::deallocate ( byte pos)
inline

Free part of a previously allocated buffer.

The parameter must point right after the last byte of the used portion of the buffer. So it must be in the range [buf, buf+num] with buf and num respectively the return value and the parameter of the last allocate() call.

See comment in allocate(). This call must be done right after the allocate() call, there cannot be any other (non-const) call to this object in between.

Definition at line 102 of file SerializeBuffer.hh.

References openmsx::MemBuffer< T, ALIGNMENT >::data().

◆ getPosition()

size_t openmsx::OutputBuffer::getPosition ( ) const
inline

Get the current size of the buffer.

Definition at line 111 of file SerializeBuffer.hh.

References openmsx::MemBuffer< T, ALIGNMENT >::data(), release(), and size().

◆ insert()

void openmsx::OutputBuffer::insert ( const void *  data,
size_t  len 
)
inline

Insert data at the end of this buffer.

This will automatically grow this buffer.

Definition at line 35 of file SerializeBuffer.hh.

References data(), and insertN().

◆ insertAt()

void openmsx::OutputBuffer::insertAt ( size_t  pos,
const void *  data,
size_t  len 
)
inline

Insert data at a given position.

This will overwrite the old data. It's not possible to grow the buffer via this method (so the buffer must already be big enough to hold the new data).

Definition at line 61 of file SerializeBuffer.hh.

References openmsx::MemBuffer< T, ALIGNMENT >::data(), and data().

◆ insertN()

void openmsx::OutputBuffer::insertN ( const void *  data,
size_t  len 
)

Definition at line 51 of file SerializeBuffer.cc.

References likely.

Referenced by insert(), and OutputBuffer().

◆ release()

MemBuffer< byte > openmsx::OutputBuffer::release ( size_t &  size)

Release ownership of the buffer.

Returns both the buffer and its size.

Definition at line 62 of file SerializeBuffer.cc.

References openmsx::MemBuffer< T, ALIGNMENT >::data(), gl::max(), and openmsx::MemBuffer< T, ALIGNMENT >::resize().

Referenced by getPosition().


The documentation for this class was generated from the following files: