KUDr@5633: /* $Id$ */ KUDr@5633: belugas@6481: /** @file blob.hpp */ belugas@6481: KUDr@5633: #ifndef BLOB_HPP KUDr@5633: #define BLOB_HPP KUDr@5633: KUDr@5633: /** Type-safe version of memcpy(). KUDr@5633: * @param d destination buffer KUDr@5633: * @param s source buffer KUDr@5633: * @param num_items number of items to be copied (!not number of bytes!) */ KUDr@5633: template KUDr@5633: FORCEINLINE void MemCpyT(Titem_* d, const Titem_* s, int num_items = 1) KUDr@5633: { KUDr@5633: memcpy(d, s, num_items * sizeof(Titem_)); KUDr@5633: } KUDr@5633: KUDr@5633: KUDr@5633: /** Base class for simple binary blobs. KUDr@5633: * Item is byte. KUDr@5633: * The word 'simple' means: KUDr@5633: * - no configurable allocator type (always made from heap) KUDr@5633: * - no smart deallocation - deallocation must be called from the same KUDr@5633: * module (DLL) where the blob was allocated KUDr@5633: * - no configurable allocation policy (how big blocks should be allocated) KUDr@5633: * - no extra ownership policy (i.e. 'copy on write') when blob is copied KUDr@5633: * - no thread synchronization at all KUDr@5633: * KUDr@5633: * Internal member layout: KUDr@5633: * 1. The only class member is pointer to the first item (see union ptr_u). KUDr@5633: * 2. Allocated block contains the blob header (see CHdr) followed by the raw byte data. KUDr@5633: * Always, when it allocates memory the allocated size is: KUDr@5633: * sizeof(CHdr) + KUDr@5633: * 3. Two 'virtual' members (m_size and m_max_size) are stored in the CHdr at beginning KUDr@5633: * of the alloated block. KUDr@5633: * 4. The pointer (in ptr_u) points behind the header (to the first data byte). KUDr@5633: * When memory block is allocated, the sizeof(CHdr) it added to it. KUDr@5633: * 5. Benefits of this layout: KUDr@5633: * - items are accessed in the simplest possible way - just dereferencing the pointer, KUDr@5633: * which is good for performance (assuming that data are accessed most often). KUDr@5633: * - sizeof(blob) is the same as the size of any other pointer KUDr@5633: * 6. Drawbacks of this layout: KUDr@5633: * - the fact, that pointer to the alocated block is adjusted by sizeof(CHdr) before KUDr@5633: * it is stored can lead to several confusions: KUDr@5633: * - it is not common pattern so the implementation code is bit harder to read KUDr@5633: * - valgrind can generate warning that allocated block is lost (not accessible) KUDr@5633: * */ KUDr@5633: class CBlobBaseSimple { KUDr@5633: protected: KUDr@5633: /** header of the allocated memory block */ KUDr@5633: struct CHdr { KUDr@5633: int m_size; ///< actual blob size in bytes KUDr@5633: int m_max_size; ///< maximum (allocated) size in bytes KUDr@5633: }; KUDr@5633: KUDr@5633: /** type used as class member */ KUDr@5633: union { KUDr@5633: int8 *m_pData; ///< pointer to the first byte of data KUDr@5633: CHdr *m_pHdr_1; ///< pointer just after the CHdr holding m_size and m_max_size KUDr@5633: } ptr_u; KUDr@5633: KUDr@5633: public: KUDr@5633: static const int Ttail_reserve = 4; ///< four extra bytes will be always allocated and zeroed at the end KUDr@5633: KUDr@5633: /** default constructor - initializes empty blob */ KUDr@5633: FORCEINLINE CBlobBaseSimple() { InitEmpty(); } KUDr@5633: /** copy constructor */ KUDr@5633: FORCEINLINE CBlobBaseSimple(const CBlobBaseSimple& src) KUDr@5633: { KUDr@5633: InitEmpty(); KUDr@5633: AppendRaw(src); KUDr@5633: } KUDr@5633: /** destructor */ KUDr@5633: FORCEINLINE ~CBlobBaseSimple() { Free(); } KUDr@5633: protected: KUDr@5633: /** initialize the empty blob by setting the ptr_u.m_pHdr_1 pointer to the static CHdr with KUDr@5633: * both m_size and m_max_size containing zero */ KUDr@5633: FORCEINLINE void InitEmpty() { static CHdr hdrEmpty[] = {{0, 0}, {0, 0}}; ptr_u.m_pHdr_1 = &hdrEmpty[1]; } KUDr@5633: /** initialize blob by attaching it to the given header followed by data */ KUDr@5633: FORCEINLINE void Init(CHdr* hdr) { ptr_u.m_pHdr_1 = &hdr[1]; } KUDr@5633: /** blob header accessor - use it rather than using the pointer arithmetics directly - non-const version */ KUDr@5633: FORCEINLINE CHdr& Hdr() { return ptr_u.m_pHdr_1[-1]; } KUDr@5633: /** blob header accessor - use it rather than using the pointer arithmetics directly - const version */ KUDr@5633: FORCEINLINE const CHdr& Hdr() const { return ptr_u.m_pHdr_1[-1]; } KUDr@5633: /** return reference to the actual blob size - used when the size needs to be modified */ KUDr@5633: FORCEINLINE int& RawSizeRef() { return Hdr().m_size; }; KUDr@5633: KUDr@5633: public: KUDr@5633: /** return true if blob doesn't contain valid data */ KUDr@5633: FORCEINLINE bool IsEmpty() const { return RawSize() == 0; } KUDr@5633: /** return the number of valid data bytes in the blob */ KUDr@5633: FORCEINLINE int RawSize() const { return Hdr().m_size; }; KUDr@5633: /** return the current blob capacity in bytes */ KUDr@5633: FORCEINLINE int MaxRawSize() const { return Hdr().m_max_size; }; KUDr@5633: /** return pointer to the first byte of data - non-const version */ KUDr@5633: FORCEINLINE int8* RawData() { return ptr_u.m_pData; } KUDr@5633: /** return pointer to the first byte of data - const version */ KUDr@5633: FORCEINLINE const int8* RawData() const { return ptr_u.m_pData; } KUDr@5633: #if 0 // reenable when needed KUDr@5633: /** return the 32 bit CRC of valid data in the blob */ KUDr@5633: FORCEINLINE uint32 Crc32() const {return CCrc32::Calc(RawData(), RawSize());} KUDr@5633: #endif //0 KUDr@5633: /** invalidate blob's data - doesn't free buffer */ KUDr@5633: FORCEINLINE void Clear() { RawSizeRef() = 0; } KUDr@5633: /** free the blob's memory */ KUDr@5633: FORCEINLINE void Free() { if (MaxRawSize() > 0) {RawFree(&Hdr()); InitEmpty();} } KUDr@5633: /** copy data from another blob - replaces any existing blob's data */ KUDr@5633: FORCEINLINE void CopyFrom(const CBlobBaseSimple& src) { Clear(); AppendRaw(src); } KUDr@5633: /** overtake ownership of data buffer from the source blob - source blob will become empty */ KUDr@5633: FORCEINLINE void MoveFrom(CBlobBaseSimple& src) { Free(); ptr_u.m_pData = src.ptr_u.m_pData; src.InitEmpty(); } KUDr@5633: /** swap buffers (with data) between two blobs (this and source blob) */ KUDr@5633: FORCEINLINE void Swap(CBlobBaseSimple& src) { int8 *tmp = ptr_u.m_pData; ptr_u.m_pData = src.ptr_u.m_pData; src.ptr_u.m_pData = tmp; } KUDr@5633: KUDr@5633: /** append new bytes at the end of existing data bytes - reallocates if necessary */ KUDr@5633: FORCEINLINE void AppendRaw(int8 *p, int num_bytes) KUDr@5633: { KUDr@5633: assert(p != NULL); KUDr@5633: if (num_bytes > 0) { KUDr@5633: memcpy(GrowRawSize(num_bytes), p, num_bytes); KUDr@5633: } else { KUDr@5633: assert(num_bytes >= 0); KUDr@5633: } KUDr@5633: } KUDr@5633: KUDr@5633: /** append bytes from given source blob to the end of existing data bytes - reallocates if necessary */ KUDr@5633: FORCEINLINE void AppendRaw(const CBlobBaseSimple& src) KUDr@5633: { KUDr@5633: if (!src.IsEmpty()) KUDr@5633: memcpy(GrowRawSize(src.RawSize()), src.RawData(), src.RawSize()); KUDr@5633: } KUDr@5633: KUDr@5633: /** Reallocate if there is no free space for num_bytes bytes. KUDr@5633: * @return pointer to the new data to be added */ KUDr@5633: FORCEINLINE int8* MakeRawFreeSpace(int num_bytes) KUDr@5633: { KUDr@5633: assert(num_bytes >= 0); KUDr@5633: int new_size = RawSize() + num_bytes; KUDr@5633: if (new_size > MaxRawSize()) SmartAlloc(new_size); KUDr@5633: FixTail(); KUDr@5633: return ptr_u.m_pData + RawSize(); KUDr@5633: } KUDr@5633: KUDr@5633: /** Increase RawSize() by num_bytes. KUDr@5633: * @return pointer to the new data added */ KUDr@5633: FORCEINLINE int8* GrowRawSize(int num_bytes) KUDr@5633: { KUDr@5633: int8* pNewData = MakeRawFreeSpace(num_bytes); KUDr@5633: RawSizeRef() += num_bytes; KUDr@5633: return pNewData; KUDr@5633: } KUDr@5633: KUDr@5633: /** Decrease RawSize() by num_bytes. */ KUDr@5633: FORCEINLINE void ReduceRawSize(int num_bytes) KUDr@5633: { KUDr@5633: if (MaxRawSize() > 0 && num_bytes > 0) { KUDr@5633: assert(num_bytes <= RawSize()); KUDr@5633: if (num_bytes < RawSize()) RawSizeRef() -= num_bytes; KUDr@5633: else RawSizeRef() = 0; KUDr@5633: } KUDr@5633: } KUDr@5633: /** reallocate blob data if needed */ KUDr@5633: void SmartAlloc(int new_size) KUDr@5633: { KUDr@5633: int old_max_size = MaxRawSize(); KUDr@5633: if (old_max_size >= new_size) return; KUDr@5633: // calculate minimum block size we need to allocate KUDr@5633: int min_alloc_size = sizeof(CHdr) + new_size + Ttail_reserve; KUDr@5633: // ask allocation policy for some reasonable block size KUDr@5633: int alloc_size = AllocPolicy(min_alloc_size); KUDr@5633: // allocate new block KUDr@5633: CHdr* pNewHdr = RawAlloc(alloc_size); KUDr@5633: // setup header KUDr@5633: pNewHdr->m_size = RawSize(); KUDr@5633: pNewHdr->m_max_size = alloc_size - (sizeof(CHdr) + Ttail_reserve); KUDr@5633: // copy existing data KUDr@5633: if (RawSize() > 0) KUDr@5633: memcpy(pNewHdr + 1, ptr_u.m_pData, pNewHdr->m_size); KUDr@5633: // replace our block with new one KUDr@5633: CHdr* pOldHdr = &Hdr(); KUDr@5633: Init(pNewHdr); KUDr@5633: if (old_max_size > 0) KUDr@5633: RawFree(pOldHdr); KUDr@5633: } KUDr@5633: /** simple allocation policy - can be optimized later */ KUDr@5633: FORCEINLINE static int AllocPolicy(int min_alloc) KUDr@5633: { KUDr@5633: if (min_alloc < (1 << 9)) { KUDr@5633: if (min_alloc < (1 << 5)) return (1 << 5); KUDr@5633: return (min_alloc < (1 << 7)) ? (1 << 7) : (1 << 9); KUDr@5633: } KUDr@5633: if (min_alloc < (1 << 15)) { KUDr@5633: if (min_alloc < (1 << 11)) return (1 << 11); KUDr@5633: return (min_alloc < (1 << 13)) ? (1 << 13) : (1 << 15); KUDr@5633: } KUDr@5633: if (min_alloc < (1 << 20)) { KUDr@5633: if (min_alloc < (1 << 17)) return (1 << 17); KUDr@5633: return (min_alloc < (1 << 19)) ? (1 << 19) : (1 << 20); KUDr@5633: } KUDr@5633: min_alloc = (min_alloc | ((1 << 20) - 1)) + 1; KUDr@5633: return min_alloc; KUDr@5633: } KUDr@5633: KUDr@5633: /** all allocation should happen here */ KUDr@5633: static FORCEINLINE CHdr* RawAlloc(int num_bytes) { return (CHdr*)malloc(num_bytes); } KUDr@5633: /** all deallocations should happen here */ KUDr@5633: static FORCEINLINE void RawFree(CHdr* p) { free(p); } KUDr@5633: /** fixing the four bytes at the end of blob data - useful when blob is used to hold string */ KUDr@5633: FORCEINLINE void FixTail() KUDr@5633: { KUDr@5633: if (MaxRawSize() > 0) { KUDr@5633: int8 *p = &ptr_u.m_pData[RawSize()]; KUDr@5633: for (int i = 0; i < Ttail_reserve; i++) p[i] = 0; KUDr@5633: } KUDr@5633: } KUDr@5633: }; KUDr@5633: KUDr@5633: /** Blob - simple dynamic Titem_ array. Titem_ (template argument) is a placeholder for any type. KUDr@5633: * Titem_ can be any integral type, pointer, or structure. Using Blob instead of just plain C array KUDr@5633: * simplifies the resource management in several ways: KUDr@5633: * 1. When adding new item(s) it automatically grows capacity if needed. KUDr@5633: * 2. When variable of type Blob comes out of scope it automatically frees the data buffer. KUDr@5633: * 3. Takes care about the actual data size (number of used items). KUDr@5633: * 4. Dynamically constructs only used items (as opposite of static array which constructs all items) */ KUDr@5633: template KUDr@5633: class CBlobT : public CBlobBaseSimple { KUDr@5633: // make template arguments public: KUDr@5633: public: KUDr@5633: typedef Titem_ Titem; KUDr@5633: typedef Tbase_ Tbase; KUDr@5633: KUDr@5633: static const int Titem_size = sizeof(Titem); KUDr@5633: KUDr@5633: /** Default constructor - makes new Blob ready to accept any data */ KUDr@5633: FORCEINLINE CBlobT() : Tbase() {} KUDr@5633: /** Copy constructor - make new blob to become copy of the original (source) blob */ KUDr@5633: FORCEINLINE CBlobT(const Tbase& src) : Tbase(src) {assert((RawSize() % Titem_size) == 0);} KUDr@5633: /** Destructor - ensures that allocated memory (if any) is freed */ KUDr@5633: FORCEINLINE ~CBlobT() { Free(); } KUDr@5633: /** Check the validity of item index (only in debug mode) */ KUDr@5633: FORCEINLINE void CheckIdx(int idx) { assert(idx >= 0); assert(idx < Size()); } KUDr@5633: /** Return pointer to the first data item - non-const version */ KUDr@5633: FORCEINLINE Titem* Data() { return (Titem*)RawData(); } KUDr@5633: /** Return pointer to the first data item - const version */ KUDr@5633: FORCEINLINE const Titem* Data() const { return (const Titem*)RawData(); } KUDr@5633: /** Return pointer to the idx-th data item - non-const version */ KUDr@5633: FORCEINLINE Titem* Data(int idx) { CheckIdx(idx); return (Data() + idx); } KUDr@5633: /** Return pointer to the idx-th data item - const version */ KUDr@5633: FORCEINLINE const Titem* Data(int idx) const { CheckIdx(idx); return (Data() + idx); } KUDr@5633: /** Return number of items in the Blob */ KUDr@5633: FORCEINLINE int Size() const { return (RawSize() / Titem_size); } KUDr@5633: /** Free the memory occupied by Blob destroying all items */ KUDr@5633: FORCEINLINE void Free() KUDr@5633: { KUDr@5633: assert((RawSize() % Titem_size) == 0); KUDr@5633: int old_size = Size(); KUDr@5633: if (old_size > 0) { KUDr@5633: // destroy removed items; KUDr@5633: Titem* pI_last_to_destroy = Data(0); KUDr@5633: for (Titem* pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem_(); KUDr@5633: } KUDr@5633: Tbase::Free(); KUDr@5633: } KUDr@5633: /** Grow number of data items in Blob by given number - doesn't construct items */ KUDr@5633: FORCEINLINE Titem* GrowSizeNC(int num_items) { return (Titem*)GrowRawSize(num_items * Titem_size); } KUDr@5633: /** Grow number of data items in Blob by given number - constructs new items (using Titem_'s default constructor) */ KUDr@5633: FORCEINLINE Titem* GrowSizeC(int num_items) KUDr@5633: { KUDr@5633: Titem* pI = GrowSizeNC(num_items); KUDr@5633: for (int i = num_items; i > 0; i--, pI++) new (pI) Titem(); KUDr@5633: } KUDr@5633: /** Destroy given number of items and reduce the Blob's data size */ KUDr@5633: FORCEINLINE void ReduceSize(int num_items) KUDr@5633: { KUDr@5633: assert((RawSize() % Titem_size) == 0); KUDr@5633: int old_size = Size(); KUDr@5633: assert(num_items <= old_size); KUDr@5633: int new_size = (num_items <= old_size) ? (old_size - num_items) : 0; KUDr@5633: // destroy removed items; KUDr@5633: Titem* pI_last_to_destroy = Data(new_size); KUDr@5633: for (Titem* pI = Data(old_size - 1); pI >= pI_last_to_destroy; pI--) pI->~Titem(); KUDr@5633: // remove them KUDr@5633: ReduceRawSize(num_items * Titem_size); KUDr@5633: } KUDr@5633: /** Append one data item at the end (calls Titem_'s default constructor) */ KUDr@5633: FORCEINLINE Titem* AppendNew() KUDr@5633: { KUDr@5633: Titem& dst = *GrowSizeNC(1); // Grow size by one item KUDr@5633: Titem* pNewItem = new (&dst) Titem(); // construct the new item by calling in-place new operator KUDr@5633: return pNewItem; KUDr@5633: } KUDr@5633: /** Append the copy of given item at the end of Blob (using copy constructor) */ KUDr@5633: FORCEINLINE Titem* Append(const Titem& src) KUDr@5633: { KUDr@5633: Titem& dst = *GrowSizeNC(1); // Grow size by one item KUDr@5633: Titem* pNewItem = new (&dst) Titem(src); // construct the new item by calling in-place new operator with copy ctor() KUDr@5633: return pNewItem; KUDr@5633: } KUDr@5633: /** Add given items (ptr + number of items) at the end of blob */ KUDr@5633: FORCEINLINE Titem* Append(const Titem* pSrc, int num_items) KUDr@5633: { KUDr@5633: Titem* pDst = GrowSizeNC(num_items); KUDr@5633: Titem* pDstOrg = pDst; KUDr@5633: Titem* pDstEnd = pDst + num_items; KUDr@5633: while (pDst < pDstEnd) new (pDst++) Titem(*(pSrc++)); KUDr@5633: return pDstOrg; KUDr@5633: } KUDr@5633: /** Remove item with the given index by replacing it by the last item and reducing the size by one */ KUDr@5633: FORCEINLINE void RemoveBySwap(int idx) KUDr@5633: { KUDr@5633: CheckIdx(idx); KUDr@5633: // destroy removed item KUDr@5633: Titem* pRemoved = Data(idx); KUDr@5633: RemoveBySwap(pRemoved); KUDr@5633: } KUDr@5633: /** Remove item given by pointer replacing it by the last item and reducing the size by one */ KUDr@5633: FORCEINLINE void RemoveBySwap(Titem* pItem) KUDr@5633: { KUDr@5633: Titem* pLast = Data(Size() - 1); KUDr@5633: assert(pItem >= Data() && pItem <= pLast); KUDr@5633: // move last item to its new place KUDr@5633: if (pItem != pLast) { KUDr@5633: pItem->~Titem_(); KUDr@5633: new (pItem) Titem_(*pLast); KUDr@5633: } KUDr@5633: // destroy the last item KUDr@5633: pLast->~Titem_(); KUDr@5633: // and reduce the raw blob size KUDr@5633: ReduceRawSize(Titem_size); KUDr@5633: } KUDr@5633: /** Ensures that given number of items can be added to the end of Blob. Returns pointer to the KUDr@5633: * first free (unused) item */ KUDr@5633: FORCEINLINE Titem* MakeFreeSpace(int num_items) { return (Titem*)MakeRawFreeSpace(num_items * Titem_size); } KUDr@5633: }; KUDr@5633: KUDr@5633: // simple string implementation KUDr@5633: struct CStrA : public CBlobT KUDr@5633: { KUDr@5633: typedef CBlobT base; KUDr@5633: CStrA(const char* str = NULL) {Append(str);} KUDr@5633: FORCEINLINE CStrA(const CBlobBaseSimple& src) : base(src) {} KUDr@5633: void Append(const char* str) {if (str != NULL && str[0] != '\0') base::Append(str, (int)strlen(str));} KUDr@5633: }; KUDr@5633: KUDr@5633: #endif /* BLOB_HPP */