xpcom/io/nsSegmentedBuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsSegmentedBuffer.h"
michael@0 7 #include "nsMemory.h"
michael@0 8
michael@0 9 nsresult
michael@0 10 nsSegmentedBuffer::Init(uint32_t segmentSize, uint32_t maxSize)
michael@0 11 {
michael@0 12 if (mSegmentArrayCount != 0)
michael@0 13 return NS_ERROR_FAILURE; // initialized more than once
michael@0 14 mSegmentSize = segmentSize;
michael@0 15 mMaxSize = maxSize;
michael@0 16 #if 0 // testing...
michael@0 17 mSegmentArrayCount = 2;
michael@0 18 #else
michael@0 19 mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
michael@0 20 #endif
michael@0 21 return NS_OK;
michael@0 22 }
michael@0 23
michael@0 24 char*
michael@0 25 nsSegmentedBuffer::AppendNewSegment()
michael@0 26 {
michael@0 27 if (GetSize() >= mMaxSize)
michael@0 28 return nullptr;
michael@0 29
michael@0 30 if (mSegmentArray == nullptr) {
michael@0 31 uint32_t bytes = mSegmentArrayCount * sizeof(char*);
michael@0 32 mSegmentArray = (char**)nsMemory::Alloc(bytes);
michael@0 33 if (mSegmentArray == nullptr)
michael@0 34 return nullptr;
michael@0 35 memset(mSegmentArray, 0, bytes);
michael@0 36 }
michael@0 37
michael@0 38 if (IsFull()) {
michael@0 39 uint32_t newArraySize = mSegmentArrayCount * 2;
michael@0 40 uint32_t bytes = newArraySize * sizeof(char*);
michael@0 41 char** newSegArray = (char**)nsMemory::Realloc(mSegmentArray, bytes);
michael@0 42 if (newSegArray == nullptr)
michael@0 43 return nullptr;
michael@0 44 mSegmentArray = newSegArray;
michael@0 45 // copy wrapped content to new extension
michael@0 46 if (mFirstSegmentIndex > mLastSegmentIndex) {
michael@0 47 // deal with wrap around case
michael@0 48 memcpy(&mSegmentArray[mSegmentArrayCount],
michael@0 49 mSegmentArray,
michael@0 50 mLastSegmentIndex * sizeof(char*));
michael@0 51 memset(mSegmentArray, 0, mLastSegmentIndex * sizeof(char*));
michael@0 52 mLastSegmentIndex += mSegmentArrayCount;
michael@0 53 memset(&mSegmentArray[mLastSegmentIndex], 0,
michael@0 54 (newArraySize - mLastSegmentIndex) * sizeof(char*));
michael@0 55 }
michael@0 56 else {
michael@0 57 memset(&mSegmentArray[mLastSegmentIndex], 0,
michael@0 58 (newArraySize - mLastSegmentIndex) * sizeof(char*));
michael@0 59 }
michael@0 60 mSegmentArrayCount = newArraySize;
michael@0 61 }
michael@0 62
michael@0 63 char* seg = (char*)moz_malloc(mSegmentSize);
michael@0 64 if (seg == nullptr) {
michael@0 65 return nullptr;
michael@0 66 }
michael@0 67 mSegmentArray[mLastSegmentIndex] = seg;
michael@0 68 mLastSegmentIndex = ModSegArraySize(mLastSegmentIndex + 1);
michael@0 69 return seg;
michael@0 70 }
michael@0 71
michael@0 72 bool
michael@0 73 nsSegmentedBuffer::DeleteFirstSegment()
michael@0 74 {
michael@0 75 NS_ASSERTION(mSegmentArray[mFirstSegmentIndex] != nullptr, "deleting bad segment");
michael@0 76 moz_free(mSegmentArray[mFirstSegmentIndex]);
michael@0 77 mSegmentArray[mFirstSegmentIndex] = nullptr;
michael@0 78 int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
michael@0 79 if (mFirstSegmentIndex == last) {
michael@0 80 mLastSegmentIndex = last;
michael@0 81 return true;
michael@0 82 }
michael@0 83 else {
michael@0 84 mFirstSegmentIndex = ModSegArraySize(mFirstSegmentIndex + 1);
michael@0 85 return false;
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 bool
michael@0 90 nsSegmentedBuffer::DeleteLastSegment()
michael@0 91 {
michael@0 92 int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
michael@0 93 NS_ASSERTION(mSegmentArray[last] != nullptr, "deleting bad segment");
michael@0 94 moz_free(mSegmentArray[last]);
michael@0 95 mSegmentArray[last] = nullptr;
michael@0 96 mLastSegmentIndex = last;
michael@0 97 return (bool)(mLastSegmentIndex == mFirstSegmentIndex);
michael@0 98 }
michael@0 99
michael@0 100 bool
michael@0 101 nsSegmentedBuffer::ReallocLastSegment(size_t newSize)
michael@0 102 {
michael@0 103 int32_t last = ModSegArraySize(mLastSegmentIndex - 1);
michael@0 104 NS_ASSERTION(mSegmentArray[last] != nullptr, "realloc'ing bad segment");
michael@0 105 char *newSegment =
michael@0 106 (char*)moz_realloc(mSegmentArray[last], newSize);
michael@0 107 if (newSegment) {
michael@0 108 mSegmentArray[last] = newSegment;
michael@0 109 return true;
michael@0 110 } else {
michael@0 111 return false;
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 void
michael@0 116 nsSegmentedBuffer::Empty()
michael@0 117 {
michael@0 118 if (mSegmentArray) {
michael@0 119 for (uint32_t i = 0; i < mSegmentArrayCount; i++) {
michael@0 120 if (mSegmentArray[i])
michael@0 121 moz_free(mSegmentArray[i]);
michael@0 122 }
michael@0 123 nsMemory::Free(mSegmentArray);
michael@0 124 mSegmentArray = nullptr;
michael@0 125 }
michael@0 126 mSegmentArrayCount = NS_SEGMENTARRAY_INITIAL_COUNT;
michael@0 127 mFirstSegmentIndex = mLastSegmentIndex = 0;
michael@0 128 }
michael@0 129
michael@0 130 #if 0
michael@0 131 void
michael@0 132 TestSegmentedBuffer()
michael@0 133 {
michael@0 134 nsSegmentedBuffer* buf = new nsSegmentedBuffer();
michael@0 135 NS_ASSERTION(buf, "out of memory");
michael@0 136 buf->Init(4, 16);
michael@0 137 char* seg;
michael@0 138 bool empty;
michael@0 139 seg = buf->AppendNewSegment();
michael@0 140 NS_ASSERTION(seg, "AppendNewSegment failed");
michael@0 141 seg = buf->AppendNewSegment();
michael@0 142 NS_ASSERTION(seg, "AppendNewSegment failed");
michael@0 143 seg = buf->AppendNewSegment();
michael@0 144 NS_ASSERTION(seg, "AppendNewSegment failed");
michael@0 145 empty = buf->DeleteFirstSegment();
michael@0 146 NS_ASSERTION(!empty, "DeleteFirstSegment failed");
michael@0 147 empty = buf->DeleteFirstSegment();
michael@0 148 NS_ASSERTION(!empty, "DeleteFirstSegment failed");
michael@0 149 seg = buf->AppendNewSegment();
michael@0 150 NS_ASSERTION(seg, "AppendNewSegment failed");
michael@0 151 seg = buf->AppendNewSegment();
michael@0 152 NS_ASSERTION(seg, "AppendNewSegment failed");
michael@0 153 seg = buf->AppendNewSegment();
michael@0 154 NS_ASSERTION(seg, "AppendNewSegment failed");
michael@0 155 empty = buf->DeleteFirstSegment();
michael@0 156 NS_ASSERTION(!empty, "DeleteFirstSegment failed");
michael@0 157 empty = buf->DeleteFirstSegment();
michael@0 158 NS_ASSERTION(!empty, "DeleteFirstSegment failed");
michael@0 159 empty = buf->DeleteFirstSegment();
michael@0 160 NS_ASSERTION(!empty, "DeleteFirstSegment failed");
michael@0 161 empty = buf->DeleteFirstSegment();
michael@0 162 NS_ASSERTION(empty, "DeleteFirstSegment failed");
michael@0 163 delete buf;
michael@0 164 }
michael@0 165 #endif
michael@0 166
michael@0 167 ////////////////////////////////////////////////////////////////////////////////

mercurial