(svn r19467) -Codechange: Use uint and byte direct instead of declaring internal types (skidd13)

-Codechange: remove now unneeded asserts
-Codechange: Set CBlobBaseSimple as absolute base class of CBlobT
pull/155/head
yexo 15 years ago
parent 33a9deca6c
commit 75c520cd08

@ -46,20 +46,16 @@
* - 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 CBlobBaseSimple {
public:
typedef ::ptrdiff_t bsize_t;
typedef ::byte bitem_t;
protected: protected:
/** header of the allocated memory block */ /** header of the allocated memory block */
struct CHdr { struct CHdr {
bsize_t m_size; ///< actual blob size in bytes uint m_size; ///< actual blob size in bytes
bsize_t m_max_size; ///< maximum (allocated) size in bytes uint m_max_size; ///< maximum (allocated) size in bytes
}; };
/** type used as class member */ /** type used as class member */
union { union {
bitem_t *m_pData; ///< ptr to the first byte of data byte *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 CHdr *m_pHdr_1; ///< ptr just after the CHdr holding m_size and m_max_size
} ptr_u; } ptr_u;
@ -72,12 +68,12 @@ private:
static CHdr hdrEmpty[]; static CHdr hdrEmpty[];
public: public:
static const bsize_t Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end static const uint Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end
/** default constructor - initializes empty blob */ /** default constructor - initializes empty blob */
FORCEINLINE CBlobBaseSimple() { InitEmpty(); } FORCEINLINE CBlobBaseSimple() { InitEmpty(); }
/** constructor - create blob with data */ /** constructor - create blob with data */
FORCEINLINE CBlobBaseSimple(const bitem_t *p, bsize_t num_bytes) FORCEINLINE CBlobBaseSimple(const byte *p, uint num_bytes)
{ {
InitEmpty(); InitEmpty();
AppendRaw(p, num_bytes); AppendRaw(p, num_bytes);
@ -131,7 +127,7 @@ protected:
} }
/** 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 bsize_t& RawSizeRef() FORCEINLINE uint& RawSizeRef()
{ {
return Hdr().m_size; return Hdr().m_size;
}; };
@ -144,31 +140,31 @@ public:
} }
/** return the number of valid data bytes in the blob */ /** return the number of valid data bytes in the blob */
FORCEINLINE bsize_t RawSize() const FORCEINLINE uint RawSize() const
{ {
return Hdr().m_size; return Hdr().m_size;
}; };
/** return the current blob capacity in bytes */ /** return the current blob capacity in bytes */
FORCEINLINE bsize_t MaxRawSize() const FORCEINLINE uint MaxRawSize() const
{ {
return Hdr().m_max_size; return Hdr().m_max_size;
}; };
/** return pointer to the first byte of data - non-const version */ /** return pointer to the first byte of data - non-const version */
FORCEINLINE bitem_t *RawData() FORCEINLINE byte *RawData()
{ {
return ptr_u.m_pData; return ptr_u.m_pData;
} }
/** return pointer to the first byte of data - const version */ /** return pointer to the first byte of data - const version */
FORCEINLINE const bitem_t *RawData() const FORCEINLINE const byte *RawData() const
{ {
return ptr_u.m_pData; return ptr_u.m_pData;
} }
/** return the 32 bit CRC of valid data in the blob */ /** return the 32 bit CRC of valid data in the blob */
//FORCEINLINE bsize_t Crc32() const //FORCEINLINE uint Crc32() const
//{ //{
// return CCrc32::Calc(RawData(), RawSize()); // return CCrc32::Calc(RawData(), RawSize());
//} //}
@ -206,18 +202,16 @@ public:
/** swap buffers (with data) between two blobs (this and source blob) */ /** swap buffers (with data) between two blobs (this and source blob) */
FORCEINLINE void Swap(CBlobBaseSimple& src) FORCEINLINE void Swap(CBlobBaseSimple& src)
{ {
bitem_t *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData; byte *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData;
src.ptr_u.m_pData = tmp; src.ptr_u.m_pData = tmp;
} }
/** append new bytes at the end of existing data bytes - reallocates if necessary */ /** append new bytes at the end of existing data bytes - reallocates if necessary */
FORCEINLINE void AppendRaw(const void *p, bsize_t num_bytes) FORCEINLINE void AppendRaw(const void *p, uint num_bytes)
{ {
assert(p != NULL); assert(p != NULL);
if (num_bytes > 0) { if (num_bytes > 0) {
memcpy(GrowRawSize(num_bytes), p, num_bytes); memcpy(GrowRawSize(num_bytes), p, num_bytes);
} else {
assert(num_bytes >= 0);
} }
} }
@ -230,25 +224,24 @@ public:
/** Reallocate if there is no free space for num_bytes bytes. /** Reallocate if there is no free space for num_bytes bytes.
* @return pointer to the new data to be added */ * @return pointer to the new data to be added */
FORCEINLINE bitem_t *MakeRawFreeSpace(bsize_t num_bytes) FORCEINLINE byte *MakeRawFreeSpace(uint num_bytes)
{ {
assert(num_bytes >= 0); uint new_size = RawSize() + num_bytes;
bsize_t new_size = RawSize() + num_bytes;
if (new_size > MaxRawSize()) SmartAlloc(new_size); if (new_size > MaxRawSize()) SmartAlloc(new_size);
return ptr_u.m_pData + RawSize(); return ptr_u.m_pData + RawSize();
} }
/** Increase RawSize() by num_bytes. /** Increase RawSize() by num_bytes.
* @return pointer to the new data added */ * @return pointer to the new data added */
FORCEINLINE bitem_t *GrowRawSize(bsize_t num_bytes) FORCEINLINE byte *GrowRawSize(uint num_bytes)
{ {
bitem_t *pNewData = MakeRawFreeSpace(num_bytes); byte *pNewData = MakeRawFreeSpace(num_bytes);
RawSizeRef() += num_bytes; RawSizeRef() += num_bytes;
return pNewData; return pNewData;
} }
/** Decrease RawSize() by num_bytes. */ /** Decrease RawSize() by num_bytes. */
FORCEINLINE void ReduceRawSize(bsize_t num_bytes) FORCEINLINE void ReduceRawSize(uint num_bytes)
{ {
if (MaxRawSize() > 0 && num_bytes > 0) { if (MaxRawSize() > 0 && num_bytes > 0) {
assert(num_bytes <= RawSize()); assert(num_bytes <= RawSize());
@ -261,14 +254,14 @@ public:
} }
/** reallocate blob data if needed */ /** reallocate blob data if needed */
void SmartAlloc(bsize_t new_size) void SmartAlloc(uint new_size)
{ {
bsize_t 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 */
bsize_t min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve; uint min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve;
/* ask allocation policy for some reasonable block size */ /* ask allocation policy for some reasonable block size */
bsize_t alloc_size = AllocPolicy(min_alloc_size); uint alloc_size = AllocPolicy(min_alloc_size);
/* allocate new block */ /* allocate new block */
CHdr *pNewHdr = RawAlloc(alloc_size); CHdr *pNewHdr = RawAlloc(alloc_size);
/* setup header */ /* setup header */
@ -285,7 +278,7 @@ public:
} }
/** simple allocation policy - can be optimized later */ /** simple allocation policy - can be optimized later */
FORCEINLINE static bsize_t AllocPolicy(bsize_t min_alloc) FORCEINLINE static uint AllocPolicy(uint min_alloc)
{ {
if (min_alloc < (1 << 9)) { if (min_alloc < (1 << 9)) {
if (min_alloc < (1 << 5)) return (1 << 5); if (min_alloc < (1 << 5)) return (1 << 5);
@ -304,7 +297,7 @@ public:
} }
/** all allocation should happen here */ /** all allocation should happen here */
static FORCEINLINE CHdr *RawAlloc(bsize_t num_bytes) static FORCEINLINE CHdr *RawAlloc(uint num_bytes)
{ {
return (CHdr*)MallocT<byte>(num_bytes); return (CHdr*)MallocT<byte>(num_bytes);
} }
@ -322,58 +315,56 @@ public:
FORCEINLINE void FixTail() const FORCEINLINE void FixTail() const
{ {
if (MaxRawSize() > 0) { if (MaxRawSize() > 0) {
bitem_t *p = &ptr_u.m_pData[RawSize()]; byte *p = &ptr_u.m_pData[RawSize()];
for (bsize_t i = 0; i < Ttail_reserve; i++) { for (uint i = 0; i < Ttail_reserve; i++) {
p[i] = 0; p[i] = 0;
} }
} }
} }
}; };
/** Blob - simple dynamic Titem_ array. Titem_ (template argument) is a placeholder for any type. /** Blob - simple dynamic T array. T (template argument) is a placeholder for any type.
* Titem_ can be any integral type, pointer, or structure. Using Blob instead of just plain C array * T can be any integral type, pointer, or structure. Using Blob instead of just plain C array
* simplifies the resource management in several ways: * simplifies the resource management in several ways:
* 1. When adding new item(s) it automatically grows capacity if needed. * 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. * 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). * 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 <class Titem_, class Tbase_ = CBlobBaseSimple> template <typename T>
class CBlobT : public Tbase_ { class CBlobT : public CBlobBaseSimple {
/* make template arguments public: */ /* make template arguments public: */
public: public:
typedef Titem_ Titem; typedef CBlobBaseSimple base;
typedef Tbase_ Tbase;
typedef typename Tbase::bsize_t bsize_t;
static const bsize_t Titem_size = sizeof(Titem); static const uint type_size = sizeof(T);
struct OnTransfer { struct OnTransfer {
typename Tbase_::CHdr *m_pHdr_1; typename base::CHdr *m_pHdr_1;
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;} 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(CBlobT& src) : m_pHdr_1(src.ptr_u.m_pHdr_1) {src.InitEmpty();} OnTransfer(CBlobT& src) : m_pHdr_1(src.ptr_u.m_pHdr_1) {src.InitEmpty();}
~OnTransfer() {assert(m_pHdr_1 == NULL);} ~OnTransfer() {assert(m_pHdr_1 == NULL);}
}; };
/** Default constructor - makes new Blob ready to accept any data */ /** Default constructor - makes new Blob ready to accept any data */
FORCEINLINE CBlobT() FORCEINLINE CBlobT()
: Tbase() : base()
{} {}
/** Constructor - makes new Blob with data */ /** Constructor - makes new Blob with data */
FORCEINLINE CBlobT(const Titem_ *p, bsize_t num_items) FORCEINLINE CBlobT(const T *p, uint num_items)
: Tbase((typename Tbase_::bitem_t*)p, num_items * Titem_size) : base((byte *)p, num_items * type_size)
{} {}
/** Copy constructor - make new blob to become copy of the original (source) blob */ /** Copy constructor - make new blob to become copy of the original (source) blob */
FORCEINLINE CBlobT(const Tbase& src) FORCEINLINE CBlobT(const base& src)
: Tbase(src) : base(src)
{ {
assert((Tbase::RawSize() % Titem_size) == 0); assert((base::RawSize() % type_size) == 0);
} }
/** Take ownership constructor */ /** Take ownership constructor */
FORCEINLINE CBlobT(const OnTransfer& ot) FORCEINLINE CBlobT(const OnTransfer& ot)
: Tbase(ot.m_pHdr_1) : base(ot.m_pHdr_1)
{} {}
/** Destructor - ensures that allocated memory (if any) is freed */ /** Destructor - ensures that allocated memory (if any) is freed */
@ -383,150 +374,150 @@ public:
} }
/** Check the validity of item index (only in debug mode) */ /** Check the validity of item index (only in debug mode) */
FORCEINLINE void CheckIdx(bsize_t idx) const FORCEINLINE void CheckIdx(uint idx) const
{ {
assert(idx >= 0); assert(idx < Size()); assert(idx < Size());
} }
/** Return pointer to the first data item - non-const version */ /** Return pointer to the first data item - non-const version */
FORCEINLINE Titem *Data() FORCEINLINE T *Data()
{ {
return (Titem*)Tbase::RawData(); return (T*)base::RawData();
} }
/** Return pointer to the first data item - const version */ /** Return pointer to the first data item - const version */
FORCEINLINE const Titem *Data() const FORCEINLINE const T *Data() const
{ {
return (const Titem*)Tbase::RawData(); return (const T*)base::RawData();
} }
/** Return pointer to the idx-th data item - non-const version */ /** Return pointer to the idx-th data item - non-const version */
FORCEINLINE Titem *Data(bsize_t idx) FORCEINLINE T *Data(uint idx)
{ {
CheckIdx(idx); CheckIdx(idx);
return (Data() + idx); return (Data() + idx);
} }
/** Return pointer to the idx-th data item - const version */ /** Return pointer to the idx-th data item - const version */
FORCEINLINE const Titem *Data(bsize_t idx) const FORCEINLINE const T *Data(uint idx) const
{ {
CheckIdx(idx); CheckIdx(idx);
return (Data() + idx); return (Data() + idx);
} }
/** Return number of items in the Blob */ /** Return number of items in the Blob */
FORCEINLINE bsize_t Size() const FORCEINLINE uint Size() const
{ {
return (Tbase::RawSize() / Titem_size); return (base::RawSize() / type_size);
} }
/** Return total number of items that can fit in the Blob without buffer reallocation */ /** Return total number of items that can fit in the Blob without buffer reallocation */
FORCEINLINE bsize_t MaxSize() const FORCEINLINE uint MaxSize() const
{ {
return (Tbase::MaxRawSize() / Titem_size); return (base::MaxRawSize() / type_size);
} }
/** Return number of additional items that can fit in the Blob without buffer reallocation */ /** Return number of additional items that can fit in the Blob without buffer reallocation */
FORCEINLINE bsize_t GetReserve() const FORCEINLINE uint GetReserve() const
{ {
return ((Tbase::MaxRawSize() - Tbase::RawSize()) / Titem_size); return ((base::MaxRawSize() - base::RawSize()) / type_size);
} }
/** Free the memory occupied by Blob destroying all items */ /** Free the memory occupied by Blob destroying all items */
FORCEINLINE void Free() FORCEINLINE void Free()
{ {
assert((Tbase::RawSize() % Titem_size) == 0); assert((base::RawSize() % type_size) == 0);
bsize_t old_size = Size(); uint old_size = Size();
if (old_size > 0) { if (old_size > 0) {
/* destroy removed items; */ /* destroy removed items; */
Titem *pI_last_to_destroy = Data(0); T *pI_last_to_destroy = Data(0);
for (Titem *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem_(); for (T *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~T();
} }
Tbase::Free(); base::Free();
} }
/** Grow number of data items in Blob by given number - doesn't construct items */ /** Grow number of data items in Blob by given number - doesn't construct items */
FORCEINLINE Titem *GrowSizeNC(bsize_t num_items) FORCEINLINE T *GrowSizeNC(uint num_items)
{ {
return (Titem*)Tbase::GrowRawSize(num_items * Titem_size); return (T*)base::GrowRawSize(num_items * type_size);
} }
/** Grow number of data items in Blob by given number - constructs new items (using Titem_'s default constructor) */ /** Grow number of data items in Blob by given number - constructs new items (using T's default constructor) */
FORCEINLINE Titem *GrowSizeC(bsize_t num_items) FORCEINLINE T *GrowSizeC(uint num_items)
{ {
Titem *pI = GrowSizeNC(num_items); T *pI = GrowSizeNC(num_items);
for (bsize_t i = num_items; i > 0; i--, pI++) new (pI) Titem(); for (uint i = num_items; i > 0; i--, pI++) new (pI) T();
} }
/** Destroy given number of items and reduce the Blob's data size */ /** Destroy given number of items and reduce the Blob's data size */
FORCEINLINE void ReduceSize(bsize_t num_items) FORCEINLINE void ReduceSize(uint num_items)
{ {
assert((Tbase::RawSize() % Titem_size) == 0); assert((base::RawSize() % type_size) == 0);
bsize_t old_size = Size(); uint old_size = Size();
assert(num_items <= old_size); assert(num_items <= old_size);
bsize_t new_size = (num_items <= old_size) ? (old_size - num_items) : 0; uint new_size = (num_items <= old_size) ? (old_size - num_items) : 0;
/* destroy removed items; */ /* destroy removed items; */
Titem *pI_last_to_destroy = Data(new_size); T *pI_last_to_destroy = Data(new_size);
for (Titem *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem(); for (T *pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~T();
/* remove them */ /* remove them */
Tbase::ReduceRawSize(num_items * Titem_size); base::ReduceRawSize(num_items * type_size);
} }
/** Append one data item at the end (calls Titem_'s default constructor) */ /** Append one data item at the end (calls T's default constructor) */
FORCEINLINE Titem *AppendNew() FORCEINLINE T *AppendNew()
{ {
Titem& dst = *GrowSizeNC(1); // Grow size by one item T& dst = *GrowSizeNC(1); // Grow size by one item
Titem *pNewItem = new (&dst) Titem(); // construct the new item by calling in-place new operator T *pNewItem = new (&dst) T(); // construct the new item by calling in-place new operator
return pNewItem; return pNewItem;
} }
/** Append the copy of given item at the end of Blob (using copy constructor) */ /** Append the copy of given item at the end of Blob (using copy constructor) */
FORCEINLINE Titem *Append(const Titem& src) FORCEINLINE T *Append(const T& src)
{ {
Titem& dst = *GrowSizeNC(1); // Grow size by one item T& dst = *GrowSizeNC(1); // Grow size by one item
Titem *pNewItem = new (&dst) Titem(src); // construct the new item by calling in-place new operator with copy ctor() T *pNewItem = new (&dst) T(src); // construct the new item by calling in-place new operator with copy ctor()
return pNewItem; return pNewItem;
} }
/** Add given items (ptr + number of items) at the end of blob */ /** Add given items (ptr + number of items) at the end of blob */
FORCEINLINE Titem *Append(const Titem *pSrc, bsize_t num_items) FORCEINLINE T *Append(const T *pSrc, uint num_items)
{ {
Titem *pDst = GrowSizeNC(num_items); T *pDst = GrowSizeNC(num_items);
Titem *pDstOrg = pDst; T *pDstOrg = pDst;
Titem *pDstEnd = pDst + num_items; T *pDstEnd = pDst + num_items;
while (pDst < pDstEnd) new (pDst++) Titem(*(pSrc++)); while (pDst < pDstEnd) new (pDst++) T(*(pSrc++));
return pDstOrg; return pDstOrg;
} }
/** Remove item with the given index by replacing it by the last item and reducing the size by one */ /** Remove item with the given index by replacing it by the last item and reducing the size by one */
FORCEINLINE void RemoveBySwap(bsize_t idx) FORCEINLINE void RemoveBySwap(uint idx)
{ {
CheckIdx(idx); CheckIdx(idx);
/* destroy removed item */ /* destroy removed item */
Titem *pRemoved = Data(idx); T *pRemoved = Data(idx);
RemoveBySwap(pRemoved); RemoveBySwap(pRemoved);
} }
/** Remove item given by pointer replacing it by the last item and reducing the size by one */ /** Remove item given by pointer replacing it by the last item and reducing the size by one */
FORCEINLINE void RemoveBySwap(Titem *pItem) FORCEINLINE void RemoveBySwap(T *pItem)
{ {
Titem *pLast = Data(Size() - 1); T *pLast = Data(Size() - 1);
assert(pItem >= Data() && pItem <= pLast); assert(pItem >= Data() && pItem <= pLast);
/* move last item to its new place */ /* move last item to its new place */
if (pItem != pLast) { if (pItem != pLast) {
pItem->~Titem_(); pItem->~T();
new (pItem) Titem_(*pLast); new (pItem) T(*pLast);
} }
/* destroy the last item */ /* destroy the last item */
pLast->~Titem_(); pLast->~T();
/* and reduce the raw blob size */ /* and reduce the raw blob size */
Tbase::ReduceRawSize(Titem_size); base::ReduceRawSize(type_size);
} }
/** Ensures that given number of items can be added to the end of Blob. Returns pointer to the /** Ensures that given number of items can be added to the end of Blob. Returns pointer to the
* first free (unused) item */ * first free (unused) item */
FORCEINLINE Titem *MakeFreeSpace(bsize_t num_items) FORCEINLINE T *MakeFreeSpace(uint num_items)
{ {
return (Titem*)Tbase::MakeRawFreeSpace(num_items * Titem_size); return (T*)base::MakeRawFreeSpace(num_items * type_size);
} }
FORCEINLINE OnTransfer Transfer() FORCEINLINE OnTransfer Transfer()

@ -40,7 +40,7 @@ struct CStrA : public CBlobT<char>
} }
/** Grow the actual buffer and fix the trailing zero at the end. */ /** Grow the actual buffer and fix the trailing zero at the end. */
FORCEINLINE char *GrowSizeNC(bsize_t count) FORCEINLINE char *GrowSizeNC(uint count)
{ {
char *ret = base::GrowSizeNC(count); char *ret = base::GrowSizeNC(count);
base::FixTail(); base::FixTail();
@ -93,14 +93,14 @@ struct CStrA : public CBlobT<char>
/** Add formated string (like vsprintf) at the end of existing contents. */ /** Add formated string (like vsprintf) at the end of existing contents. */
int AddFormatL(const char *format, va_list args) int AddFormatL(const char *format, va_list args)
{ {
bsize_t addSize = max<size_t>(strlen(format), 16); uint addSize = max<uint>(strlen(format), 16);
addSize += addSize / 2; addSize += addSize / 2;
int ret; int ret;
int err = 0; int err = 0;
for (;;) { for (;;) {
char *buf = MakeFreeSpace(addSize); char *buf = MakeFreeSpace(addSize);
ret = vsnprintf(buf, base::GetReserve(), format, args); ret = vsnprintf(buf, base::GetReserve(), format, args);
if (ret >= base::GetReserve()) { if (ret >= (int)base::GetReserve()) {
/* Greater return than given count means needed buffer size. */ /* Greater return than given count means needed buffer size. */
addSize = ret + 1; addSize = ret + 1;
continue; continue;

Loading…
Cancel
Save