michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsSegmentedBuffer_h__ michael@0: #define nsSegmentedBuffer_h__ michael@0: michael@0: #include "nsIMemory.h" michael@0: michael@0: class nsSegmentedBuffer michael@0: { michael@0: public: michael@0: nsSegmentedBuffer() michael@0: : mSegmentSize(0), mMaxSize(0), michael@0: mSegmentArray(nullptr), michael@0: mSegmentArrayCount(0), michael@0: mFirstSegmentIndex(0), mLastSegmentIndex(0) {} michael@0: michael@0: ~nsSegmentedBuffer() { michael@0: Empty(); michael@0: } michael@0: michael@0: michael@0: nsresult Init(uint32_t segmentSize, uint32_t maxSize); michael@0: michael@0: char* AppendNewSegment(); // pushes at end michael@0: michael@0: // returns true if no more segments remain: michael@0: bool DeleteFirstSegment(); // pops from beginning michael@0: michael@0: // returns true if no more segments remain: michael@0: bool DeleteLastSegment(); // pops from beginning michael@0: michael@0: // Call Realloc() on last segment. This is used to reduce memory michael@0: // consumption when data is not an exact multiple of segment size. michael@0: bool ReallocLastSegment(size_t newSize); michael@0: michael@0: void Empty(); // frees all segments michael@0: michael@0: inline uint32_t GetSegmentCount() { michael@0: if (mFirstSegmentIndex <= mLastSegmentIndex) michael@0: return mLastSegmentIndex - mFirstSegmentIndex; michael@0: else michael@0: return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex; michael@0: } michael@0: michael@0: inline uint32_t GetSegmentSize() { return mSegmentSize; } michael@0: inline uint32_t GetMaxSize() { return mMaxSize; } michael@0: inline uint32_t GetSize() { return GetSegmentCount() * mSegmentSize; } michael@0: michael@0: inline char* GetSegment(uint32_t indx) { michael@0: NS_ASSERTION(indx < GetSegmentCount(), "index out of bounds"); michael@0: int32_t i = ModSegArraySize(mFirstSegmentIndex + (int32_t)indx); michael@0: return mSegmentArray[i]; michael@0: } michael@0: michael@0: protected: michael@0: inline int32_t ModSegArraySize(int32_t n) { michael@0: uint32_t result = n & (mSegmentArrayCount - 1); michael@0: NS_ASSERTION(result == n % mSegmentArrayCount, michael@0: "non-power-of-2 mSegmentArrayCount"); michael@0: return result; michael@0: } michael@0: michael@0: inline bool IsFull() { michael@0: return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex; michael@0: } michael@0: michael@0: protected: michael@0: uint32_t mSegmentSize; michael@0: uint32_t mMaxSize; michael@0: char** mSegmentArray; michael@0: uint32_t mSegmentArrayCount; michael@0: int32_t mFirstSegmentIndex; michael@0: int32_t mLastSegmentIndex; michael@0: }; michael@0: michael@0: // NS_SEGMENTARRAY_INITIAL_SIZE: This number needs to start out as a michael@0: // power of 2 given how it gets used. We double the segment array michael@0: // when we overflow it, and use that fact that it's a power of 2 michael@0: // to compute a fast modulus operation in IsFull. michael@0: // michael@0: // 32 segment array entries can accommodate 128k of data if segments michael@0: // are 4k in size. That seems like a reasonable amount that will avoid michael@0: // needing to grow the segment array. michael@0: #define NS_SEGMENTARRAY_INITIAL_COUNT 32 michael@0: michael@0: #endif // nsSegmentedBuffer_h__