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
|
|
|
|
|
|
|
#include "nsSegmentedBuffer.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSegmentedBuffer::Init(uint32_t segmentSize, uint32_t maxSize,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIMemory* allocator)
|
|
|
|
{
|
|
|
|
if (mSegmentArrayCount != 0)
|
|
|
|
return NS_ERROR_FAILURE; // initialized more than once
|
|
|
|
mSegmentSize = segmentSize;
|
|
|
|
mMaxSize = maxSize;
|
|
|
|
mSegAllocator = allocator;
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mSegAllocator == nullptr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mSegAllocator = nsMemory::GetGlobalMemoryService();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ADDREF(mSegAllocator);
|
|
|
|
}
|
|
|
|
#if 0 // testing...
|
|
|
|
mSegmentArrayCount = 2;
|
|
|
|
#else
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
nsSegmentedBuffer::AppendNewSegment()
|
|
|
|
{
|
|
|
|
if (GetSize() >= mMaxSize)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mSegmentArray == nullptr) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytes = mSegmentArrayCount * sizeof(char*);
|
2007-03-22 10:30:00 -07:00
|
|
|
mSegmentArray = (char**)nsMemory::Alloc(bytes);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (mSegmentArray == nullptr)
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
memset(mSegmentArray, 0, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsFull()) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t newArraySize = mSegmentArrayCount * 2;
|
|
|
|
uint32_t bytes = newArraySize * sizeof(char*);
|
2007-03-22 10:30:00 -07:00
|
|
|
char** newSegArray = (char**)nsMemory::Realloc(mSegmentArray, bytes);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (newSegArray == nullptr)
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
mSegmentArray = newSegArray;
|
|
|
|
// copy wrapped content to new extension
|
|
|
|
if (mFirstSegmentIndex > mLastSegmentIndex) {
|
|
|
|
// deal with wrap around case
|
|
|
|
memcpy(&mSegmentArray[mSegmentArrayCount],
|
|
|
|
mSegmentArray,
|
|
|
|
mLastSegmentIndex * sizeof(char*));
|
|
|
|
memset(mSegmentArray, 0, mLastSegmentIndex * sizeof(char*));
|
|
|
|
mLastSegmentIndex += mSegmentArrayCount;
|
|
|
|
memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
|
|
|
}
|
|
|
|
mSegmentArrayCount = newArraySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* seg = (char*)mSegAllocator->Alloc(mSegmentSize);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (seg == nullptr) {
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
mSegmentArray[mLastSegmentIndex] = seg;
|
|
|
|
mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
|
|
|
|
return seg;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSegmentedBuffer::DeleteFirstSegment()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nullptr, "deleting bad segment");
|
2007-03-22 10:30:00 -07:00
|
|
|
(void)mSegAllocator->Free(mSegmentArray[mFirstSegmentIndex]);
|
2012-07-30 07:20:58 -07:00
|
|
|
mSegmentArray[mFirstSegmentIndex] = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mFirstSegmentIndex == last) {
|
|
|
|
mLastSegmentIndex = last;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFirstSegmentIndex = ModSegArraySize(mFirstSegmentIndex + 1);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSegmentedBuffer::DeleteLastSegment()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSegmentArray[last] != nullptr, "deleting bad segment");
|
2007-03-22 10:30:00 -07:00
|
|
|
(void)mSegAllocator->Free(mSegmentArray[last]);
|
2012-07-30 07:20:58 -07:00
|
|
|
mSegmentArray[last] = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
mLastSegmentIndex = last;
|
2011-09-28 23:19:26 -07:00
|
|
|
return (bool)(mLastSegmentIndex == mFirstSegmentIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSegmentedBuffer::ReallocLastSegment(size_t newSize)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSegmentArray[last] != nullptr, "realloc'ing bad segment");
|
2007-03-22 10:30:00 -07:00
|
|
|
char *newSegment =
|
|
|
|
(char*)mSegAllocator->Realloc(mSegmentArray[last], newSize);
|
|
|
|
if (newSegment) {
|
|
|
|
mSegmentArray[last] = newSegment;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSegmentedBuffer::Empty()
|
|
|
|
{
|
|
|
|
if (mSegmentArray) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mSegmentArrayCount; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mSegmentArray[i])
|
|
|
|
mSegAllocator->Free(mSegmentArray[i]);
|
|
|
|
}
|
|
|
|
nsMemory::Free(mSegmentArray);
|
2012-07-30 07:20:58 -07:00
|
|
|
mSegmentArray = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
|
|
|
mFirstSegmentIndex = mLastSegmentIndex = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-23 09:54:47 -07:00
|
|
|
#if 0
|
2011-08-18 06:46:39 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
TestSegmentedBuffer()
|
|
|
|
{
|
|
|
|
nsSegmentedBuffer* buf = new nsSegmentedBuffer();
|
|
|
|
NS_ASSERTION(buf, "out of memory");
|
|
|
|
buf->Init(4, 16);
|
|
|
|
char* seg;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool empty;
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|