|
|
/* $Id$ */
|
|
|
|
|
|
#ifndef BLOB_HPP
|
|
|
#define BLOB_HPP
|
|
|
|
|
|
template <class Titem_>
|
|
|
FORCEINLINE void MemCpyT(Titem_* d, const Titem_* s, int num_items = 1)
|
|
|
{
|
|
|
memcpy(d, s, num_items * sizeof(Titem_));
|
|
|
}
|
|
|
|
|
|
|
|
|
/** 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 */
|
|
|
* 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 */
|
|
|
class CBlobBaseSimple {
|
|
|
protected:
|
|
|
struct CHdr {
|
|
|
int m_size; // actual blob size in bytes
|
|
|
int m_max_size; // maximum (allocated) size in bytes
|
|
|
};
|
|
|
|
|
|
union {
|
|
|
int8 *m_pData;
|
|
|
CHdr *m_pHdr_1;
|
|
|
} ptr_u;
|
|
|
|
|
|
public:
|
|
|
ST_CONST(int, Ttail_reserve = 4); // four extra bytes will be always allocated and zeroed at the end
|
|
|
|
|
|
FORCEINLINE CBlobBaseSimple() { InitEmpty(); }
|
|
|
FORCEINLINE CBlobBaseSimple(const CBlobBaseSimple& src)
|
|
|
{
|
|
|
InitEmpty();
|
|
|
AppendRaw(src);
|
|
|
}
|
|
|
FORCEINLINE ~CBlobBaseSimple() { Free(); }
|
|
|
protected:
|
|
|
FORCEINLINE void InitEmpty() { static CHdr hdrEmpty[] = {{0, 0}, {0, 0}}; ptr_u.m_pHdr_1 = &hdrEmpty[1]; }
|
|
@@ -57,60 +57,60 @@ public:
|
|
|
FORCEINLINE uint32 Crc32() const {return CCrc32::Calc(RawData(), RawSize());}
|
|
|
FORCEINLINE void Clear() { RawSizeRef() = 0; }
|
|
|
FORCEINLINE void Free() { if (MaxRawSize() > 0) {RawFree(&Hdr()); InitEmpty();} }
|
|
|
FORCEINLINE void CopyFrom(const CBlobBaseSimple& src) { Clear(); AppendRaw(src); }
|
|
|
FORCEINLINE void MoveFrom(CBlobBaseSimple& src) { Free(); ptr_u.m_pData = src.ptr_u.m_pData; src.InitEmpty(); }
|
|
|
FORCEINLINE void Swap(CBlobBaseSimple& src) { int8 *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData; src.ptr_u.m_pData = tmp; }
|
|
|
|
|
|
FORCEINLINE void AppendRaw(int8 *p, int num_bytes)
|
|
|
{
|
|
|
assert(p != NULL);
|
|
|
if (num_bytes > 0) {
|
|
|
memcpy(GrowRawSize(num_bytes), p, num_bytes);
|
|
|
} else {
|
|
|
assert(num_bytes >= 0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
FORCEINLINE void AppendRaw(const CBlobBaseSimple& src)
|
|
|
{
|
|
|
if (!src.IsEmpty())
|
|
|
memcpy(GrowRawSize(src.RawSize()), src.RawData(), src.RawSize());
|
|
|
}
|
|
|
|
|
|
/** Reallocate if there is no free space for num_bytes bytes.
|
|
|
@return pointer to the new data to be added */
|
|
|
* @return pointer to the new data to be added */
|
|
|
FORCEINLINE int8* MakeRawFreeSpace(int num_bytes)
|
|
|
{
|
|
|
assert(num_bytes >= 0);
|
|
|
int new_size = RawSize() + num_bytes;
|
|
|
if (new_size > MaxRawSize()) SmartAlloc(new_size);
|
|
|
FixTail();
|
|
|
return ptr_u.m_pData + RawSize();
|
|
|
}
|
|
|
|
|
|
/** Increase RawSize() by num_bytes.
|
|
|
@return pointer to the new data added */
|
|
|
* @return pointer to the new data added */
|
|
|
FORCEINLINE int8* GrowRawSize(int num_bytes)
|
|
|
{
|
|
|
int8* pNewData = MakeRawFreeSpace(num_bytes);
|
|
|
RawSizeRef() += num_bytes;
|
|
|
return pNewData;
|
|
|
}
|
|
|
|
|
|
/** Decrease RawSize() by num_bytes. */
|
|
|
FORCEINLINE void ReduceRawSize(int num_bytes)
|
|
|
{
|
|
|
if (MaxRawSize() > 0 && num_bytes > 0) {
|
|
|
assert(num_bytes <= RawSize());
|
|
|
if (num_bytes < RawSize()) RawSizeRef() -= num_bytes;
|
|
|
else RawSizeRef() = 0;
|
|
|
}
|
|
|
}
|
|
|
/** reallocate blob data if needed */
|
|
|
void SmartAlloc(int new_size)
|
|
|
{
|
|
|
int old_max_size = MaxRawSize();
|
|
|
if (old_max_size >= new_size) return;
|
|
|
// calculate minimum block size we need to allocate
|
|
|
int min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve;
|
|
|
// ask allocation policy for some reasonable block size
|