OpenTTD Source
1.11.0-beta2
|
Go to the documentation of this file.
13 #include "../core/alloc_func.hpp"
69 static const size_t header_size =
sizeof(
BlobHeader);
87 assert(src !=
nullptr);
117 if (min_alloc < (1 << 9)) {
118 if (min_alloc < (1 << 5))
return (1 << 5);
119 return (min_alloc < (1 << 7)) ? (1 << 7) : (1 << 9);
121 if (min_alloc < (1 << 15)) {
122 if (min_alloc < (1 << 11))
return (1 << 11);
123 return (min_alloc < (1 << 13)) ? (1 << 13) : (1 << 15);
125 if (min_alloc < (1 << 20)) {
126 if (min_alloc < (1 << 17))
return (1 << 17);
127 return (min_alloc < (1 << 19)) ? (1 << 19) : (1 << 20);
129 min_alloc = (min_alloc | ((1 << 20) - 1)) + 1;
222 assert(p !=
nullptr);
224 memcpy(
Append(num_bytes), p, num_bytes);
242 size_t new_size =
Length() + num_bytes;
253 byte *pNewData =
Prepare(num_bytes);
264 assert(new_size < SIZE_MAX - header_size -
tail_reserve);
273 if (tmp->
items != 0) {
305 template <
typename T>
311 static const size_t type_size =
sizeof(T);
318 assert(src.header !=
nullptr);
329 assert(header ==
nullptr);
352 assert(index <
Size());
371 return (
Data() + index);
375 inline const T *
Data(
size_t index)
const
378 return (
Data() + index);
414 inline OnTransfer Transfer()
416 return OnTransfer(*
this);
const BlobHeader & Hdr() const
blob header accessor - use it rather than using the pointer arithmetic directly - const version
size_t GetReserve() const
Return number of additional items that can fit in the Blob without buffer reallocation.
bool IsEmpty() const
return true if blob doesn't contain valid data
BlobHeader & Hdr()
blob header accessor - use it rather than using the pointer arithmetic directly - non-const version
byte * Append(size_t num_bytes)
Increase Length() by num_bytes.
void Init(BlobHeader *src)
initialize blob by attaching it to the given header followed by data
static const size_t tail_reserve
four extra bytes will be always allocated and zeroed at the end
Blob - simple dynamic T array.
void Free()
free the blob's memory
static BlobHeader * RawAlloc(size_t num_bytes)
all allocation should happen here
static BlobHeader hdrEmpty[]
Just to silence an unsilencable GCC 4.4+ warning Note: This cannot be 'const' as we do a lot of 'hdrE...
size_t Capacity() const
return the current blob capacity in bytes
T * GrowSizeNC(size_t num_items)
Grow number of data items in Blob by given number - doesn't construct items.
void Clear()
invalidate blob's data - doesn't free buffer
static BlobHeader * Zero()
Return header pointer to the static BlobHeader with both items and capacity containing zero.
ByteBlob()
default constructor - initializes empty blob
void InitEmpty()
initialize the empty blob
CBlobT()
Default constructor - makes new Blob ready to accept any data.
byte * Prepare(size_t num_bytes)
Reallocate if there is no free space for num_bytes bytes.
size_t Length() const
return the number of valid data bytes in the blob
static size_t AllocPolicy(size_t min_alloc)
simple allocation policy - can be optimized later
byte * data
ptr to the first byte of data
static void RawFree(BlobHeader *p)
all deallocations should happen here
size_t Size() const
Return number of items in the Blob.
~CBlobT()
Destructor - ensures that allocated memory (if any) is freed.
void CheckIdx(size_t index) const
Check the validity of item index (only in debug mode)
const byte * Begin() const
return pointer to the first byte of data - const version
BlobHeader * header
ptr just after the BlobHeader holding items and capacity
CBlobT(const OnTransfer &ot)
Take ownership constructor.
Base class for simple binary blobs.
void FixTail() const
fixing the four bytes at the end of blob data - useful when blob is used to hold string
byte * Begin()
return pointer to the first byte of data - non-const version
size_t MaxSize() const
Return total number of items that can fit in the Blob without buffer reallocation.
void AppendRaw(const void *p, size_t num_bytes)
append new bytes at the end of existing data bytes - reallocates if necessary
void AppendRaw(const ByteBlob &src)
append bytes from given source blob to the end of existing data bytes - reallocates if necessary
T * MakeFreeSpace(size_t num_items)
Ensures that given number of items can be added to the end of Blob.
ByteBlob(const ByteBlob &src)
copy constructor
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void SmartAlloc(size_t new_size)
reallocate blob data if needed
T * Data()
Return pointer to the first data item - non-const version.
size_t & LengthRef()
return reference to the actual blob size - used when the size needs to be modified
ByteBlob(BlobHeader *const &src)
move constructor - take ownership of blob data