mirror of https://github.com/OpenTTD/OpenTTD
(svn r19470) -Codechange: rename blob internals (skidd13)
parent
767259853b
commit
6f1a0f890b
|
@ -28,95 +28,96 @@
|
||||||
*
|
*
|
||||||
* Internal member layout:
|
* Internal member layout:
|
||||||
* 1. The only class member is pointer to the first item (see union).
|
* 1. The only class member is pointer to the first item (see union).
|
||||||
* 2. Allocated block contains the blob header (see CHdr) followed by the raw byte data.
|
* 2. Allocated block contains the blob header (see BlobHeader) followed by the raw byte data.
|
||||||
* Always, when it allocates memory the allocated size is:
|
* Always, when it allocates memory the allocated size is:
|
||||||
* sizeof(CHdr) + <data capacity>
|
* sizeof(BlobHeader) + <data capacity>
|
||||||
* 3. Two 'virtual' members (m_size and m_max_size) are stored in the CHdr at beginning
|
* 3. Two 'virtual' members (items and capacity) are stored in the BlobHeader at beginning
|
||||||
* of the alloated block.
|
* of the alloated block.
|
||||||
* 4. The pointer of the union pobsize_ts behind the header (to the first data byte).
|
* 4. The pointer of the union pobsize_ts behind the header (to the first data byte).
|
||||||
* When memory block is allocated, the sizeof(CHdr) it added to it.
|
* When memory block is allocated, the sizeof(BlobHeader) it added to it.
|
||||||
* 5. Benefits of this layout:
|
* 5. Benefits of this layout:
|
||||||
* - items are accessed in the simplest possible way - just dereferencing the pointer,
|
* - items are accessed in the simplest possible way - just dereferencing the pointer,
|
||||||
* which is good for performance (assuming that data are accessed most often).
|
* which is good for performance (assuming that data are accessed most often).
|
||||||
* - sizeof(blob) is the same as the size of any other pointer
|
* - sizeof(blob) is the same as the size of any other pointer
|
||||||
* 6. Drawbacks of this layout:
|
* 6. Drawbacks of this layout:
|
||||||
* - the fact, that pointer to the alocated block is adjusted by sizeof(CHdr) before
|
* - the fact, that pointer to the alocated block is adjusted by sizeof(BlobHeader) before
|
||||||
* it is stored can lead to several confusions:
|
* it is stored can lead to several confusions:
|
||||||
* - it is not common pattern so the implementation code is bit harder to read
|
* - 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)
|
* - valgrind can generate warning that allocated block is lost (not accessible)
|
||||||
*/
|
*/
|
||||||
class CBlobBaseSimple {
|
class ByteBlob {
|
||||||
protected:
|
protected:
|
||||||
/** header of the allocated memory block */
|
/** header of the allocated memory block */
|
||||||
struct CHdr {
|
struct BlobHeader {
|
||||||
uint m_size; ///< actual blob size in bytes
|
uint items; ///< actual blob size in bytes
|
||||||
uint m_max_size; ///< maximum (allocated) size in bytes
|
uint capacity; ///< maximum (allocated) size in bytes
|
||||||
};
|
};
|
||||||
|
|
||||||
/** type used as class member */
|
/** type used as class member */
|
||||||
union {
|
union {
|
||||||
byte *m_pData; ///< ptr to the first byte of data
|
byte *data; ///< ptr to the first byte of data
|
||||||
CHdr *m_pHdr_1; ///< ptr just after the CHdr holding m_size and m_max_size
|
BlobHeader *header; ///< ptr just after the BlobHeader holding items and capacity
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* Just to silence an unsilencable GCC 4.4+ warning
|
* Just to silence an unsilencable GCC 4.4+ warning
|
||||||
* Note: This cannot be 'const' as we do a lot of 'hdrEmpty[0]->m_size += 0;' and 'hdrEmpty[0]->m_max_size += 0;'
|
* Note: This cannot be 'const' as we do a lot of 'hdrEmpty[0]->items += 0;' and 'hdrEmpty[0]->capacity += 0;'
|
||||||
* after const_casting.
|
* after const_casting.
|
||||||
*/
|
*/
|
||||||
static CHdr hdrEmpty[];
|
static BlobHeader hdrEmpty[];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const uint Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end
|
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);
|
||||||
|
|
||||||
/** default constructor - initializes empty blob */
|
/** default constructor - initializes empty blob */
|
||||||
FORCEINLINE CBlobBaseSimple() { InitEmpty(); }
|
FORCEINLINE ByteBlob() { InitEmpty(); }
|
||||||
|
|
||||||
/** move constructor - take ownership of blob data */
|
/** move constructor - take ownership of blob data */
|
||||||
FORCEINLINE CBlobBaseSimple(CHdr * const & pHdr_1)
|
FORCEINLINE ByteBlob(BlobHeader * const & src)
|
||||||
{
|
{
|
||||||
assert(pHdr_1 != NULL);
|
assert(src != NULL);
|
||||||
m_pHdr_1 = pHdr_1;
|
header = src;
|
||||||
*const_cast<CHdr**>(&pHdr_1) = NULL;
|
*const_cast<BlobHeader**>(&src) = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** destructor */
|
/** destructor */
|
||||||
FORCEINLINE ~CBlobBaseSimple()
|
FORCEINLINE ~ByteBlob()
|
||||||
{
|
{
|
||||||
Free();
|
Free();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** initialize the empty blob by setting the m_pHdr_1 pointer to the static CHdr with
|
/** initialize the empty blob by setting the header pointer to the static BlobHeader with
|
||||||
* both m_size and m_max_size containing zero */
|
* both items and capacity containing zero */
|
||||||
FORCEINLINE void InitEmpty()
|
FORCEINLINE void InitEmpty()
|
||||||
{
|
{
|
||||||
m_pHdr_1 = const_cast<CHdr *>(&CBlobBaseSimple::hdrEmpty[1]);
|
header = const_cast<BlobHeader *>(&ByteBlob::hdrEmpty[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** initialize blob by attaching it to the given header followed by data */
|
/** initialize blob by attaching it to the given header followed by data */
|
||||||
FORCEINLINE void Init(CHdr *hdr)
|
FORCEINLINE void Init(BlobHeader *src)
|
||||||
{
|
{
|
||||||
m_pHdr_1 = &hdr[1];
|
header = &src[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */
|
/** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */
|
||||||
FORCEINLINE CHdr& Hdr()
|
FORCEINLINE BlobHeader& Hdr()
|
||||||
{
|
{
|
||||||
return *(m_pHdr_1 - 1);
|
return *(header - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** blob header accessor - use it rather than using the pointer arithmetics directly - const version */
|
/** blob header accessor - use it rather than using the pointer arithmetics directly - const version */
|
||||||
FORCEINLINE const CHdr& Hdr() const
|
FORCEINLINE const BlobHeader& Hdr() const
|
||||||
{
|
{
|
||||||
return *(m_pHdr_1 - 1);
|
return *(header - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** return reference to the actual blob size - used when the size needs to be modified */
|
/** return reference to the actual blob size - used when the size needs to be modified */
|
||||||
FORCEINLINE uint& RawSizeRef()
|
FORCEINLINE uint& RawSizeRef()
|
||||||
{
|
{
|
||||||
return Hdr().m_size;
|
return Hdr().items;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -129,25 +130,25 @@ public:
|
||||||
/** return the number of valid data bytes in the blob */
|
/** return the number of valid data bytes in the blob */
|
||||||
FORCEINLINE uint RawSize() const
|
FORCEINLINE uint RawSize() const
|
||||||
{
|
{
|
||||||
return Hdr().m_size;
|
return Hdr().items;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** return the current blob capacity in bytes */
|
/** return the current blob capacity in bytes */
|
||||||
FORCEINLINE uint MaxRawSize() const
|
FORCEINLINE uint MaxRawSize() const
|
||||||
{
|
{
|
||||||
return Hdr().m_max_size;
|
return Hdr().capacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** return pointer to the first byte of data - non-const version */
|
/** return pointer to the first byte of data - non-const version */
|
||||||
FORCEINLINE byte *RawData()
|
FORCEINLINE byte *RawData()
|
||||||
{
|
{
|
||||||
return m_pData;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** return pointer to the first byte of data - const version */
|
/** return pointer to the first byte of data - const version */
|
||||||
FORCEINLINE const byte *RawData() const
|
FORCEINLINE const byte *RawData() const
|
||||||
{
|
{
|
||||||
return m_pData;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** invalidate blob's data - doesn't free buffer */
|
/** invalidate blob's data - doesn't free buffer */
|
||||||
|
@ -175,7 +176,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */
|
/** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */
|
||||||
FORCEINLINE void AppendRaw(const CBlobBaseSimple& src)
|
FORCEINLINE void AppendRaw(const ByteBlob& src)
|
||||||
{
|
{
|
||||||
if (!src.IsEmpty())
|
if (!src.IsEmpty())
|
||||||
memcpy(GrowRawSize(src.RawSize()), src.RawData(), src.RawSize());
|
memcpy(GrowRawSize(src.RawSize()), src.RawData(), src.RawSize());
|
||||||
|
@ -187,7 +188,7 @@ public:
|
||||||
{
|
{
|
||||||
uint new_size = RawSize() + num_bytes;
|
uint new_size = RawSize() + num_bytes;
|
||||||
if (new_size > MaxRawSize()) SmartAlloc(new_size);
|
if (new_size > MaxRawSize()) SmartAlloc(new_size);
|
||||||
return m_pData + RawSize();
|
return data + RawSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Increase RawSize() by num_bytes.
|
/** Increase RawSize() by num_bytes.
|
||||||
|
@ -205,20 +206,20 @@ public:
|
||||||
uint old_max_size = MaxRawSize();
|
uint old_max_size = MaxRawSize();
|
||||||
if (old_max_size >= new_size) return;
|
if (old_max_size >= new_size) return;
|
||||||
/* calculate minimum block size we need to allocate */
|
/* calculate minimum block size we need to allocate */
|
||||||
uint min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve;
|
uint min_alloc_size = header_size + new_size + tail_reserve;
|
||||||
/* ask allocation policy for some reasonable block size */
|
/* ask allocation policy for some reasonable block size */
|
||||||
uint alloc_size = AllocPolicy(min_alloc_size);
|
uint alloc_size = AllocPolicy(min_alloc_size);
|
||||||
/* allocate new block */
|
/* allocate new block */
|
||||||
CHdr *pNewHdr = RawAlloc(alloc_size);
|
BlobHeader *tmp = RawAlloc(alloc_size);
|
||||||
/* setup header */
|
/* setup header */
|
||||||
pNewHdr->m_size = RawSize();
|
tmp->items = RawSize();
|
||||||
pNewHdr->m_max_size = alloc_size - (sizeof(CHdr) + Ttail_reserve);
|
tmp->capacity = alloc_size - (header_size + tail_reserve);
|
||||||
/* copy existing data */
|
/* copy existing data */
|
||||||
if (RawSize() > 0)
|
if (RawSize() > 0)
|
||||||
memcpy(pNewHdr + 1, m_pData, pNewHdr->m_size);
|
memcpy(tmp + 1, data, tmp->items);
|
||||||
/* replace our block with new one */
|
/* replace our block with new one */
|
||||||
CHdr *pOldHdr = &Hdr();
|
BlobHeader *pOldHdr = &Hdr();
|
||||||
Init(pNewHdr);
|
Init(tmp);
|
||||||
if (old_max_size > 0)
|
if (old_max_size > 0)
|
||||||
RawFree(pOldHdr);
|
RawFree(pOldHdr);
|
||||||
}
|
}
|
||||||
|
@ -243,16 +244,16 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** all allocation should happen here */
|
/** all allocation should happen here */
|
||||||
static FORCEINLINE CHdr *RawAlloc(uint num_bytes)
|
static FORCEINLINE BlobHeader *RawAlloc(uint num_bytes)
|
||||||
{
|
{
|
||||||
return (CHdr*)MallocT<byte>(num_bytes);
|
return (BlobHeader*)MallocT<byte>(num_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** all deallocations should happen here */
|
/** all deallocations should happen here */
|
||||||
static FORCEINLINE void RawFree(CHdr *p)
|
static FORCEINLINE void RawFree(BlobHeader *p)
|
||||||
{
|
{
|
||||||
/* Just to silence an unsilencable GCC 4.4+ warning. */
|
/* Just to silence an unsilencable GCC 4.4+ warning. */
|
||||||
assert(p != CBlobBaseSimple::hdrEmpty);
|
assert(p != ByteBlob::hdrEmpty);
|
||||||
|
|
||||||
/* In case GCC warns about the following, see GCC's PR38509 why it is bogus. */
|
/* In case GCC warns about the following, see GCC's PR38509 why it is bogus. */
|
||||||
free(p);
|
free(p);
|
||||||
|
@ -261,8 +262,8 @@ public:
|
||||||
FORCEINLINE void FixTail() const
|
FORCEINLINE void FixTail() const
|
||||||
{
|
{
|
||||||
if (MaxRawSize() > 0) {
|
if (MaxRawSize() > 0) {
|
||||||
byte *p = &m_pData[RawSize()];
|
byte *p = &data[RawSize()];
|
||||||
for (uint i = 0; i < Ttail_reserve; i++) {
|
for (uint i = 0; i < tail_reserve; i++) {
|
||||||
p[i] = 0;
|
p[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,18 +278,18 @@ public:
|
||||||
* 3. Takes care about the actual data size (number of used items).
|
* 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) */
|
* 4. Dynamically constructs only used items (as opposite of static array which constructs all items) */
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class CBlobT : public CBlobBaseSimple {
|
class CBlobT : public ByteBlob {
|
||||||
/* make template arguments public: */
|
/* make template arguments public: */
|
||||||
public:
|
public:
|
||||||
typedef CBlobBaseSimple base;
|
typedef ByteBlob base;
|
||||||
|
|
||||||
static const uint type_size = sizeof(T);
|
static const uint type_size = sizeof(T);
|
||||||
|
|
||||||
struct OnTransfer {
|
struct OnTransfer {
|
||||||
typename base::CHdr *m_pHdr_1;
|
typename base::BlobHeader *header;
|
||||||
OnTransfer(const OnTransfer& src) : m_pHdr_1(src.m_pHdr_1) {assert(src.m_pHdr_1 != NULL); *const_cast<typename base::CHdr**>(&src.m_pHdr_1) = NULL;}
|
OnTransfer(const OnTransfer& src) : header(src.header) {assert(src.header != NULL); *const_cast<typename base::BlobHeader**>(&src.header) = NULL;}
|
||||||
OnTransfer(CBlobT& src) : m_pHdr_1(src.m_pHdr_1) {src.InitEmpty();}
|
OnTransfer(CBlobT& src) : header(src.header) {src.InitEmpty();}
|
||||||
~OnTransfer() {assert(m_pHdr_1 == NULL);}
|
~OnTransfer() {assert(header == NULL);}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Default constructor - makes new Blob ready to accept any data */
|
/** Default constructor - makes new Blob ready to accept any data */
|
||||||
|
@ -298,7 +299,7 @@ public:
|
||||||
|
|
||||||
/** Take ownership constructor */
|
/** Take ownership constructor */
|
||||||
FORCEINLINE CBlobT(const OnTransfer& ot)
|
FORCEINLINE CBlobT(const OnTransfer& ot)
|
||||||
: base(ot.m_pHdr_1)
|
: base(ot.header)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/** Destructor - ensures that allocated memory (if any) is freed */
|
/** Destructor - ensures that allocated memory (if any) is freed */
|
||||||
|
@ -308,9 +309,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check the validity of item index (only in debug mode) */
|
/** Check the validity of item index (only in debug mode) */
|
||||||
FORCEINLINE void CheckIdx(uint idx) const
|
FORCEINLINE void CheckIdx(uint index) const
|
||||||
{
|
{
|
||||||
assert(idx < Size());
|
assert(index < Size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return pointer to the first data item - non-const version */
|
/** Return pointer to the first data item - non-const version */
|
||||||
|
@ -325,18 +326,18 @@ public:
|
||||||
return (const T*)base::RawData();
|
return (const T*)base::RawData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return pointer to the idx-th data item - non-const version */
|
/** Return pointer to the index-th data item - non-const version */
|
||||||
FORCEINLINE T *Data(uint idx)
|
FORCEINLINE T *Data(uint index)
|
||||||
{
|
{
|
||||||
CheckIdx(idx);
|
CheckIdx(index);
|
||||||
return (Data() + idx);
|
return (Data() + index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return pointer to the idx-th data item - const version */
|
/** Return pointer to the index-th data item - const version */
|
||||||
FORCEINLINE const T *Data(uint idx) const
|
FORCEINLINE const T *Data(uint index) const
|
||||||
{
|
{
|
||||||
CheckIdx(idx);
|
CheckIdx(index);
|
||||||
return (Data() + idx);
|
return (Data() + index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return number of items in the Blob */
|
/** Return number of items in the Blob */
|
||||||
|
|
|
@ -177,4 +177,4 @@ void DumpTarget::EndStruct()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Just to silence an unsilencable GCC 4.4+ warning */
|
/** Just to silence an unsilencable GCC 4.4+ warning */
|
||||||
/* static */ CBlobBaseSimple::CHdr CBlobBaseSimple::hdrEmpty[] = {{0, 0}, {0, 0}};
|
/* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};
|
||||||
|
|
Loading…
Reference in New Issue