1999-08-10 23:18:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsSegmentedBuffer.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
nsSegmentedBuffer::nsSegmentedBuffer()
|
|
|
|
: mSegmentSize(0), mMaxSize(0),
|
|
|
|
mSegAllocator(nsnull), mSegmentArray(nsnull),
|
|
|
|
mSegmentArrayCount(0),
|
1999-08-10 23:18:39 +04:00
|
|
|
mFirstSegmentIndex(0), mLastSegmentIndex(0)
|
|
|
|
{
|
1999-08-24 12:45:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSegmentedBuffer::~nsSegmentedBuffer()
|
|
|
|
{
|
|
|
|
Empty();
|
|
|
|
NS_IF_RELEASE(mSegAllocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSegmentedBuffer::Init(PRUint32 segmentSize, PRUint32 maxSize,
|
|
|
|
nsIAllocator* allocator)
|
|
|
|
{
|
|
|
|
if (mSegmentArrayCount != 0)
|
|
|
|
return NS_ERROR_FAILURE; // initialized more than once
|
|
|
|
mSegmentSize = segmentSize;
|
|
|
|
mMaxSize = maxSize;
|
|
|
|
mSegAllocator = allocator;
|
1999-08-10 23:18:39 +04:00
|
|
|
if (mSegAllocator == nsnull) {
|
|
|
|
mSegAllocator = nsAllocator::GetGlobalAllocator();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ADDREF(mSegAllocator);
|
|
|
|
}
|
1999-08-24 12:45:17 +04:00
|
|
|
#if 0 // testing...
|
|
|
|
mSegmentArrayCount = 2;
|
|
|
|
#else
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
nsSegmentedBuffer::AppendNewSegment()
|
|
|
|
{
|
1999-08-24 12:45:17 +04:00
|
|
|
if (GetSize() >= mMaxSize)
|
|
|
|
return nsnull;
|
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
if (mSegmentArray == nsnull) {
|
|
|
|
PRUint32 bytes = mSegmentArrayCount * sizeof(char*);
|
|
|
|
mSegmentArray = (char**)nsAllocator::Alloc(bytes);
|
|
|
|
if (mSegmentArray == nsnull)
|
|
|
|
return nsnull;
|
1999-08-24 12:45:17 +04:00
|
|
|
nsCRT::memset(mSegmentArray, 0, bytes);
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
1999-08-24 12:45:17 +04:00
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
if (IsFull()) {
|
|
|
|
PRUint32 newArraySize = mSegmentArrayCount * 2;
|
|
|
|
PRUint32 bytes = newArraySize * sizeof(char*);
|
|
|
|
char** newSegArray = (char**)nsAllocator::Realloc(mSegmentArray, bytes);
|
|
|
|
if (newSegArray == nsnull)
|
|
|
|
return nsnull;
|
|
|
|
mSegmentArray = newSegArray;
|
|
|
|
// copy wrapped content to new extension
|
|
|
|
if (mFirstSegmentIndex > mLastSegmentIndex) {
|
|
|
|
// deal with wrap around case
|
|
|
|
nsCRT::memcpy(&mSegmentArray[mSegmentArrayCount],
|
|
|
|
mSegmentArray,
|
|
|
|
mLastSegmentIndex * sizeof(char*));
|
1999-08-24 12:45:17 +04:00
|
|
|
nsCRT::memset(mSegmentArray, 0, mLastSegmentIndex * sizeof(char*));
|
1999-08-10 23:18:39 +04:00
|
|
|
mLastSegmentIndex += mSegmentArrayCount;
|
1999-08-24 12:45:17 +04:00
|
|
|
nsCRT::memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsCRT::memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
mSegmentArrayCount = newArraySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* seg = (char*)mSegAllocator->Alloc(mSegmentSize);
|
|
|
|
if (seg == nsnull) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
mSegmentArray[mLastSegmentIndex] = seg;
|
|
|
|
mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
|
|
|
|
return seg;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSegmentedBuffer::DeleteFirstSegment()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nsnull, "deleting bad segment");
|
|
|
|
(void)mSegAllocator->Free(mSegmentArray[mFirstSegmentIndex]);
|
|
|
|
mSegmentArray[mFirstSegmentIndex] = nsnull;
|
|
|
|
PRInt32 last = ModSegArraySize(mLastSegmentIndex - 1);
|
|
|
|
if (mFirstSegmentIndex == last) {
|
|
|
|
mLastSegmentIndex = last;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFirstSegmentIndex = ModSegArraySize(mFirstSegmentIndex + 1);
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSegmentedBuffer::Empty()
|
|
|
|
{
|
|
|
|
if (mSegmentArray) {
|
|
|
|
for (PRUint32 i = 0; i < mSegmentArrayCount; i++) {
|
|
|
|
if (mSegmentArray[i])
|
|
|
|
mSegAllocator->Free(mSegmentArray[i]);
|
|
|
|
}
|
|
|
|
nsAllocator::Free(mSegmentArray);
|
|
|
|
mSegmentArray = nsnull;
|
|
|
|
}
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
1999-08-26 04:25:43 +04:00
|
|
|
mFirstSegmentIndex = mLastSegmentIndex = 0;
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_COM void
|
|
|
|
TestSegmentedBuffer()
|
|
|
|
{
|
1999-08-24 12:45:17 +04:00
|
|
|
nsSegmentedBuffer* buf = new nsSegmentedBuffer();
|
|
|
|
NS_ASSERTION(buf, "out of memory");
|
|
|
|
buf->Init(4, 16);
|
|
|
|
char* seg;
|
|
|
|
PRBool empty;
|
|
|
|
seg = buf->AppendNewSegment();
|
|
|
|
NS_ASSERTION(seg, "AppendNewSegment failed");
|
|
|
|
seg = buf->AppendNewSegment();
|
|
|
|
NS_ASSERTION(seg, "AppendNewSegment failed");
|
|
|
|
seg = buf->AppendNewSegment();
|
|
|
|
NS_ASSERTION(seg, "AppendNewSegment failed");
|
|
|
|
empty = buf->DeleteFirstSegment();
|
|
|
|
NS_ASSERTION(!empty, "DeleteFirstSegment failed");
|
|
|
|
empty = buf->DeleteFirstSegment();
|
|
|
|
NS_ASSERTION(!empty, "DeleteFirstSegment failed");
|
|
|
|
seg = buf->AppendNewSegment();
|
|
|
|
NS_ASSERTION(seg, "AppendNewSegment failed");
|
|
|
|
seg = buf->AppendNewSegment();
|
|
|
|
NS_ASSERTION(seg, "AppendNewSegment failed");
|
|
|
|
seg = buf->AppendNewSegment();
|
|
|
|
NS_ASSERTION(seg, "AppendNewSegment failed");
|
|
|
|
empty = buf->DeleteFirstSegment();
|
|
|
|
NS_ASSERTION(!empty, "DeleteFirstSegment failed");
|
|
|
|
empty = buf->DeleteFirstSegment();
|
|
|
|
NS_ASSERTION(!empty, "DeleteFirstSegment failed");
|
|
|
|
empty = buf->DeleteFirstSegment();
|
|
|
|
NS_ASSERTION(!empty, "DeleteFirstSegment failed");
|
|
|
|
empty = buf->DeleteFirstSegment();
|
|
|
|
NS_ASSERTION(empty, "DeleteFirstSegment failed");
|
|
|
|
delete buf;
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|