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