|
@@ -13,13 +13,14 @@
|
|
|
#define BLOB_HPP
|
|
|
|
|
|
#include "../core/alloc_func.hpp"
|
|
|
#include "../core/mem_func.hpp"
|
|
|
#include <new>
|
|
|
|
|
|
/** Base class for simple binary blobs.
|
|
|
/**
|
|
|
* 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)
|
|
@@ -99,13 +100,14 @@ protected:
|
|
|
/** all allocation should happen here */
|
|
|
static FORCEINLINE BlobHeader *RawAlloc(size_t num_bytes)
|
|
|
{
|
|
|
return (BlobHeader*)MallocT<byte>(num_bytes);
|
|
|
}
|
|
|
|
|
|
/** Return header pointer to the static BlobHeader with
|
|
|
/**
|
|
|
* Return header pointer to the static BlobHeader with
|
|
|
* both items and capacity containing zero */
|
|
|
static FORCEINLINE BlobHeader *Zero()
|
|
|
{
|
|
|
return const_cast<BlobHeader *>(&ByteBlob::hdrEmpty[1]);
|
|
|
}
|
|
|
|
|
@@ -228,22 +230,24 @@ public:
|
|
|
{
|
|
|
if (!src.IsEmpty()) {
|
|
|
memcpy(Append(src.Length()), src.Begin(), src.Length());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/** Reallocate if there is no free space for num_bytes bytes.
|
|
|
/**
|
|
|
* Reallocate if there is no free space for num_bytes bytes.
|
|
|
* @return pointer to the new data to be added */
|
|
|
FORCEINLINE byte *Prepare(size_t num_bytes)
|
|
|
{
|
|
|
size_t new_size = Length() + num_bytes;
|
|
|
if (new_size > Capacity()) SmartAlloc(new_size);
|
|
|
return data + Length();
|
|
|
}
|
|
|
|
|
|
/** Increase Length() by num_bytes.
|
|
|
/**
|
|
|
* Increase Length() by num_bytes.
|
|
|
* @return pointer to the new data added */
|
|
|
FORCEINLINE byte *Append(size_t num_bytes)
|
|
|
{
|
|
|
byte *pNewData = Prepare(num_bytes);
|
|
|
LengthRef() += num_bytes;
|
|
|
return pNewData;
|
|
@@ -283,13 +287,14 @@ public:
|
|
|
p[i] = 0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/** Blob - simple dynamic T array. T (template argument) is a placeholder for any type.
|
|
|
/**
|
|
|
* Blob - simple dynamic T array. T (template argument) is a placeholder for any type.
|
|
|
* T can be any integral type, pointer, or structure. Using Blob instead of just plain C array
|
|
|
* simplifies the resource management in several ways:
|
|
|
* 1. When adding new item(s) it automatically grows capacity if needed.
|
|
|
* 2. When variable of type Blob comes out of scope it automatically frees the data buffer.
|
|
|
* 3. Takes care about the actual data size (number of used items).
|
|
|
* 4. Dynamically constructs only used items (as opposite of static array which constructs all items) */
|
|
@@ -377,13 +382,14 @@ public:
|
|
|
/** Grow number of data items in Blob by given number - doesn't construct items */
|
|
|
FORCEINLINE T *GrowSizeNC(size_t num_items)
|
|
|
{
|
|
|
return (T*)base::Append(num_items * type_size);
|
|
|
}
|
|
|
|
|
|
/** Ensures that given number of items can be added to the end of Blob. Returns pointer to the
|
|
|
/**
|
|
|
* Ensures that given number of items can be added to the end of Blob. Returns pointer to the
|
|
|
* first free (unused) item */
|
|
|
FORCEINLINE T *MakeFreeSpace(size_t num_items)
|
|
|
{
|
|
|
return (T*)base::Prepare(num_items * type_size);
|
|
|
}
|
|
|
|