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"
|
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:
|
|
|
|
* 1. The only class member is pointer to the first item (see union ptr_u).
|
|
|
|
* 2. Allocated block contains the blob header (see CHdr) followed by the raw byte data.
|
|
|
|
* Always, when it allocates memory the allocated size is:
|
|
|
|
* sizeof(CHdr) + <data capacity>
|
|
|
|
* 3. Two 'virtual' members (m_size and m_max_size) are stored in the CHdr at beginning
|
|
|
|
* of the alloated block.
|
|
|
|
* 4. The pointter (in ptr_u) pobsize_ts behind the header (to the first data byte).
|
|
|
|
* When memory block is allocated, the sizeof(CHdr) it added to it.
|
|
|
|
* 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:
|
|
|
|
* - the fact, that pointer to the alocated block is adjusted by sizeof(CHdr) before
|
|
|
|
* 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)
|
|
|
|
*/
|
2006-05-27 16:12:16 +00:00
|
|
|
class CBlobBaseSimple {
|
2007-06-29 22:16:15 +00:00
|
|
|
public:
|
|
|
|
typedef ::ptrdiff_t bsize_t;
|
|
|
|
typedef ::byte bitem_t;
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
protected:
|
2006-11-24 21:37:08 +00:00
|
|
|
/** header of the allocated memory block */
|
2006-08-28 18:53:03 +00:00
|
|
|
struct CHdr {
|
2007-06-29 22:16:15 +00:00
|
|
|
bsize_t m_size; ///< actual blob size in bytes
|
|
|
|
bsize_t m_max_size; ///< 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 {
|
2007-06-29 22:16:15 +00:00
|
|
|
bitem_t *m_pData; ///< ptr to the first byte of data
|
|
|
|
CHdr *m_pHdr_1; ///< ptr just after the CHdr holding m_size and m_max_size
|
2006-05-27 16:12:16 +00:00
|
|
|
} ptr_u;
|
|
|
|
|
2009-07-29 20:24:48 +00:00
|
|
|
private:
|
|
|
|
/** Just to silence an unsilencable GCC 4.4+ warning */
|
|
|
|
static const CHdr hdrEmpty[];
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
public:
|
2007-06-29 22:16:15 +00:00
|
|
|
static const bsize_t Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** default constructor - initializes empty blob */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE CBlobBaseSimple() { InitEmpty(); }
|
2007-06-29 22:16:15 +00:00
|
|
|
/** constructor - create blob with data */
|
|
|
|
FORCEINLINE CBlobBaseSimple(const bitem_t *p, bsize_t num_bytes)
|
|
|
|
{
|
|
|
|
InitEmpty();
|
|
|
|
AppendRaw(p, num_bytes);
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** copy constructor */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE CBlobBaseSimple(const CBlobBaseSimple& src)
|
|
|
|
{
|
|
|
|
InitEmpty();
|
|
|
|
AppendRaw(src);
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
|
|
|
/** move constructor - take ownership of blob data */
|
|
|
|
FORCEINLINE CBlobBaseSimple(CHdr * const & pHdr_1)
|
|
|
|
{
|
|
|
|
assert(pHdr_1 != NULL);
|
|
|
|
ptr_u.m_pHdr_1 = pHdr_1;
|
2009-05-24 21:09:00 +00:00
|
|
|
*const_cast<CHdr**>(&pHdr_1) = NULL;
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** destructor */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE ~CBlobBaseSimple()
|
|
|
|
{
|
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
protected:
|
2006-11-24 21:37:08 +00:00
|
|
|
/** initialize the empty blob by setting the ptr_u.m_pHdr_1 pointer to the static CHdr with
|
2009-03-14 18:16:29 +00:00
|
|
|
* both m_size and m_max_size containing zero */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void InitEmpty()
|
|
|
|
{
|
2009-07-29 20:24:48 +00:00
|
|
|
ptr_u.m_pHdr_1 = const_cast<CHdr *>(&CBlobBaseSimple::hdrEmpty[1]);
|
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 */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE void Init(CHdr *hdr)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
ptr_u.m_pHdr_1 = &hdr[1];
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE CHdr& Hdr()
|
|
|
|
{
|
|
|
|
return ptr_u.m_pHdr_1[-1];
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** blob header accessor - use it rather than using the pointer arithmetics directly - const version */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE const CHdr& Hdr() const
|
|
|
|
{
|
|
|
|
return ptr_u.m_pHdr_1[-1];
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return reference to the actual blob size - used when the size needs to be modified */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE bsize_t& RawSizeRef()
|
|
|
|
{
|
|
|
|
return Hdr().m_size;
|
|
|
|
};
|
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
|
|
|
|
{
|
|
|
|
return RawSize() == 0;
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return the number of valid data bytes in the blob */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE bsize_t RawSize() const
|
|
|
|
{
|
|
|
|
return Hdr().m_size;
|
|
|
|
};
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return the current blob capacity in bytes */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE bsize_t MaxRawSize() const
|
|
|
|
{
|
|
|
|
return Hdr().m_max_size;
|
|
|
|
};
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return pointer to the first byte of data - non-const version */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE bitem_t *RawData()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
return ptr_u.m_pData;
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return pointer to the first byte of data - const version */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE const bitem_t *RawData() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
return ptr_u.m_pData;
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** return the 32 bit CRC of valid data in the blob */
|
2007-06-29 22:16:15 +00:00
|
|
|
//FORCEINLINE bsize_t Crc32() const
|
|
|
|
//{
|
|
|
|
// return CCrc32::Calc(RawData(), RawSize());
|
|
|
|
//}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
RawSizeRef() = 0;
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** free the blob's memory */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void Free()
|
|
|
|
{
|
|
|
|
if (MaxRawSize() > 0) {
|
|
|
|
RawFree(&Hdr());
|
|
|
|
InitEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** copy data from another blob - replaces any existing blob's data */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void CopyFrom(const CBlobBaseSimple& src)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
AppendRaw(src);
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** overtake ownership of data buffer from the source blob - source blob will become empty */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void MoveFrom(CBlobBaseSimple& src)
|
|
|
|
{
|
|
|
|
Free();
|
|
|
|
ptr_u.m_pData = src.ptr_u.m_pData;
|
|
|
|
src.InitEmpty();
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** swap buffers (with data) between two blobs (this and source blob) */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void Swap(CBlobBaseSimple& src)
|
|
|
|
{
|
|
|
|
bitem_t *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData;
|
|
|
|
src.ptr_u.m_pData = tmp;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** append new bytes at the end of existing data bytes - reallocates if necessary */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void AppendRaw(const void *p, bsize_t num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
assert(p != NULL);
|
|
|
|
if (num_bytes > 0) {
|
|
|
|
memcpy(GrowRawSize(num_bytes), p, num_bytes);
|
2006-08-28 18:53:03 +00:00
|
|
|
} else {
|
2006-05-27 16:12:16 +00:00
|
|
|
assert(num_bytes >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */
|
2006-05-27 16:12:16 +00:00
|
|
|
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.
|
2009-03-14 18:16:29 +00:00
|
|
|
* @return pointer to the new data to be added */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE bitem_t *MakeRawFreeSpace(bsize_t num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
assert(num_bytes >= 0);
|
2007-06-29 22:16:15 +00:00
|
|
|
bsize_t new_size = RawSize() + num_bytes;
|
2006-05-27 16:12:16 +00:00
|
|
|
if (new_size > MaxRawSize()) SmartAlloc(new_size);
|
|
|
|
return ptr_u.m_pData + RawSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Increase RawSize() by num_bytes.
|
2009-03-14 18:16:29 +00:00
|
|
|
* @return pointer to the new data added */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE bitem_t *GrowRawSize(bsize_t num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
bitem_t *pNewData = MakeRawFreeSpace(num_bytes);
|
2006-05-27 16:12:16 +00:00
|
|
|
RawSizeRef() += num_bytes;
|
|
|
|
return pNewData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Decrease RawSize() by num_bytes. */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void ReduceRawSize(bsize_t num_bytes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
if (MaxRawSize() > 0 && num_bytes > 0) {
|
|
|
|
assert(num_bytes <= RawSize());
|
2009-08-20 10:23:39 +00:00
|
|
|
if (num_bytes < RawSize()) {
|
|
|
|
RawSizeRef() -= num_bytes;
|
|
|
|
} else {
|
|
|
|
RawSizeRef() = 0;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/** reallocate blob data if needed */
|
2007-06-29 22:16:15 +00:00
|
|
|
void SmartAlloc(bsize_t new_size)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2007-06-29 22:16:15 +00:00
|
|
|
bsize_t old_max_size = MaxRawSize();
|
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 */
|
2007-06-29 22:16:15 +00:00
|
|
|
bsize_t min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* ask allocation policy for some reasonable block size */
|
2007-06-29 22:16:15 +00:00
|
|
|
bsize_t alloc_size = AllocPolicy(min_alloc_size);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* allocate new block */
|
2009-01-10 00:31:47 +00:00
|
|
|
CHdr *pNewHdr = RawAlloc(alloc_size);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* setup header */
|
2006-05-27 16:12:16 +00:00
|
|
|
pNewHdr->m_size = RawSize();
|
|
|
|
pNewHdr->m_max_size = alloc_size - (sizeof(CHdr) + Ttail_reserve);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* copy existing data */
|
2006-05-27 16:12:16 +00:00
|
|
|
if (RawSize() > 0)
|
|
|
|
memcpy(pNewHdr + 1, ptr_u.m_pData, pNewHdr->m_size);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* replace our block with new one */
|
2009-01-10 00:31:47 +00:00
|
|
|
CHdr *pOldHdr = &Hdr();
|
2006-05-27 16:12:16 +00:00
|
|
|
Init(pNewHdr);
|
|
|
|
if (old_max_size > 0)
|
|
|
|
RawFree(pOldHdr);
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/** simple allocation policy - can be optimized later */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE static bsize_t AllocPolicy(bsize_t min_alloc)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
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 allocation should happen here */
|
2009-01-10 00:31:47 +00:00
|
|
|
static FORCEINLINE CHdr *RawAlloc(bsize_t num_bytes)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2007-12-08 14:50:41 +00:00
|
|
|
return (CHdr*)MallocT<byte>(num_bytes);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/** all deallocations should happen here */
|
2009-01-10 00:31:47 +00:00
|
|
|
static FORCEINLINE void RawFree(CHdr *p)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2009-09-07 20:07:47 +00:00
|
|
|
/* Just to silence an unsilencable GCC 4.4+ warning. */
|
2009-07-29 20:24:48 +00:00
|
|
|
assert(p != CBlobBaseSimple::hdrEmpty);
|
|
|
|
|
2009-09-07 20:07:47 +00:00
|
|
|
/* In case GCC warns about the following, see GCC's PR38509 why it is bogus. */
|
2007-06-29 22:16:15 +00:00
|
|
|
free(p);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
if (MaxRawSize() > 0) {
|
2007-06-29 22:16:15 +00:00
|
|
|
bitem_t *p = &ptr_u.m_pData[RawSize()];
|
|
|
|
for (bsize_t i = 0; i < Ttail_reserve; i++) {
|
|
|
|
p[i] = 0;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Blob - simple dynamic Titem_ array. Titem_ (template argument) is a placeholder for any type.
|
2009-03-14 18:16:29 +00:00
|
|
|
* Titem_ 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) */
|
2006-05-27 16:12:16 +00:00
|
|
|
template <class Titem_, class Tbase_ = CBlobBaseSimple>
|
2007-06-29 22:16:15 +00:00
|
|
|
class CBlobT : public Tbase_ {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* make template arguments public: */
|
2006-05-27 16:12:16 +00:00
|
|
|
public:
|
|
|
|
typedef Titem_ Titem;
|
|
|
|
typedef Tbase_ Tbase;
|
2007-06-29 22:16:15 +00:00
|
|
|
typedef typename Tbase::bsize_t bsize_t;
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2007-06-29 22:16:15 +00:00
|
|
|
static const bsize_t Titem_size = sizeof(Titem);
|
|
|
|
|
|
|
|
struct OnTransfer {
|
|
|
|
typename Tbase_::CHdr *m_pHdr_1;
|
2009-05-24 21:09:00 +00:00
|
|
|
OnTransfer(const OnTransfer& src) : m_pHdr_1(src.m_pHdr_1) {assert(src.m_pHdr_1 != NULL); *const_cast<typename Tbase_::CHdr**>(&src.m_pHdr_1) = NULL;}
|
2007-06-29 22:16:15 +00:00
|
|
|
OnTransfer(CBlobT& src) : m_pHdr_1(src.ptr_u.m_pHdr_1) {src.InitEmpty();}
|
|
|
|
~OnTransfer() {assert(m_pHdr_1 == NULL);}
|
|
|
|
};
|
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()
|
|
|
|
: Tbase()
|
|
|
|
{}
|
|
|
|
|
|
|
|
/** Constructor - makes new Blob with data */
|
|
|
|
FORCEINLINE CBlobT(const Titem_ *p, bsize_t num_items)
|
|
|
|
: Tbase((typename Tbase_::bitem_t*)p, num_items * Titem_size)
|
|
|
|
{}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Copy constructor - make new blob to become copy of the original (source) blob */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE CBlobT(const Tbase& src)
|
|
|
|
: Tbase(src)
|
|
|
|
{
|
|
|
|
assert((Tbase::RawSize() % Titem_size) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Take ownership constructor */
|
|
|
|
FORCEINLINE CBlobT(const OnTransfer& ot)
|
|
|
|
: Tbase(ot.m_pHdr_1)
|
|
|
|
{}
|
|
|
|
|
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) */
|
2008-05-07 18:31:29 +00:00
|
|
|
FORCEINLINE void CheckIdx(bsize_t idx) const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
assert(idx >= 0); assert(idx < Size());
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return pointer to the first data item - non-const version */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *Data()
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
return (Titem*)Tbase::RawData();
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return pointer to the first data item - const version */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE const Titem *Data() const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
return (const Titem*)Tbase::RawData();
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return pointer to the idx-th data item - non-const version */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *Data(bsize_t idx)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
CheckIdx(idx);
|
|
|
|
return (Data() + idx);
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return pointer to the idx-th data item - const version */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE const Titem *Data(bsize_t idx) const
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
2008-05-07 18:31:29 +00:00
|
|
|
CheckIdx(idx);
|
|
|
|
return (Data() + idx);
|
2007-06-29 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Return number of items in the Blob */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE bsize_t Size() const
|
|
|
|
{
|
|
|
|
return (Tbase::RawSize() / Titem_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return total number of items that can fit in the Blob without buffer reallocation */
|
|
|
|
FORCEINLINE bsize_t MaxSize() const
|
|
|
|
{
|
|
|
|
return (Tbase::MaxRawSize() / Titem_size);
|
|
|
|
}
|
|
|
|
/** Return number of additional items that can fit in the Blob without buffer reallocation */
|
|
|
|
FORCEINLINE bsize_t GetReserve() const
|
|
|
|
{
|
|
|
|
return ((Tbase::MaxRawSize() - Tbase::RawSize()) / Titem_size);
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Free the memory occupied by Blob destroying all items */
|
2006-05-27 16:12:16 +00:00
|
|
|
FORCEINLINE void Free()
|
|
|
|
{
|
2007-06-29 22:16:15 +00:00
|
|
|
assert((Tbase::RawSize() % Titem_size) == 0);
|
|
|
|
bsize_t old_size = Size();
|
2006-05-27 16:12:16 +00:00
|
|
|
if (old_size > 0) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* destroy removed items; */
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pI_last_to_destroy = Data(0);
|
|
|
|
for (Titem *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem_();
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
Tbase::Free();
|
|
|
|
}
|
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 */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *GrowSizeNC(bsize_t num_items)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
return (Titem*)Tbase::GrowRawSize(num_items * Titem_size);
|
|
|
|
}
|
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Grow number of data items in Blob by given number - constructs new items (using Titem_'s default constructor) */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *GrowSizeC(bsize_t num_items)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pI = GrowSizeNC(num_items);
|
2007-06-29 22:16:15 +00:00
|
|
|
for (bsize_t i = num_items; i > 0; i--, pI++) new (pI) Titem();
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Destroy given number of items and reduce the Blob's data size */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void ReduceSize(bsize_t num_items)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2007-06-29 22:16:15 +00:00
|
|
|
assert((Tbase::RawSize() % Titem_size) == 0);
|
|
|
|
bsize_t old_size = Size();
|
2006-05-27 16:12:16 +00:00
|
|
|
assert(num_items <= old_size);
|
2007-06-29 22:16:15 +00:00
|
|
|
bsize_t new_size = (num_items <= old_size) ? (old_size - num_items) : 0;
|
2009-03-15 00:32:18 +00:00
|
|
|
/* destroy removed items; */
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pI_last_to_destroy = Data(new_size);
|
|
|
|
for (Titem *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem();
|
2009-03-15 00:32:18 +00:00
|
|
|
/* remove them */
|
2007-06-29 22:16:15 +00:00
|
|
|
Tbase::ReduceRawSize(num_items * Titem_size);
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Append one data item at the end (calls Titem_'s default constructor) */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *AppendNew()
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2006-11-24 21:37:08 +00:00
|
|
|
Titem& dst = *GrowSizeNC(1); // Grow size by one item
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pNewItem = new (&dst) Titem(); // construct the new item by calling in-place new operator
|
2006-05-27 16:12:16 +00:00
|
|
|
return pNewItem;
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Append the copy of given item at the end of Blob (using copy constructor) */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *Append(const Titem& src)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2006-11-24 21:37:08 +00:00
|
|
|
Titem& dst = *GrowSizeNC(1); // Grow size by one item
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pNewItem = new (&dst) Titem(src); // construct the new item by calling in-place new operator with copy ctor()
|
2006-05-27 16:12:16 +00:00
|
|
|
return pNewItem;
|
|
|
|
}
|
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 */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *Append(const Titem *pSrc, bsize_t num_items)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pDst = GrowSizeNC(num_items);
|
|
|
|
Titem *pDstOrg = pDst;
|
|
|
|
Titem *pDstEnd = pDst + num_items;
|
2006-05-27 16:12:16 +00:00
|
|
|
while (pDst < pDstEnd) new (pDst++) Titem(*(pSrc++));
|
|
|
|
return pDstOrg;
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Remove item with the given index by replacing it by the last item and reducing the size by one */
|
2007-06-29 22:16:15 +00:00
|
|
|
FORCEINLINE void RemoveBySwap(bsize_t idx)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
CheckIdx(idx);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* destroy removed item */
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pRemoved = Data(idx);
|
2006-05-27 16:12:16 +00:00
|
|
|
RemoveBySwap(pRemoved);
|
|
|
|
}
|
2007-06-29 22:16:15 +00:00
|
|
|
|
2006-11-24 21:37:08 +00:00
|
|
|
/** Remove item given by pointer replacing it by the last item and reducing the size by one */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE void RemoveBySwap(Titem *pItem)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
Titem *pLast = Data(Size() - 1);
|
2006-05-27 16:12:16 +00:00
|
|
|
assert(pItem >= Data() && pItem <= pLast);
|
2009-03-15 00:32:18 +00:00
|
|
|
/* move last item to its new place */
|
2006-05-27 16:12:16 +00:00
|
|
|
if (pItem != pLast) {
|
|
|
|
pItem->~Titem_();
|
|
|
|
new (pItem) Titem_(*pLast);
|
|
|
|
}
|
2009-03-15 00:32:18 +00:00
|
|
|
/* destroy the last item */
|
2006-05-27 16:12:16 +00:00
|
|
|
pLast->~Titem_();
|
2009-03-15 00:32:18 +00:00
|
|
|
/* and reduce the raw blob size */
|
2007-06-29 22:16:15 +00:00
|
|
|
Tbase::ReduceRawSize(Titem_size);
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
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 */
|
2009-01-10 00:31:47 +00:00
|
|
|
FORCEINLINE Titem *MakeFreeSpace(bsize_t num_items)
|
2007-06-29 22:16:15 +00:00
|
|
|
{
|
|
|
|
return (Titem*)Tbase::MakeRawFreeSpace(num_items * Titem_size);
|
|
|
|
}
|
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 */
|