2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
1999-08-10 19:18:39 +00:00
|
|
|
|
|
|
|
#include "nsSegmentedBuffer.h"
|
2013-08-22 10:32:11 +00:00
|
|
|
#include "nsMemory.h"
|
1999-08-10 19:18:39 +00:00
|
|
|
|
1999-08-24 08:45:17 +00:00
|
|
|
nsresult
|
2013-12-11 19:14:56 +00:00
|
|
|
nsSegmentedBuffer::Init(uint32_t segmentSize, uint32_t maxSize)
|
1999-08-24 08:45:17 +00:00
|
|
|
{
|
|
|
|
if (mSegmentArrayCount != 0)
|
|
|
|
return NS_ERROR_FAILURE; // initialized more than once
|
|
|
|
mSegmentSize = segmentSize;
|
|
|
|
mMaxSize = maxSize;
|
|
|
|
#if 0 // testing...
|
|
|
|
mSegmentArrayCount = 2;
|
|
|
|
#else
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
nsSegmentedBuffer::AppendNewSegment()
|
|
|
|
{
|
1999-08-24 08:45:17 +00:00
|
|
|
if (GetSize() >= mMaxSize)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
1999-08-24 08:45:17 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (mSegmentArray == nullptr) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytes = mSegmentArrayCount * sizeof(char*);
|
2000-06-03 09:46:12 +00:00
|
|
|
mSegmentArray = (char**)nsMemory::Alloc(bytes);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (mSegmentArray == nullptr)
|
|
|
|
return nullptr;
|
2002-02-05 01:41:13 +00:00
|
|
|
memset(mSegmentArray, 0, bytes);
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
1999-08-24 08:45:17 +00:00
|
|
|
|
1999-08-10 19:18:39 +00:00
|
|
|
if (IsFull()) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t newArraySize = mSegmentArrayCount * 2;
|
|
|
|
uint32_t bytes = newArraySize * sizeof(char*);
|
2000-06-03 09:46:12 +00:00
|
|
|
char** newSegArray = (char**)nsMemory::Realloc(mSegmentArray, bytes);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (newSegArray == nullptr)
|
|
|
|
return nullptr;
|
1999-08-10 19:18:39 +00:00
|
|
|
mSegmentArray = newSegArray;
|
|
|
|
// copy wrapped content to new extension
|
|
|
|
if (mFirstSegmentIndex > mLastSegmentIndex) {
|
|
|
|
// deal with wrap around case
|
2002-01-12 03:18:55 +00:00
|
|
|
memcpy(&mSegmentArray[mSegmentArrayCount],
|
2002-01-26 00:04:45 +00:00
|
|
|
mSegmentArray,
|
|
|
|
mLastSegmentIndex * sizeof(char*));
|
2002-02-05 01:41:13 +00:00
|
|
|
memset(mSegmentArray, 0, mLastSegmentIndex * sizeof(char*));
|
1999-08-10 19:18:39 +00:00
|
|
|
mLastSegmentIndex += mSegmentArrayCount;
|
2002-02-05 01:41:13 +00:00
|
|
|
memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
1999-08-24 08:45:17 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-02-05 01:41:13 +00:00
|
|
|
memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
mSegmentArrayCount = newArraySize;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:14:28 +00:00
|
|
|
char* seg = (char*)moz_malloc(mSegmentSize);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (seg == nullptr) {
|
|
|
|
return nullptr;
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
mSegmentArray[mLastSegmentIndex] = seg;
|
|
|
|
mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
|
|
|
|
return seg;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
1999-08-10 19:18:39 +00:00
|
|
|
nsSegmentedBuffer::DeleteFirstSegment()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nullptr, "deleting bad segment");
|
2013-12-11 19:14:28 +00:00
|
|
|
moz_free(mSegmentArray[mFirstSegmentIndex]);
|
2012-07-30 14:20:58 +00:00
|
|
|
mSegmentArray[mFirstSegmentIndex] = nullptr;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
1999-08-10 19:18:39 +00:00
|
|
|
if (mFirstSegmentIndex == last) {
|
|
|
|
mLastSegmentIndex = last;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFirstSegmentIndex = ModSegArraySize(mFirstSegmentIndex + 1);
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
1999-11-16 19:12:41 +00:00
|
|
|
nsSegmentedBuffer::DeleteLastSegment()
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(mSegmentArray[last] != nullptr, "deleting bad segment");
|
2013-12-11 19:14:28 +00:00
|
|
|
moz_free(mSegmentArray[last]);
|
2012-07-30 14:20:58 +00:00
|
|
|
mSegmentArray[last] = nullptr;
|
1999-11-16 19:12:41 +00:00
|
|
|
mLastSegmentIndex = last;
|
2011-09-29 06:19:26 +00:00
|
|
|
return (bool)(mLastSegmentIndex == mFirstSegmentIndex);
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
1999-11-16 19:12:41 +00:00
|
|
|
nsSegmentedBuffer::ReallocLastSegment(size_t newSize)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(mSegmentArray[last] != nullptr, "realloc'ing bad segment");
|
1999-11-16 19:12:41 +00:00
|
|
|
char *newSegment =
|
2013-12-11 19:14:28 +00:00
|
|
|
(char*)moz_realloc(mSegmentArray[last], newSize);
|
1999-11-16 19:12:41 +00:00
|
|
|
if (newSegment) {
|
|
|
|
mSegmentArray[last] = newSegment;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1999-11-16 19:12:41 +00:00
|
|
|
} else {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-10 19:18:39 +00:00
|
|
|
void
|
|
|
|
nsSegmentedBuffer::Empty()
|
|
|
|
{
|
|
|
|
if (mSegmentArray) {
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mSegmentArrayCount; i++) {
|
1999-08-10 19:18:39 +00:00
|
|
|
if (mSegmentArray[i])
|
2013-12-11 19:14:28 +00:00
|
|
|
moz_free(mSegmentArray[i]);
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
2000-06-03 09:46:12 +00:00
|
|
|
nsMemory::Free(mSegmentArray);
|
2012-07-30 14:20:58 +00:00
|
|
|
mSegmentArray = nullptr;
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
1999-08-26 00:25:43 +00:00
|
|
|
mFirstSegmentIndex = mLastSegmentIndex = 0;
|
1999-08-10 19:18:39 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 16:54:47 +00:00
|
|
|
#if 0
|
2011-08-18 13:46:39 +00:00
|
|
|
void
|
1999-08-10 19:18:39 +00:00
|
|
|
TestSegmentedBuffer()
|
|
|
|
{
|
1999-08-24 08:45:17 +00:00
|
|
|
nsSegmentedBuffer* buf = new nsSegmentedBuffer();
|
|
|
|
NS_ASSERTION(buf, "out of memory");
|
|
|
|
buf->Init(4, 16);
|
|
|
|
char* seg;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool empty;
|
1999-08-24 08:45:17 +00:00
|
|
|
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 19:18:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|