2014-05-05 10:30:43 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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__
|
|
|
|
|
2013-08-12 14:44:56 -07:00
|
|
|
#include "nsIMemory.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsSegmentedBuffer
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
nsSegmentedBuffer()
|
2014-05-14 13:15:46 -07:00
|
|
|
: mSegmentSize(0)
|
|
|
|
, mMaxSize(0)
|
|
|
|
, mSegmentArray(nullptr)
|
|
|
|
, mSegmentArrayCount(0)
|
|
|
|
, mFirstSegmentIndex(0)
|
|
|
|
, mLastSegmentIndex(0)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
~nsSegmentedBuffer()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
Empty();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult Init(uint32_t aSegmentSize, uint32_t aMaxSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
char* AppendNewSegment(); // pushes at end
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// returns true if no more segments remain:
|
|
|
|
bool DeleteFirstSegment(); // pops from beginning
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// returns true if no more segments remain:
|
|
|
|
bool DeleteLastSegment(); // pops from beginning
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Call Realloc() on last segment. This is used to reduce memory
|
|
|
|
// consumption when data is not an exact multiple of segment size.
|
2014-05-14 13:15:46 -07:00
|
|
|
bool ReallocLastSegment(size_t aNewSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
void Empty(); // frees all segments
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
inline uint32_t GetSegmentCount()
|
|
|
|
{
|
|
|
|
if (mFirstSegmentIndex <= mLastSegmentIndex) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return mLastSegmentIndex - mFirstSegmentIndex;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -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
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
inline char* GetSegment(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aIndex < GetSegmentCount(), "index out of bounds");
|
|
|
|
int32_t i = ModSegArraySize(mFirstSegmentIndex + (int32_t)aIndex);
|
2014-05-05 10:30:43 -07:00
|
|
|
return mSegmentArray[i];
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
2014-05-14 13:15:46 -07:00
|
|
|
inline int32_t ModSegArraySize(int32_t aIndex)
|
|
|
|
{
|
|
|
|
uint32_t result = aIndex & (mSegmentArrayCount - 1);
|
|
|
|
NS_ASSERTION(result == aIndex % mSegmentArrayCount,
|
2014-05-05 10:30:43 -07:00
|
|
|
"non-power-of-2 mSegmentArrayCount");
|
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
inline bool IsFull()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t mSegmentSize;
|
|
|
|
uint32_t mMaxSize;
|
|
|
|
char** mSegmentArray;
|
|
|
|
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
|
2014-05-05 10:30:43 -07:00
|
|
|
// when we overflow it, and use that fact that it's a power of 2
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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__
|