15 #include "../core/alloc_func.hpp" 71 static const size_t header_size =
sizeof(
BlobHeader);
119 if (min_alloc < (1 << 9)) {
120 if (min_alloc < (1 << 5))
return (1 << 5);
121 return (min_alloc < (1 << 7)) ? (1 << 7) : (1 << 9);
123 if (min_alloc < (1 << 15)) {
124 if (min_alloc < (1 << 11))
return (1 << 11);
125 return (min_alloc < (1 << 13)) ? (1 << 13) : (1 << 15);
127 if (min_alloc < (1 << 20)) {
128 if (min_alloc < (1 << 17))
return (1 << 17);
129 return (min_alloc < (1 << 19)) ? (1 << 19) : (1 << 20);
131 min_alloc = (min_alloc | ((1 << 20) - 1)) + 1;
226 memcpy(
Append(num_bytes), p, num_bytes);
244 size_t new_size =
Length() + num_bytes;
255 byte *pNewData =
Prepare(num_bytes);
266 assert(new_size < SIZE_MAX - header_size - tail_reserve);
267 new_size =
AllocPolicy(header_size + new_size + tail_reserve);
275 if (tmp->
items != 0) {
307 template <
typename T>
313 static const size_t type_size =
sizeof(T);
320 assert(src.header != NULL);
331 assert(header == NULL);
354 assert(index < Size());
360 return (T*)base::Begin();
364 inline const T *Data()
const 366 return (
const T*)base::Begin();
370 inline T *Data(
size_t index)
373 return (Data() + index);
377 inline const T *Data(
size_t index)
const 380 return (Data() + index);
386 return (base::Length() / type_size);
392 return (base::Capacity() / type_size);
398 return ((base::Capacity() - base::Length()) / type_size);
402 inline T *GrowSizeNC(
size_t num_items)
404 return (T*)base::Append(num_items * type_size);
411 inline T *MakeFreeSpace(
size_t num_items)
413 return (T*)base::Prepare(num_items * type_size);
416 inline OnTransfer Transfer()
418 return OnTransfer(*
this);
static size_t AllocPolicy(size_t min_alloc)
simple allocation policy - can be optimized later
static void RawFree(BlobHeader *p)
all deallocations should happen here
size_t GetReserve() const
Return number of additional items that can fit in the Blob without buffer reallocation.
BlobHeader & Hdr()
blob header accessor - use it rather than using the pointer arithmetics directly - non-const version ...
~CBlobT()
Destructor - ensures that allocated memory (if any) is freed.
size_t MaxSize() const
Return total number of items that can fit in the Blob without buffer reallocation.
ByteBlob(BlobHeader *const &src)
move constructor - take ownership of blob data
size_t Length() const
return the number of valid data bytes in the blob
static BlobHeader * RawAlloc(size_t num_bytes)
all allocation should happen here
byte * data
ptr to the first byte of data
void CheckIdx(size_t index) const
Check the validity of item index (only in debug mode)
bool IsEmpty() const
return true if blob doesn't contain valid data
Blob - simple dynamic T array.
CBlobT(const OnTransfer &ot)
Take ownership constructor.
void Clear()
invalidate blob's data - doesn't free buffer
BlobHeader * header
ptr just after the BlobHeader holding items and capacity
void AppendRaw(const ByteBlob &src)
append bytes from given source blob to the end of existing data bytes - reallocates if necessary ...
size_t Capacity() const
return the current blob capacity in bytes
CBlobT()
Default constructor - makes new Blob ready to accept any data.
void InitEmpty()
initialize the empty blob
byte * Prepare(size_t num_bytes)
Reallocate if there is no free space for num_bytes bytes.
static BlobHeader hdrEmpty[]
Just to silence an unsilencable GCC 4.4+ warning Note: This cannot be 'const' as we do a lot of 'hdrE...
ByteBlob(const ByteBlob &src)
copy constructor
void SmartAlloc(size_t new_size)
reallocate blob data if needed
const byte * Begin() const
return pointer to the first byte of data - const version
byte * Append(size_t num_bytes)
Increase Length() by num_bytes.
byte * Begin()
return pointer to the first byte of data - non-const version
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void AppendRaw(const void *p, size_t num_bytes)
append new bytes at the end of existing data bytes - reallocates if necessary
Base class for simple binary blobs.
const BlobHeader & Hdr() const
blob header accessor - use it rather than using the pointer arithmetics directly - const version ...
size_t Size() const
Return number of items in the Blob.
ByteBlob()
default constructor - initializes empty blob
void Free()
free the blob's memory
void FixTail() const
fixing the four bytes at the end of blob data - useful when blob is used to hold string ...
size_t & LengthRef()
return reference to the actual blob size - used when the size needs to be modified ...
void Init(BlobHeader *src)
initialize blob by attaching it to the given header followed by data
static BlobHeader * Zero()
Return header pointer to the static BlobHeader with both items and capacity containing zero...
static const size_t tail_reserve
four extra bytes will be always allocated and zeroed at the end