2017-10-27 20:33:53 +03: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-04 04:14:02 +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/. */
|
|
|
|
|
|
|
|
#include "StackArena.h"
|
2013-08-20 02:55:18 +04:00
|
|
|
#include "nsAlgorithm.h"
|
|
|
|
#include "nsDebug.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
2012-05-04 04:14:02 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// A block of memory that the stack will chop up and hand out.
|
2012-05-04 04:14:02 +04:00
|
|
|
struct StackBlock {
|
2013-09-26 08:28:55 +04:00
|
|
|
// Subtract sizeof(StackBlock*) to give space for the |mNext| field.
|
|
|
|
static const size_t MAX_USABLE_SIZE = 4096 - sizeof(StackBlock*);
|
2013-09-26 08:28:46 +04:00
|
|
|
|
2013-09-26 08:28:55 +04:00
|
|
|
// A block of memory.
|
2013-09-26 08:28:46 +04:00
|
|
|
char mBlock[MAX_USABLE_SIZE];
|
|
|
|
|
|
|
|
// Another block of memory that would only be created if our stack
|
|
|
|
// overflowed.
|
|
|
|
StackBlock* mNext;
|
|
|
|
|
|
|
|
StackBlock() : mNext(nullptr) { }
|
|
|
|
~StackBlock() { }
|
2012-05-04 04:14:02 +04:00
|
|
|
};
|
|
|
|
|
2013-09-26 08:28:55 +04:00
|
|
|
static_assert(sizeof(StackBlock) == 4096, "StackBlock must be 4096 bytes");
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// We hold an array of marks. A push pushes a mark on the stack.
|
|
|
|
// A pop pops it off.
|
2012-05-04 04:14:02 +04:00
|
|
|
struct StackMark {
|
2013-09-26 08:28:46 +04:00
|
|
|
// The block of memory from which we are currently handing out chunks.
|
|
|
|
StackBlock* mBlock;
|
|
|
|
|
|
|
|
// Our current position in the block.
|
|
|
|
size_t mPos;
|
2012-05-04 04:14:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
StackArena* AutoStackArena::gStackArena;
|
|
|
|
|
|
|
|
StackArena::StackArena()
|
|
|
|
{
|
|
|
|
mMarkLength = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
mMarks = nullptr;
|
2012-05-04 04:14:02 +04:00
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Allocate our stack memory.
|
2012-05-04 04:14:02 +04:00
|
|
|
mBlocks = new StackBlock();
|
|
|
|
mCurBlock = mBlocks;
|
|
|
|
|
|
|
|
mStackTop = 0;
|
|
|
|
mPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StackArena::~StackArena()
|
|
|
|
{
|
2013-09-26 08:28:46 +04:00
|
|
|
// Free up our data.
|
|
|
|
delete [] mMarks;
|
|
|
|
while (mBlocks) {
|
2012-05-04 04:14:02 +04:00
|
|
|
StackBlock* toDelete = mBlocks;
|
|
|
|
mBlocks = mBlocks->mNext;
|
|
|
|
delete toDelete;
|
|
|
|
}
|
2013-09-26 08:28:46 +04:00
|
|
|
}
|
2012-05-04 04:14:02 +04:00
|
|
|
|
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
StackArena::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-05-04 04:14:02 +04:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
StackBlock *block = mBlocks;
|
|
|
|
while (block) {
|
|
|
|
n += aMallocSizeOf(block);
|
|
|
|
block = block->mNext;
|
|
|
|
}
|
|
|
|
n += aMallocSizeOf(mMarks);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
static const int STACK_ARENA_MARK_INCREMENT = 50;
|
|
|
|
|
2012-05-04 04:14:02 +04:00
|
|
|
void
|
|
|
|
StackArena::Push()
|
|
|
|
{
|
|
|
|
// Resize the mark array if we overrun it. Failure to allocate the
|
|
|
|
// mark array is not fatal; we just won't free to that mark. This
|
|
|
|
// allows callers not to worry about error checking.
|
2013-09-26 08:28:46 +04:00
|
|
|
if (mStackTop >= mMarkLength) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newLength = mStackTop + STACK_ARENA_MARK_INCREMENT;
|
2012-05-04 04:14:02 +04:00
|
|
|
StackMark* newMarks = new StackMark[newLength];
|
|
|
|
if (newMarks) {
|
2013-09-26 08:28:46 +04:00
|
|
|
if (mMarkLength) {
|
2012-05-04 04:14:02 +04:00
|
|
|
memcpy(newMarks, mMarks, sizeof(StackMark)*mMarkLength);
|
2013-09-26 08:28:46 +04:00
|
|
|
}
|
2012-05-04 04:14:02 +04:00
|
|
|
// Fill in any marks that we couldn't allocate during a prior call
|
|
|
|
// to Push().
|
|
|
|
for (; mMarkLength < mStackTop; ++mMarkLength) {
|
|
|
|
NS_NOTREACHED("should only hit this on out-of-memory");
|
|
|
|
newMarks[mMarkLength].mBlock = mCurBlock;
|
|
|
|
newMarks[mMarkLength].mPos = mPos;
|
|
|
|
}
|
|
|
|
delete [] mMarks;
|
|
|
|
mMarks = newMarks;
|
|
|
|
mMarkLength = newLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Set a mark at the top (if we can).
|
2012-05-04 04:14:02 +04:00
|
|
|
NS_ASSERTION(mStackTop < mMarkLength, "out of memory");
|
|
|
|
if (mStackTop < mMarkLength) {
|
|
|
|
mMarks[mStackTop].mBlock = mCurBlock;
|
|
|
|
mMarks[mStackTop].mPos = mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStackTop++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
StackArena::Allocate(size_t aSize)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStackTop > 0, "Allocate called without Push");
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Align to a multiple of 8.
|
2012-05-04 04:14:02 +04:00
|
|
|
aSize = NS_ROUNDUP<size_t>(aSize, 8);
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// On stack overflow, grab another block.
|
|
|
|
if (mPos + aSize >= StackBlock::MAX_USABLE_SIZE) {
|
|
|
|
NS_ASSERTION(aSize <= StackBlock::MAX_USABLE_SIZE,
|
2012-05-04 04:14:02 +04:00
|
|
|
"Requested memory is greater that our block size!!");
|
2013-09-26 08:28:46 +04:00
|
|
|
if (mCurBlock->mNext == nullptr) {
|
2012-05-04 04:14:02 +04:00
|
|
|
mCurBlock->mNext = new StackBlock();
|
2013-09-26 08:28:46 +04:00
|
|
|
}
|
2012-05-04 04:14:02 +04:00
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
mCurBlock = mCurBlock->mNext;
|
2012-05-04 04:14:02 +04:00
|
|
|
mPos = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Return the chunk they need.
|
2012-05-04 04:14:02 +04:00
|
|
|
void *result = mCurBlock->mBlock + mPos;
|
|
|
|
mPos += aSize;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StackArena::Pop()
|
|
|
|
{
|
2013-09-26 08:28:46 +04:00
|
|
|
// Pop off the mark.
|
2012-05-04 04:14:02 +04:00
|
|
|
NS_ASSERTION(mStackTop > 0, "unmatched pop");
|
|
|
|
mStackTop--;
|
|
|
|
|
|
|
|
if (mStackTop >= mMarkLength) {
|
|
|
|
// We couldn't allocate the marks array at the time of the push, so
|
|
|
|
// we don't know where we're freeing to.
|
|
|
|
NS_NOTREACHED("out of memory");
|
|
|
|
if (mStackTop == 0) {
|
|
|
|
// But we do know if we've completely pushed the stack.
|
|
|
|
mCurBlock = mBlocks;
|
|
|
|
mPos = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Mark the "freed" memory with 0xdd to help with debugging of memory
|
|
|
|
// allocation problems.
|
|
|
|
{
|
|
|
|
StackBlock *block = mMarks[mStackTop].mBlock, *block_end = mCurBlock;
|
|
|
|
size_t pos = mMarks[mStackTop].mPos;
|
|
|
|
for (; block != block_end; block = block->mNext, pos = 0) {
|
|
|
|
memset(block->mBlock + pos, 0xdd, sizeof(block->mBlock) - pos);
|
|
|
|
}
|
|
|
|
memset(block->mBlock + pos, 0xdd, mPos - pos);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mCurBlock = mMarks[mStackTop].mBlock;
|
|
|
|
mPos = mMarks[mStackTop].mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|