openMSX
Public Member Functions | List of all members
openmsx::MemBuffer< T, ALIGNMENT > Class Template Reference

This class manages the lifetime of a block of memory. More...

#include <MemBuffer.hh>

Inheritance diagram for openmsx::MemBuffer< T, ALIGNMENT >:
Inheritance graph
[legend]

Public Member Functions

 MemBuffer ()
 Construct an empty MemBuffer, no memory is allocated. More...
 
 MemBuffer (size_t size)
 Construct a (uninitialized) memory buffer of given size. More...
 
 MemBuffer (T *data, size_t size)
 Take ownership of the given memory block. More...
 
 MemBuffer (MemBuffer &&other)
 Move constructor. More...
 
MemBufferoperator= (MemBuffer &&other)
 Move assignment. More...
 
 ~MemBuffer ()
 Free the memory buffer. More...
 
const T * data () const
 Returns pointer to the start of the memory buffer. More...
 
T * data ()
 
const T & operator[] (size_t i) const
 Access elements in the memory buffer. More...
 
T & operator[] (size_t i)
 
size_t size () const
 Returns size of the memory buffer. More...
 
bool empty () const
 Same as size() == 0. More...
 
void resize (size_t size)
 Grow or shrink the memory block. More...
 
void clear ()
 Free the allocated memory block and set the current size to 0. More...
 
const T * begin () const
 STL-style iterators. More...
 
T * begin ()
 
const T * end () const
 
T * end ()
 
void swap (MemBuffer &other)
 Swap the managed memory block of two MemBuffers. More...
 

Detailed Description

template<typename T, size_t ALIGNMENT = 0>
class openmsx::MemBuffer< T, ALIGNMENT >

This class manages the lifetime of a block of memory.

Its two main use cases are: 1) As a safer alternative for new[] / delete[]. Using this class makes sure the memory block is always properly cleaned up. Also in case of exceptions. 2) As an alternative for vector<byte> ('byte' or other primitive types). The main difference with vector is that the allocated block is left uninitialized. This can be a bit more efficient if the block will anyway soon be overwritten.

Like vector this buffer can dynamically grow or shrink. But it's not optimized for this case (it doesn't keep track of extra capacity). If you need frequent resizing prefer to use vector instead of this class.

Definition at line 38 of file MemBuffer.hh.

Constructor & Destructor Documentation

template<typename T, size_t ALIGNMENT = 0>
openmsx::MemBuffer< T, ALIGNMENT >::MemBuffer ( )
inline

Construct an empty MemBuffer, no memory is allocated.

Definition at line 43 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
openmsx::MemBuffer< T, ALIGNMENT >::MemBuffer ( size_t  size)
inlineexplicit

Construct a (uninitialized) memory buffer of given size.

Definition at line 51 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
openmsx::MemBuffer< T, ALIGNMENT >::MemBuffer ( T *  data,
size_t  size 
)
inline

Take ownership of the given memory block.

This pointer should have been allocated earlier with malloc() or realloc() (or it should be nullptr).

Definition at line 61 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
openmsx::MemBuffer< T, ALIGNMENT >::MemBuffer ( MemBuffer< T, ALIGNMENT > &&  other)
inline

Move constructor.

Definition at line 69 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
openmsx::MemBuffer< T, ALIGNMENT >::~MemBuffer ( )
inline

Free the memory buffer.

Definition at line 86 of file MemBuffer.hh.

Member Function Documentation

template<typename T, size_t ALIGNMENT = 0>
const T* openmsx::MemBuffer< T, ALIGNMENT >::begin ( ) const
inline

STL-style iterators.

Definition at line 142 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
T* openmsx::MemBuffer< T, ALIGNMENT >::begin ( )
inline

Definition at line 143 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
void openmsx::MemBuffer< T, ALIGNMENT >::clear ( )
inline

Free the allocated memory block and set the current size to 0.

Definition at line 133 of file MemBuffer.hh.

Referenced by openmsx::FileBase::munmap().

template<typename T, size_t ALIGNMENT = 0>
const T* openmsx::MemBuffer< T, ALIGNMENT >::data ( ) const
inline
template<typename T, size_t ALIGNMENT = 0>
T* openmsx::MemBuffer< T, ALIGNMENT >::data ( )
inline

Definition at line 95 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
bool openmsx::MemBuffer< T, ALIGNMENT >::empty ( ) const
inline

Same as size() == 0.

Definition at line 117 of file MemBuffer.hh.

Referenced by openmsx::FileBase::mmap().

template<typename T, size_t ALIGNMENT = 0>
const T* openmsx::MemBuffer< T, ALIGNMENT >::end ( ) const
inline

Definition at line 144 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
T* openmsx::MemBuffer< T, ALIGNMENT >::end ( )
inline

Definition at line 145 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
MemBuffer& openmsx::MemBuffer< T, ALIGNMENT >::operator= ( MemBuffer< T, ALIGNMENT > &&  other)
inline

Move assignment.

Definition at line 77 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
const T& openmsx::MemBuffer< T, ALIGNMENT >::operator[] ( size_t  i) const
inline

Access elements in the memory buffer.

Definition at line 99 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
T& openmsx::MemBuffer< T, ALIGNMENT >::operator[] ( size_t  i)
inline

Definition at line 104 of file MemBuffer.hh.

template<typename T, size_t ALIGNMENT = 0>
void openmsx::MemBuffer< T, ALIGNMENT >::resize ( size_t  size)
inline

Grow or shrink the memory block.

In case of growing, the extra space is left uninitialized. It is possible (even likely) that the memory buffer is copied to a new location after this call, so data() will return a different pointer value.

Definition at line 125 of file MemBuffer.hh.

Referenced by openmsx::ZlibInflate::inflate(), openmsx::SDLGLOutputSurface::init(), openmsx::XMLLoader::load(), openmsx::SDLSoundDriver::SDLSoundDriver(), and openmsx::WavData::WavData().

template<typename T, size_t ALIGNMENT = 0>
size_t openmsx::MemBuffer< T, ALIGNMENT >::size ( ) const
inline

Returns size of the memory buffer.

The size is in number of elements, not number of allocated bytes.

Definition at line 113 of file MemBuffer.hh.

Referenced by openmsx::Ram::clear(), openmsx::Ram::getSize(), openmsx::ZlibInflate::inflate(), openmsx::CompressedFileAdapter::read(), openmsx::MemBuffer< const byte * >::resize(), openmsx::Ram::serialize(), and openmsx::SDLSoundDriver::uploadBuffer().

template<typename T, size_t ALIGNMENT = 0>
void openmsx::MemBuffer< T, ALIGNMENT >::swap ( MemBuffer< T, ALIGNMENT > &  other)
inline

Swap the managed memory block of two MemBuffers.

Definition at line 149 of file MemBuffer.hh.

Referenced by openmsx::XSAExtractor::getData().


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