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)
 
template<typename TUPLE >
ALWAYS_INLINE void insert_tuple_ptr (const TUPLE &tuple)
 Insert all the elements of the given tuple. More...
 
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. More...
 
uint8_t * allocate (size_t len)
 Reserve space to insert the given number of bytes. More...
 
void deallocate (uint8_t *pos)
 Free part of a previously allocated buffer. More...
 
size_t getPosition () const
 Get the current size of the buffer. More...
 
MemBuffer< uint8_t > release (size_t &size)
 Release ownership of the buffer. More...
 

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 10 of file SerializeBuffer.cc.

Member Function Documentation

◆ allocate()

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

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

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

◆ deallocate()

void openmsx::OutputBuffer::deallocate ( uint8_t *  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 138 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 147 of file SerializeBuffer.hh.

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

Referenced by 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 87 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.

References unlikely.

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

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

◆ insertN()

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

Definition at line 49 of file SerializeBuffer.cc.

References likely.

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 60 of file SerializeBuffer.cc.

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

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


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