2014-05-05 21:30:43 +04: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 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"
|
2013-08-22 14:32:11 +04:00
|
|
|
#include "nsMemory.h"
|
1999-08-10 23:18:39 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult nsSegmentedBuffer::Init(uint32_t aSegmentSize, uint32_t aMaxSize) {
|
|
|
|
if (mSegmentArrayCount != 0) {
|
|
|
|
return NS_ERROR_FAILURE; // initialized more than once
|
|
|
|
}
|
|
|
|
mSegmentSize = aSegmentSize;
|
|
|
|
mMaxSize = aMaxSize;
|
1999-08-24 12:45:17 +04:00
|
|
|
#if 0 // testing...
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentArrayCount = 2;
|
1999-08-24 12:45:17 +04:00
|
|
|
#else
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
1999-08-24 12:45:17 +04:00
|
|
|
#endif
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char* nsSegmentedBuffer::AppendNewSegment() {
|
2014-05-15 00:15:46 +04:00
|
|
|
if (GetSize() >= mMaxSize) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return nullptr;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
1999-08-24 12:45:17 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mSegmentArray) {
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t bytes = mSegmentArrayCount * sizeof(char*);
|
2015-03-27 03:01:12 +03:00
|
|
|
mSegmentArray = (char**)moz_xmalloc(bytes);
|
2014-05-05 21:30:43 +04:00
|
|
|
memset(mSegmentArray, 0, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsFull()) {
|
|
|
|
uint32_t newArraySize = mSegmentArrayCount * 2;
|
|
|
|
uint32_t bytes = newArraySize * sizeof(char*);
|
2018-08-28 08:56:23 +03:00
|
|
|
mSegmentArray = (char**)moz_xrealloc(mSegmentArray, bytes);
|
2014-05-05 21:30:43 +04:00
|
|
|
// 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*));
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
memset(&mSegmentArray[mLastSegmentIndex], 0,
|
|
|
|
(newArraySize - mLastSegmentIndex) * sizeof(char*));
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentArrayCount = newArraySize;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
|
2015-02-19 07:51:06 +03:00
|
|
|
char* seg = (char*)malloc(mSegmentSize);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!seg) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mSegmentArray[mLastSegmentIndex] = seg;
|
|
|
|
mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
|
|
|
|
return seg;
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nsSegmentedBuffer::DeleteFirstSegment() {
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nullptr,
|
|
|
|
"deleting bad segment");
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mSegmentArray[mFirstSegmentIndex]);
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentArray[mFirstSegmentIndex] = nullptr;
|
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
|
|
|
if (mFirstSegmentIndex == last) {
|
|
|
|
mLastSegmentIndex = last;
|
|
|
|
return true;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
mFirstSegmentIndex = ModSegArraySize(mFirstSegmentIndex + 1);
|
|
|
|
return false;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
bool nsSegmentedBuffer::DeleteLastSegment() {
|
2014-05-05 21:30:43 +04:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
|
|
|
NS_ASSERTION(mSegmentArray[last] != nullptr, "deleting bad segment");
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mSegmentArray[last]);
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentArray[last] = nullptr;
|
|
|
|
mLastSegmentIndex = last;
|
|
|
|
return (bool)(mLastSegmentIndex == mFirstSegmentIndex);
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
bool nsSegmentedBuffer::ReallocLastSegment(size_t aNewSize) {
|
2014-05-05 21:30:43 +04:00
|
|
|
int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
|
|
|
|
NS_ASSERTION(mSegmentArray[last] != nullptr, "realloc'ing bad segment");
|
2015-02-19 07:51:06 +03:00
|
|
|
char* newSegment = (char*)realloc(mSegmentArray[last], aNewSize);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (newSegment) {
|
|
|
|
mSegmentArray[last] = newSegment;
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
return false;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
void nsSegmentedBuffer::Empty() {
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mSegmentArray) {
|
|
|
|
for (uint32_t i = 0; i < mSegmentArrayCount; i++) {
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mSegmentArray[i]) {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mSegmentArray[i]);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
2015-03-27 03:01:12 +03:00
|
|
|
free(mSegmentArray);
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentArray = nullptr;
|
|
|
|
}
|
|
|
|
mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
|
|
|
|
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()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
nsSegmentedBuffer* buf = new nsSegmentedBuffer();
|
|
|
|
NS_ASSERTION(buf, "out of memory");
|
|
|
|
buf->Init(4, 16);
|
|
|
|
char* seg;
|
|
|
|
bool 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
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|