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