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.
 
void insert (const void *data, size_t len)
 Insert data at the end of this buffer.
 
void insertN (const void *data, size_t len)
 
template<typename TUPLE >
ALWAYS_INLINE void insert_tuple_ptr (const TUPLE &tuple)
 Insert all the elements of the given tuple.
 
template<typename T >
ALWAYS_INLINE void insert_tuple_ptr (const std::tuple< T * > &tuple)
 
void insertAt (size_t pos, const void *data, size_t len)
 Insert data at a given position.
 
std::span< uint8_t > allocate (size_t len)
 Reserve space to insert the given number of bytes.
 
size_t getPosition () const
 Free part of a previously allocated buffer.
 
MemBuffer< uint8_t > release (size_t &size)
 Release ownership of the buffer.
 

Detailed Description

Memory output buffer.

Acts as a replacement for std::vector<uint8_t>. 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 28 of file SerializeBuffer.hh.

Constructor & Destructor Documentation

◆ OutputBuffer()

openmsx::OutputBuffer::OutputBuffer ( )

Create an empty output buffer.

Definition at line 9 of file SerializeBuffer.cc.

Member Function Documentation

◆ allocate()

std::span< uint8_t > 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 110 of file SerializeBuffer.hh.

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

Referenced by openmsx::MemOutputArchive::save(), and openmsx::MemOutputArchive::serialize_blob().

◆ getPosition()

size_t openmsx::OutputBuffer::getPosition ( ) const
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. Get the current size of the buffer.

Definition at line 146 of file SerializeBuffer.hh.

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

Referenced by openmsx::MemOutputArchive::beginSection(), and openmsx::MemOutputArchive::endSection().

◆ 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 38 of file SerializeBuffer.hh.

References insertN().

Referenced by insert_tuple_ptr(), and openmsx::MemOutputArchive::serialize().

◆ insert_tuple_ptr() [1/2]

template<typename T >
ALWAYS_INLINE void openmsx::OutputBuffer::insert_tuple_ptr ( const std::tuple< T * > &  tuple)
inline

Definition at line 86 of file SerializeBuffer.hh.

References insert().

◆ insert_tuple_ptr() [2/2]

template<typename TUPLE >
ALWAYS_INLINE void openmsx::OutputBuffer::insert_tuple_ptr ( const TUPLE &  tuple)
inline

Insert all the elements of the given tuple.

Equivalent to repeatedly calling insert() for all the elements of the tuple. Though using this method the implementation only has to check once whether enough memory is allocated.

Definition at line 65 of file SerializeBuffer.hh.

◆ 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 96 of file SerializeBuffer.hh.

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

Referenced by openmsx::MemOutputArchive::endSection().

◆ insertN()

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

Definition at line 48 of file SerializeBuffer.cc.

Referenced by insert().

◆ release()

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

Release ownership of the buffer.

Returns both the buffer and its size.

Definition at line 59 of file SerializeBuffer.cc.

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

Referenced by openmsx::MemOutputArchive::releaseBuffer().


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