ByteBlob Class Reference

Base class for simple binary blobs. More...

#include <blob.hpp>

Inheritance diagram for ByteBlob:
CBlobT< T > CBlobT< char > CStrA

Data Structures

struct  BlobHeader
 header of the allocated memory block More...

Public Member Functions

FORCEINLINE ByteBlob ()
 default constructor - initializes empty blob
FORCEINLINE ByteBlob (const ByteBlob &src)
 copy constructor
FORCEINLINE ByteBlob (BlobHeader *const &src)
 move constructor - take ownership of blob data
FORCEINLINE ~ByteBlob ()
 destructor
FORCEINLINE bool IsEmpty () const
 return true if blob doesn't contain valid data
FORCEINLINE size_t Length () const
 return the number of valid data bytes in the blob
FORCEINLINE size_t Capacity () const
 return the current blob capacity in bytes
FORCEINLINE byte * Begin ()
 return pointer to the first byte of data - non-const version
FORCEINLINE const byte * Begin () const
 return pointer to the first byte of data - const version
FORCEINLINE void Clear ()
 invalidate blob's data - doesn't free buffer
FORCEINLINE void Free ()
 free the blob's memory
FORCEINLINE void AppendRaw (const void *p, size_t num_bytes)
 append new bytes at the end of existing data bytes - reallocates if necessary
FORCEINLINE void AppendRaw (const ByteBlob &src)
 append bytes from given source blob to the end of existing data bytes - reallocates if necessary
FORCEINLINE byte * Prepare (size_t num_bytes)
 Reallocate if there is no free space for num_bytes bytes.
FORCEINLINE byte * Append (size_t num_bytes)
 Increase Length() by num_bytes.
void SmartAlloc (size_t new_size)
 reallocate blob data if needed
FORCEINLINE void FixTail () const
 fixing the four bytes at the end of blob data - useful when blob is used to hold string

Static Public Attributes

static const size_t tail_reserve = 4
 four extra bytes will be always allocated and zeroed at the end
static const size_t header_size = sizeof(BlobHeader)

Protected Member Functions

FORCEINLINE void InitEmpty ()
 initialize the empty blob
FORCEINLINE void Init (BlobHeader *src)
 initialize blob by attaching it to the given header followed by data
FORCEINLINE BlobHeaderHdr ()
 blob header accessor - use it rather than using the pointer arithmetics directly - non-const version
FORCEINLINE const BlobHeaderHdr () const
 blob header accessor - use it rather than using the pointer arithmetics directly - const version
FORCEINLINE size_t & LengthRef ()
 return reference to the actual blob size - used when the size needs to be modified

Static Protected Member Functions

static FORCEINLINE BlobHeaderRawAlloc (size_t num_bytes)
 all allocation should happen here
static FORCEINLINE BlobHeaderZero ()
 Return header pointer to the static BlobHeader with both items and capacity containing zero.
static FORCEINLINE size_t AllocPolicy (size_t min_alloc)
 simple allocation policy - can be optimized later
static FORCEINLINE void RawFree (BlobHeader *p)
 all deallocations should happen here

Protected Attributes

union {
   byte *   data
 ptr to the first byte of data
   BlobHeader *   header
 ptr just after the BlobHeader holding items and capacity
}; 
 type used as class member

Static Private Attributes

static BlobHeader hdrEmpty [] = {{0, 0}, {0, 0}}
 Just to silence an unsilencable GCC 4.4+ warning Note: This cannot be 'const' as we do a lot of 'hdrEmpty[0]->items += 0;' and 'hdrEmpty[0]->capacity += 0;' after const_casting.

Detailed Description

Base class for simple binary blobs.

Item is byte. The word 'simple' means:

Internal member layout: 1. The only class member is pointer to the first item (see union). 2. Allocated block contains the blob header (see BlobHeader) followed by the raw byte data. Always, when it allocates memory the allocated size is: sizeof(BlobHeader) + <data capacity>=""> 3. Two 'virtual' members (items and capacity) are stored in the BlobHeader at beginning of the alloated block. 4. The pointer of the union pobsize_ts behind the header (to the first data byte). When memory block is allocated, the sizeof(BlobHeader) it added to it. 5. Benefits of this layout:

Definition at line 47 of file blob.hpp.


Member Function Documentation

FORCEINLINE byte* ByteBlob::Append ( size_t  num_bytes  )  [inline]

Increase Length() by num_bytes.

Returns:
pointer to the new data added

Definition at line 250 of file blob.hpp.

References LengthRef(), and Prepare().

Referenced by AppendRaw(), and CBlobT< char >::GrowSizeNC().

FORCEINLINE byte* ByteBlob::Prepare ( size_t  num_bytes  )  [inline]

Reallocate if there is no free space for num_bytes bytes.

Returns:
pointer to the new data to be added

Definition at line 239 of file blob.hpp.

References Capacity(), data, Length(), and SmartAlloc().

Referenced by Append(), and CBlobT< char >::MakeFreeSpace().


Field Documentation

ByteBlob::BlobHeader ByteBlob::hdrEmpty = {{0, 0}, {0, 0}} [static, private]

Just to silence an unsilencable GCC 4.4+ warning Note: This cannot be 'const' as we do a lot of 'hdrEmpty[0]->items += 0;' and 'hdrEmpty[0]->capacity += 0;' after const_casting.

Just to silence an unsilencable GCC 4.4+ warning.

Definition at line 67 of file blob.hpp.

Referenced by RawFree(), and Zero().


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

Generated on Fri May 27 04:20:33 2011 for OpenTTD by  doxygen 1.6.1