2006-05-27 16:12:16 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file blob.hpp Support for storing random binary data. */
|
2007-04-17 20:23:13 +00:00
|
|
|
|
2007-12-25 09:48:53 +00:00
|
|
|
#ifndef BLOB_HPP
|
|
|
|
#define BLOB_HPP
|
|
|
|
|
|
|
|
#include "../core/alloc_func.hpp"
|
2008-06-14 16:23:08 +00:00
|
|
|
#include "../core/mem_func.hpp"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include <new>
|
2006-05-27 16:12:16 +00:00
|
|
|
|
|
|
|
/** Base class for simple binary blobs.
|
2009-03-14 18:16:29 +00:00
|
|
|
* 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:
|
2010-03-19 20:04:24 +00:00
|
|
|
* 1. The only class member is pointer to the first item (see union).
|
2010-03-19 20:06:33 +00:00
|
|
|
* 2. Allocated block contains the blob header (see BlobHeader) followed by the raw byte data.
|
2009-03-14 18:16:29 +00:00
|
|
|
* Always, when it allocates memory the allocated size is:
|
2010-03-19 20:06:33 +00:00
|
|
|
* sizeof(BlobHeader) + <data capacity>
|
|
|
|
* 3. Two 'virtual' members (items and capacity) are stored in the BlobHeader at beginning
|
2009-03-14 18:16:29 +00:00
|
|
|
* of the alloated block.
|
2010-03-19 20:04:24 +00:00
|
|
|
* 4. The pointer of the union pobsize_ts behind the header (to the first data byte).
|
2010-03-19 20:06:33 +00:00
|
|
|
* When memory block is allocated, the sizeof(BlobHeader) it added to it.
|
2009-03-14 18:16:29 +00:00
|
|
|
* 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:
|
2010-03-19 20:06:33 +00:00
|
|
|
* - the fact, that pointer to the alocated block is adjusted by sizeof(BlobHeader) before
|
2009-03-14 18:16:29 +00:00
|
|
|
* 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)
|
|
|
|
*/
|
2010-03-19 20:06:33 +00:00
|
|
|
class ByteBlob {
|
2006-05-27 16:12:16 +00:00
|
|
|
protected:
|
2006-11-24 21:37:08 +00:00
|
|
|
/** header of the allocated memory block */
|
2010-03-19 20:06:33 +00:00
|
|
|
struct BlobHeader {
|
|
|
|
uint items; ///< actual blob size in bytes
|
|
|
|
uint capacity; ///< maximum (allocated) size in bytes
|
2006-05-27 16:12:16 +00:00
|
|
|
};
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** type used as class member */
|
2006-05-27 16:12:16 +00:00
|
|
|
union {
|
2010-03-19 20:06:33 +00:00
|
|
|
byte *data; ///< ptr to the first byte of data
|
|
|
|
BlobHeader *header; ///< ptr just after the BlobHeader holding items and capacity
|
2010-03-19 20:04:24 +00:00
|
|
|
};
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2009-07-29 20:24:48 +00:00
|
|
|
private:
|
2010-02-14 18:33:57 +00:00
|
|
|
/**
|
|
|
|
* Just to silence an unsilencable GCC 4.4+ warning
|
2010-03-19 20:06:33 +00:00
|
|
|
* Note: This cannot be 'const' as we do a lot of 'hdrEmpty[0]->items += 0;' and 'hdrEmpty[0]->capacity += 0;'
|
2010-02-14 18:33:57 +00:00
|
|
|
* after const_casting.
|
|
|
|
*/
|
2010-03-19 20:06:33 +00:00
|
|
|
static BlobHeader hdrEmpty[];
|
2009-07-29 20:24:48 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
public:
|
2010-03-19 20:06:33 +00:00
|
|
|
static const uint tail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end
|
|
|
|
static const uint header_size = sizeof(BlobHeader);
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** default constructor - initializes empty blob */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE ByteBlob() { InitEmpty(); }
|
2007-06-29 22:16:15 +00:00
|
|
|
|
|
|
|
/** move constructor - take ownership of blob data */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE ByteBlob(BlobHeader * const & src)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
assert(src != NULL);
|
|
|
|
header = src;
|
|
|
|
*const_cast<BlobHeader**>(&src) = NULL;
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** destructor */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE ~ByteBlob()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
protected:
|
2010-03-19 20:13:15 +00:00
|
|
|
/** all allocation should happen here */
|
|
|
|
static FORCEINLINE BlobHeader *RawAlloc(uint num_bytes)
|
|
|
|
{
|
|
|
|
return (BlobHeader*)MallocT<byte>(num_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** simple allocation policy - can be optimized later */
|
|
|
|
static FORCEINLINE uint AllocPolicy(uint min_alloc)
|
|
|
|
{
|
|
|
|
if (min_alloc < (1 << 9)) {
|
|
|
|
if (min_alloc < (1 << 5)) return (1 << 5);
|
|
|
|
return (min_alloc < (1 << 7)) ? (1 << 7) : (1 << 9);
|
|
|
|
}
|
|
|
|
if (min_alloc < (1 << 15)) {
|
|
|
|
if (min_alloc < (1 << 11)) return (1 << 11);
|
|
|
|
return (min_alloc < (1 << 13)) ? (1 << 13) : (1 << 15);
|
|
|
|
}
|
|
|
|
if (min_alloc < (1 << 20)) {
|
|
|
|
if (min_alloc < (1 << 17)) return (1 << 17);
|
|
|
|
return (min_alloc < (1 << 19)) ? (1 << 19) : (1 << 20);
|
|
|
|
}
|
|
|
|
min_alloc = (min_alloc | ((1 << 20) - 1)) + 1;
|
|
|
|
return min_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** all deallocations should happen here */
|
|
|
|
static FORCEINLINE void RawFree(BlobHeader *p)
|
|
|
|
{
|
|
|
|
/* Just to silence an unsilencable GCC 4.4+ warning. */
|
|
|
|
assert(p != ByteBlob::hdrEmpty);
|
|
|
|
|
|
|
|
/* In case GCC warns about the following, see GCC's PR38509 why it is bogus. */
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** initialize the empty blob */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void InitEmpty()
|
|
|
|
{
|
2010-03-19 20:13:15 +00:00
|
|
|
header = Zero();
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** initialize blob by attaching it to the given header followed by data */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE void Init(BlobHeader *src)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
header = &src[1];
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE BlobHeader& Hdr()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return *(header - 1);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** blob header accessor - use it rather than using the pointer arithmetics directly - const version */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE const BlobHeader& Hdr() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return *(header - 1);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return reference to the actual blob size - used when the size needs to be modified */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE uint& LengthRef()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return Hdr().items;
|
2007-06-29 22:16:15 +00:00
|
|
|
};
|
2006-05-27 16:12:16 +00:00
|
|
|
|
|
|
|
public:
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return true if blob doesn't contain valid data */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE bool IsEmpty() const
|
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return Length() == 0;
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return the number of valid data bytes in the blob */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE uint Length() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return Hdr().items;
|
2007-06-29 22:16:15 +00:00
|
|
|
};
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return the current blob capacity in bytes */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE uint Capacity() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return Hdr().capacity;
|
2007-06-29 22:16:15 +00:00
|
|
|
};
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return pointer to the first byte of data - non-const version */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE byte *Begin()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return data;
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return pointer to the first byte of data - const version */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE const byte *Begin() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
return data;
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** invalidate blob's data - doesn't free buffer */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void Clear()
|
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
LengthRef() = 0;
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** free the blob's memory */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void Free()
|
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
if (Capacity() > 0) {
|
2007-06-29 22:16:15 +00:00
|
|
|
RawFree(&Hdr());
|
|
|
|
InitEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** append new bytes at the end of existing data bytes - reallocates if necessary */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE void AppendRaw(const void *p, uint num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
assert(p != NULL);
|
|
|
|
if (num_bytes > 0) {
|
2010-03-19 20:08:40 +00:00
|
|
|
memcpy(Append(num_bytes), p, num_bytes);
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reallocate if there is no free space for num_bytes bytes.
|
2009-03-14 18:16:29 +00:00
|
|
|
* @return pointer to the new data to be added */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE byte *Prepare(uint num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
uint new_size = Length() + num_bytes;
|
|
|
|
if (new_size > Capacity()) SmartAlloc(new_size);
|
|
|
|
return data + Length();
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 20:08:40 +00:00
|
|
|
/** Increase Length() by num_bytes.
|
2009-03-14 18:16:29 +00:00
|
|
|
* @return pointer to the new data added */
|
2010-03-19 20:08:40 +00:00
|
|
|
FORCEINLINE byte *Append(uint num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
byte *pNewData = Prepare(num_bytes);
|
|
|
|
LengthRef() += num_bytes;
|
2006-05-27 16:12:16 +00:00
|
|
|
return pNewData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** reallocate blob data if needed */
|
2010-03-19 20:03:25 +00:00
|
|
|
void SmartAlloc(uint new_size)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
uint old_max_size = Capacity();
|
2006-05-27 16:12:16 +00:00
|
|
|
if (old_max_size >= new_size) return;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* calculate minimum block size we need to allocate */
|
2010-03-19 20:06:33 +00:00
|
|
|
uint min_alloc_size = header_size + new_size + tail_reserve;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* ask allocation policy for some reasonable block size */
|
2010-03-19 20:03:25 +00:00
|
|
|
uint alloc_size = AllocPolicy(min_alloc_size);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* allocate new block */
|
2010-03-19 20:06:33 +00:00
|
|
|
BlobHeader *tmp = RawAlloc(alloc_size);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* setup header */
|
2010-03-19 20:08:40 +00:00
|
|
|
tmp->items = Length();
|
2010-03-19 20:06:33 +00:00
|
|
|
tmp->capacity = alloc_size - (header_size + tail_reserve);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* copy existing data */
|
2010-03-19 20:08:40 +00:00
|
|
|
if (Length() > 0)
|
2010-03-19 20:06:33 +00:00
|
|
|
memcpy(tmp + 1, data, tmp->items);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* replace our block with new one */
|
2010-03-19 20:06:33 +00:00
|
|
|
BlobHeader *pOldHdr = &Hdr();
|
|
|
|
Init(tmp);
|
2006-05-27 16:12:16 +00:00
|
|
|
if (old_max_size > 0)
|
|
|
|
RawFree(pOldHdr);
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/** fixing the four bytes at the end of blob data - useful when blob is used to hold string */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void FixTail() const
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
if (Capacity() > 0) {
|
|
|
|
byte *p = &data[Length()];
|
2010-03-19 20:06:33 +00:00
|
|
|
for (uint i = 0; i < tail_reserve; i++) {
|
2007-06-29 22:16:15 +00:00
|
|
|
p[i] = 0;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-19 20:03:25 +00:00
|
|
|
/** 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
|
2009-03-14 18:16:29 +00:00
|
|
|
* 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) */
|
2010-03-19 20:03:25 +00:00
|
|
|
template <typename T>
|
2010-03-19 20:06:33 +00:00
|
|
|
class CBlobT : public ByteBlob {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* make template arguments public: */
|
2006-05-27 16:12:16 +00:00
|
|
|
public:
|
2010-03-19 20:06:33 +00:00
|
|
|
typedef ByteBlob base;
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-03-19 20:03:25 +00:00
|
|
|
static const uint type_size = sizeof(T);
|
2007-06-29 22:16:15 +00:00
|
|
|
|
|
|
|
struct OnTransfer {
|
2010-03-19 20:06:33 +00:00
|
|
|
typename base::BlobHeader *header;
|
|
|
|
OnTransfer(const OnTransfer& src) : header(src.header) {assert(src.header != NULL); *const_cast<typename base::BlobHeader**>(&src.header) = NULL;}
|
|
|
|
OnTransfer(CBlobT& src) : header(src.header) {src.InitEmpty();}
|
|
|
|
~OnTransfer() {assert(header == NULL);}
|
2007-06-29 22:16:15 +00:00
|
|
|
};
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Default constructor - makes new Blob ready to accept any data */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE CBlobT()
|
2010-03-19 20:03:25 +00:00
|
|
|
: base()
|
2007-06-29 22:16:15 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
/** Take ownership constructor */
|
|
|
|
FORCEINLINE CBlobT(const OnTransfer& ot)
|
2010-03-19 20:06:33 +00:00
|
|
|
: base(ot.header)
|
2007-06-29 22:16:15 +00:00
|
|
|
{}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Destructor - ensures that allocated memory (if any) is freed */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE ~CBlobT()
|
|
|
|
{
|
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Check the validity of item index (only in debug mode) */
|
2010-03-19 20:06:33 +00:00
|
|
|
FORCEINLINE void CheckIdx(uint index) const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
assert(index < Size());
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return pointer to the first data item - non-const version */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE T *Data()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return (T*)base::Begin();
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return pointer to the first data item - const version */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE const T *Data() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return (const T*)base::Begin();
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 20:06:33 +00:00
|
|
|
/** Return pointer to the index-th data item - non-const version */
|
|
|
|
FORCEINLINE T *Data(uint index)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
CheckIdx(index);
|
|
|
|
return (Data() + index);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 20:06:33 +00:00
|
|
|
/** Return pointer to the index-th data item - const version */
|
|
|
|
FORCEINLINE const T *Data(uint index) const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:06:33 +00:00
|
|
|
CheckIdx(index);
|
|
|
|
return (Data() + index);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return number of items in the Blob */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE uint Size() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return (base::Length() / type_size);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return total number of items that can fit in the Blob without buffer reallocation */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE uint MaxSize() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return (base::Capacity() / type_size);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
/** Return number of additional items that can fit in the Blob without buffer reallocation */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE uint GetReserve() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return ((base::Capacity() - base::Length()) / type_size);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Grow number of data items in Blob by given number - doesn't construct items */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE T *GrowSizeNC(uint num_items)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return (T*)base::Append(num_items * type_size);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Add given items (ptr + number of items) at the end of blob */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE T *Append(const T *pSrc, uint num_items)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-03-19 20:03:25 +00:00
|
|
|
T *pDst = GrowSizeNC(num_items);
|
|
|
|
T *pDstOrg = pDst;
|
|
|
|
T *pDstEnd = pDst + num_items;
|
|
|
|
while (pDst < pDstEnd) new (pDst++) T(*(pSrc++));
|
2006-05-27 16:12:16 +00:00
|
|
|
return pDstOrg;
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Ensures that given number of items can be added to the end of Blob. Returns pointer to the
|
2009-03-14 18:16:29 +00:00
|
|
|
* first free (unused) item */
|
2010-03-19 20:03:25 +00:00
|
|
|
FORCEINLINE T *MakeFreeSpace(uint num_items)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2010-03-19 20:08:40 +00:00
|
|
|
return (T*)base::Prepare(num_items * type_size);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE OnTransfer Transfer()
|
|
|
|
{
|
|
|
|
return OnTransfer(*this);
|
|
|
|
};
|
2006-05-27 16:12:16 +00:00
|
|
|
};
|
|
|
|
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
#endif /* BLOB_HPP */
|