# CBlobBaseSimple Class Reference

Base class for simple binary blobs. More...

`#include <blob.hpp>`

Inheritance diagram for CBlobBaseSimple:

## Public Types

typedef ::ptrdiff_t | bsize_t
typedef ::byte | bitem_t

## Public Member Functions

FORCEINLINE | CBlobBaseSimple ()
| default constructor - initializes empty blob
FORCEINLINE | CBlobBaseSimple (const bitem_t *p, bsize_t num_bytes)
| constructor - create blob with data
FORCEINLINE | CBlobBaseSimple (const CBlobBaseSimple &src)
| copy constructor
FORCEINLINE | CBlobBaseSimple (CHdr *const &pHdr_1)
| move constructor - take ownership of blob data
FORCEINLINE | ~CBlobBaseSimple ()
| destructor
FORCEINLINE bool | IsEmpty () const
| return true if blob doesn't contain valid data
FORCEINLINE bsize_t | RawSize () const
| return the number of valid data bytes in the blob
FORCEINLINE bsize_t | MaxRawSize () const
| return the current blob capacity in bytes
FORCEINLINE bitem_t * | RawData ()
| return pointer to the first byte of data - non-const version
FORCEINLINE const bitem_t * | RawData () const
| return pointer to the first byte of data - const version
FORCEINLINE void | Clear ()
| return the 32 bit CRC of valid data in the blob
FORCEINLINE void | Free ()
| free the blob's memory
FORCEINLINE void | CopyFrom (const CBlobBaseSimple &src)
| copy data from another blob - replaces any existing blob's data
FORCEINLINE void | MoveFrom (CBlobBaseSimple &src)
| overtake ownership of data buffer from the source blob - source blob will become empty
FORCEINLINE void | Swap (CBlobBaseSimple &src)
| swap buffers (with data) between two blobs (this and source blob)
FORCEINLINE void | AppendRaw (const void *p, bsize_t num_bytes)
| append new bytes at the end of existing data bytes - reallocates if necessary
FORCEINLINE void | AppendRaw (const CBlobBaseSimple &src)
| append bytes from given source blob to the end of existing data bytes - reallocates if necessary
FORCEINLINE bitem_t * | MakeRawFreeSpace (bsize_t num_bytes)
| Reallocate if there is no free space for num_bytes bytes.
FORCEINLINE bitem_t * | GrowRawSize (bsize_t num_bytes)
| Increase RawSize() by num_bytes.
FORCEINLINE void | ReduceRawSize (bsize_t num_bytes)
| Decrease RawSize() by num_bytes.
void | SmartAlloc (bsize_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 Member Functions

static FORCEINLINE bsize_t | AllocPolicy (bsize_t min_alloc)
| simple allocation policy - can be optimized later
static FORCEINLINE CHdr * | RawAlloc (bsize_t num_bytes)
| all allocation should happen here
static FORCEINLINE void | RawFree (CHdr *p)
| all deallocations should happen here

## Static Public Attributes

static const bsize_t | Ttail_reserve = 4
| four extra bytes will be always allocated and zeroed at the end

## Protected Member Functions

FORCEINLINE void | InitEmpty ()
| initialize the empty blob by setting the ptr_u.m_pHdr_1 pointer to the static CHdr with both m_size and m_max_size containing zero
FORCEINLINE void | Init (CHdr *hdr)
| initialize blob by attaching it to the given header followed by data
FORCEINLINE CHdr & | Hdr ()
| blob header accessor - use it rather than using the pointer arithmetics directly - non-const version
FORCEINLINE const CHdr & | Hdr () const
| blob header accessor - use it rather than using the pointer arithmetics directly - const version
FORCEINLINE bsize_t & | RawSizeRef ()
| return reference to the actual blob size - used when the size needs to be modified

## Protected Attributes

union {
bitem_t * m_pData
| ptr to the first byte of data
CHdr * m_pHdr_1
| ptr just after the CHdr holding m_size and m_max_size
} | ptr_u
| type used as class member

## Data Structures

struct | CHdr
| header of the allocated memory block More...

## Detailed Description

Base class for simple binary blobs.

Item is byte. The word 'simple' means:

- no configurable allocator type (always made from heap)
- no smart deallocation - deallocation must be called from the same module (DLL) where the blob was allocated
- no configurable allocation policy (how big blocks should be allocated)
- no extra ownership policy (i.e. 'copy on write') when blob is copied
- no thread synchronization at all

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

- items are accessed in the simplest possible way - just dereferencing the pointer, which is good for performance (assuming that data are accessed most often).
- sizeof(blob) is the same as the size of any other pointer 6. Drawbacks of this layout:
- the fact, that pointer to the alocated block is adjusted by sizeof(CHdr) before it is stored can lead to several confusions:
  - it is not common pattern so the implementation code is bit harder to read
  - valgrind can generate warning that allocated block is lost (not accessible)

Definition at line 40 of file blob.hpp.

## Member Function Documentation

FORCEINLINE void CBlobBaseSimple::Clear | ( |  | ) | `[inline]`

return the 32 bit CRC of valid data in the blob

invalidate blob's data - doesn't free buffer

Definition at line 165 of file blob.hpp.

References RawSizeRef().

Referenced by CopyFrom(), and CStrT< char, false >::operator=().

FORCEINLINE bitem_t* CBlobBaseSimple::MakeRawFreeSpace | ( | bsize_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 221 of file blob.hpp.

References MaxRawSize(), ptr_u, RawSize(), and SmartAlloc().

Referenced by GrowRawSize().

FORCEINLINE bitem_t* CBlobBaseSimple::GrowRawSize | ( | bsize_t | num_bytes | ) | `[inline]`

Increase RawSize() by num_bytes.

Returns:
  pointer to the new data added

Definition at line 231 of file blob.hpp.

References MakeRawFreeSpace(), and RawSizeRef().

Referenced by AppendRaw().

FORCEINLINE void CBlobBaseSimple::ReduceRawSize | ( | bsize_t | num_bytes | ) | `[inline]`

Decrease RawSize() by num_bytes.

Definition at line 239 of file blob.hpp.

References MaxRawSize(), RawSize(), and RawSizeRef().

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

- src/misc/blob.hpp