| 77 | class Dorky {}; // Used to force use of a particular constructor. |
| 78 | |
| 79 | class ByteVector //: public Vector<ByteType> |
| 80 | : public ItemWithSize |
| 81 | { |
| 82 | ByteType* mData; ///< allocated data block, if any |
| 83 | ByteType* mStart; ///< start of useful data, always >=mData and <=mAllocEnd |
| 84 | unsigned mSizeBits; ///< size of useful data in bits. |
| 85 | ByteType *mAllocEnd; ///< end of allocated data + 1. |
| 86 | |
| 87 | //unsigned mBitInd; |
| 88 | unsigned bitind() { return mSizeBits % 8; } |
| 89 | |
| 90 | #if BYTEVECTOR_REFCNT |
| 91 | // The first mDataOffset bytes of mData is a short reference count of the number |
| 92 | // of ByteVectors pointing at it. |
| 93 | static const int mDataOffset = sizeof(short); |
| 94 | int setRefCnt(int val) { return ((short*)mData)[0] = val; } |
| 95 | int decRefCnt() { return setRefCnt(((short*)mData)[0] - 1); } |
| 96 | void incRefCnt() { setRefCnt(((short*)mData)[0] + 1); } |
| 97 | #endif |
| 98 | |
| 99 | |
| 100 | void init(size_t newSize); /** set size and allocated size to that specified */ |
| 101 | void initstr(const char *wdata, unsigned wlen) { init(wlen); setAppendP(0); append(wdata,wlen); } |
| 102 | void initstr(const char *wdata) { initstr(wdata,strlen(wdata)); } |
| 103 | void dup(const ByteVector& other); |
| 104 | |
| 105 | // Constructor: A ByteVector that does not own any memory, but is just a segment |
| 106 | // of some other memory. This constructor is private. |
| 107 | // The public way to do this is to use segmentTemp or create a ByteVectorTemp. |
| 108 | protected: |
| 109 | ByteVector(Dorky,ByteType*wstart,ByteType*wend) |
| 110 | : mData(0), mStart(wstart), mSizeBits(8*(wend-wstart)), mAllocEnd(wend) {} |
| 111 | //: mData(0), mStart(wstart), mEnd(wend), mAllocEnd(wend), mBitInd(0) {} |
| 112 | |
| 113 | public: |
| 114 | void clear(); // Release the memory used by this ByteVector. |
| 115 | // clone semantics are weird: copies data from other to self. |
| 116 | void clone(const ByteVector& other); /** Copy data from another vector. */ |
| 117 | #if BYTEVECTOR_REFCNT |
| 118 | int getRefCnt() { return mData ? ((short*)mData)[0] : 0; } |
| 119 | #endif |
| 120 | |
| 121 | const ByteType* begin() const { return mStart; } |
| 122 | ByteType*begin() { return mStart; } |
| 123 | |
| 124 | // This is the allocSize from mStart, not from mData. |
| 125 | size_t allocSize() const { return mAllocEnd - mStart; } |
| 126 | //size_t sizeBytes() const { return mEnd - mStart + !!mBitInd; } // size in bytes |
| 127 | size_t sizeBytes() const { return (mSizeBits+7)/8; } // size in bytes |
| 128 | size_t size() const { return sizeBytes(); } // size in bytes |
| 129 | //size_t sizeBits() const { return size() * 8 + mBitInd; } // size in bits |
| 130 | size_t sizeBits() const { return mSizeBits; } // size in bits |
| 131 | size_t sizeRemaining() const { // Remaining full bytes for appends |
| 132 | return (mAllocEnd - mStart) - sizeBytes(); |
| 133 | //int result = mAllocEnd - mEnd - !!mBitInd; |
| 134 | //return result >= 0 ? (size_t) result : 0; |
| 135 | } |
| 136 |
no outgoing calls
no test coverage detected