1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/io/nsSegmentedBuffer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsSegmentedBuffer_h__ 1.10 +#define nsSegmentedBuffer_h__ 1.11 + 1.12 +#include "nsIMemory.h" 1.13 + 1.14 +class nsSegmentedBuffer 1.15 +{ 1.16 +public: 1.17 + nsSegmentedBuffer() 1.18 + : mSegmentSize(0), mMaxSize(0), 1.19 + mSegmentArray(nullptr), 1.20 + mSegmentArrayCount(0), 1.21 + mFirstSegmentIndex(0), mLastSegmentIndex(0) {} 1.22 + 1.23 + ~nsSegmentedBuffer() { 1.24 + Empty(); 1.25 + } 1.26 + 1.27 + 1.28 + nsresult Init(uint32_t segmentSize, uint32_t maxSize); 1.29 + 1.30 + char* AppendNewSegment(); // pushes at end 1.31 + 1.32 + // returns true if no more segments remain: 1.33 + bool DeleteFirstSegment(); // pops from beginning 1.34 + 1.35 + // returns true if no more segments remain: 1.36 + bool DeleteLastSegment(); // pops from beginning 1.37 + 1.38 + // Call Realloc() on last segment. This is used to reduce memory 1.39 + // consumption when data is not an exact multiple of segment size. 1.40 + bool ReallocLastSegment(size_t newSize); 1.41 + 1.42 + void Empty(); // frees all segments 1.43 + 1.44 + inline uint32_t GetSegmentCount() { 1.45 + if (mFirstSegmentIndex <= mLastSegmentIndex) 1.46 + return mLastSegmentIndex - mFirstSegmentIndex; 1.47 + else 1.48 + return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex; 1.49 + } 1.50 + 1.51 + inline uint32_t GetSegmentSize() { return mSegmentSize; } 1.52 + inline uint32_t GetMaxSize() { return mMaxSize; } 1.53 + inline uint32_t GetSize() { return GetSegmentCount() * mSegmentSize; } 1.54 + 1.55 + inline char* GetSegment(uint32_t indx) { 1.56 + NS_ASSERTION(indx < GetSegmentCount(), "index out of bounds"); 1.57 + int32_t i = ModSegArraySize(mFirstSegmentIndex + (int32_t)indx); 1.58 + return mSegmentArray[i]; 1.59 + } 1.60 + 1.61 +protected: 1.62 + inline int32_t ModSegArraySize(int32_t n) { 1.63 + uint32_t result = n & (mSegmentArrayCount - 1); 1.64 + NS_ASSERTION(result == n % mSegmentArrayCount, 1.65 + "non-power-of-2 mSegmentArrayCount"); 1.66 + return result; 1.67 + } 1.68 + 1.69 + inline bool IsFull() { 1.70 + return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex; 1.71 + } 1.72 + 1.73 +protected: 1.74 + uint32_t mSegmentSize; 1.75 + uint32_t mMaxSize; 1.76 + char** mSegmentArray; 1.77 + uint32_t mSegmentArrayCount; 1.78 + int32_t mFirstSegmentIndex; 1.79 + int32_t mLastSegmentIndex; 1.80 +}; 1.81 + 1.82 +// NS_SEGMENTARRAY_INITIAL_SIZE: This number needs to start out as a 1.83 +// power of 2 given how it gets used. We double the segment array 1.84 +// when we overflow it, and use that fact that it's a power of 2 1.85 +// to compute a fast modulus operation in IsFull. 1.86 +// 1.87 +// 32 segment array entries can accommodate 128k of data if segments 1.88 +// are 4k in size. That seems like a reasonable amount that will avoid 1.89 +// needing to grow the segment array. 1.90 +#define NS_SEGMENTARRAY_INITIAL_COUNT 32 1.91 + 1.92 +#endif // nsSegmentedBuffer_h__