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