Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsSegmentedBuffer_h__ |
michael@0 | 7 | #define nsSegmentedBuffer_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIMemory.h" |
michael@0 | 10 | |
michael@0 | 11 | class nsSegmentedBuffer |
michael@0 | 12 | { |
michael@0 | 13 | public: |
michael@0 | 14 | nsSegmentedBuffer() |
michael@0 | 15 | : mSegmentSize(0), mMaxSize(0), |
michael@0 | 16 | mSegmentArray(nullptr), |
michael@0 | 17 | mSegmentArrayCount(0), |
michael@0 | 18 | mFirstSegmentIndex(0), mLastSegmentIndex(0) {} |
michael@0 | 19 | |
michael@0 | 20 | ~nsSegmentedBuffer() { |
michael@0 | 21 | Empty(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | nsresult Init(uint32_t segmentSize, uint32_t maxSize); |
michael@0 | 26 | |
michael@0 | 27 | char* AppendNewSegment(); // pushes at end |
michael@0 | 28 | |
michael@0 | 29 | // returns true if no more segments remain: |
michael@0 | 30 | bool DeleteFirstSegment(); // pops from beginning |
michael@0 | 31 | |
michael@0 | 32 | // returns true if no more segments remain: |
michael@0 | 33 | bool DeleteLastSegment(); // pops from beginning |
michael@0 | 34 | |
michael@0 | 35 | // Call Realloc() on last segment. This is used to reduce memory |
michael@0 | 36 | // consumption when data is not an exact multiple of segment size. |
michael@0 | 37 | bool ReallocLastSegment(size_t newSize); |
michael@0 | 38 | |
michael@0 | 39 | void Empty(); // frees all segments |
michael@0 | 40 | |
michael@0 | 41 | inline uint32_t GetSegmentCount() { |
michael@0 | 42 | if (mFirstSegmentIndex <= mLastSegmentIndex) |
michael@0 | 43 | return mLastSegmentIndex - mFirstSegmentIndex; |
michael@0 | 44 | else |
michael@0 | 45 | return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | inline uint32_t GetSegmentSize() { return mSegmentSize; } |
michael@0 | 49 | inline uint32_t GetMaxSize() { return mMaxSize; } |
michael@0 | 50 | inline uint32_t GetSize() { return GetSegmentCount() * mSegmentSize; } |
michael@0 | 51 | |
michael@0 | 52 | inline char* GetSegment(uint32_t indx) { |
michael@0 | 53 | NS_ASSERTION(indx < GetSegmentCount(), "index out of bounds"); |
michael@0 | 54 | int32_t i = ModSegArraySize(mFirstSegmentIndex + (int32_t)indx); |
michael@0 | 55 | return mSegmentArray[i]; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | protected: |
michael@0 | 59 | inline int32_t ModSegArraySize(int32_t n) { |
michael@0 | 60 | uint32_t result = n & (mSegmentArrayCount - 1); |
michael@0 | 61 | NS_ASSERTION(result == n % mSegmentArrayCount, |
michael@0 | 62 | "non-power-of-2 mSegmentArrayCount"); |
michael@0 | 63 | return result; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | inline bool IsFull() { |
michael@0 | 67 | return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | protected: |
michael@0 | 71 | uint32_t mSegmentSize; |
michael@0 | 72 | uint32_t mMaxSize; |
michael@0 | 73 | char** mSegmentArray; |
michael@0 | 74 | uint32_t mSegmentArrayCount; |
michael@0 | 75 | int32_t mFirstSegmentIndex; |
michael@0 | 76 | int32_t mLastSegmentIndex; |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | // NS_SEGMENTARRAY_INITIAL_SIZE: This number needs to start out as a |
michael@0 | 80 | // power of 2 given how it gets used. We double the segment array |
michael@0 | 81 | // when we overflow it, and use that fact that it's a power of 2 |
michael@0 | 82 | // to compute a fast modulus operation in IsFull. |
michael@0 | 83 | // |
michael@0 | 84 | // 32 segment array entries can accommodate 128k of data if segments |
michael@0 | 85 | // are 4k in size. That seems like a reasonable amount that will avoid |
michael@0 | 86 | // needing to grow the segment array. |
michael@0 | 87 | #define NS_SEGMENTARRAY_INITIAL_COUNT 32 |
michael@0 | 88 | |
michael@0 | 89 | #endif // nsSegmentedBuffer_h__ |