OpenTTD Source 20241224-master-gf74b0cf984
ReusableBuffer< T > Class Template Reference

A reusable buffer that can be used for places that temporary allocate a bit of memory and do that very often, or for places where static memory is allocated that might need to be reallocated sometimes. More...

#include <alloc_type.hpp>

Public Member Functions

 ReusableBuffer ()
 Create a new buffer.
 
 ~ReusableBuffer ()
 Clear the buffer.
 
T * Allocate (size_t count)
 Get buffer of at least count times T.
 
T * ZeroAllocate (size_t count)
 Get buffer of at least count times T with zeroed memory.
 
const T * GetBuffer () const
 Get the currently allocated buffer.
 

Private Attributes

T * buffer
 The real data buffer.
 
size_t count
 Number of T elements in the buffer.
 

Detailed Description

template<typename T>
class ReusableBuffer< T >

A reusable buffer that can be used for places that temporary allocate a bit of memory and do that very often, or for places where static memory is allocated that might need to be reallocated sometimes.

Every time Allocate or ZeroAllocate is called previous results of both functions will become invalid.

Definition at line 24 of file alloc_type.hpp.

Constructor & Destructor Documentation

◆ ReusableBuffer()

template<typename T >
ReusableBuffer< T >::ReusableBuffer ( )
inline

Create a new buffer.

Definition at line 31 of file alloc_type.hpp.

◆ ~ReusableBuffer()

template<typename T >
ReusableBuffer< T >::~ReusableBuffer ( )
inline

Clear the buffer.

Definition at line 33 of file alloc_type.hpp.

References free().

Member Function Documentation

◆ Allocate()

template<typename T >
T * ReusableBuffer< T >::Allocate ( size_t  count)
inline

Get buffer of at least count times T.

Note
the buffer might be bigger
calling this function invalidates any previous buffers given
Parameters
countthe minimum buffer size
Returns
the buffer

Definition at line 42 of file alloc_type.hpp.

References ReusableBuffer< T >::buffer, ReusableBuffer< T >::count, and free().

Referenced by Blitter_8bppOptimized::Encode(), LoadNewGRFFileFromFile(), NetworkDrawChatMessage(), and OpenGLSprite::Update().

◆ GetBuffer()

template<typename T >
const T * ReusableBuffer< T >::GetBuffer ( ) const
inline

Get the currently allocated buffer.

Returns
the buffer

Definition at line 75 of file alloc_type.hpp.

References ReusableBuffer< T >::buffer.

Referenced by NetworkUndrawChatMessage(), and OpenGLSprite::Update().

◆ ZeroAllocate()

template<typename T >
T * ReusableBuffer< T >::ZeroAllocate ( size_t  count)
inline

Get buffer of at least count times T with zeroed memory.

Note
the buffer might be bigger
calling this function invalidates any previous buffers given
Parameters
countthe minimum buffer size
Returns
the buffer

Definition at line 59 of file alloc_type.hpp.

References ReusableBuffer< T >::buffer, ReusableBuffer< T >::count, and free().

Field Documentation

◆ buffer

template<typename T >
T* ReusableBuffer< T >::buffer
private

The real data buffer.

Definition at line 26 of file alloc_type.hpp.

Referenced by ReusableBuffer< T >::Allocate(), ReusableBuffer< T >::GetBuffer(), and ReusableBuffer< T >::ZeroAllocate().

◆ count

template<typename T >
size_t ReusableBuffer< T >::count
private

Number of T elements in the buffer.

Definition at line 27 of file alloc_type.hpp.

Referenced by ReusableBuffer< T >::Allocate(), and ReusableBuffer< T >::ZeroAllocate().


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