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