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/. */
|
|
|
|
|
2013-11-15 06:49:29 +04:00
|
|
|
#ifndef StackArena_h
|
|
|
|
#define StackArena_h
|
|
|
|
|
2013-08-20 02:55:18 +04:00
|
|
|
#include "nsError.h"
|
2012-05-04 04:14:02 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-05-04 04:14:02 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-05-18 19:42:39 +04:00
|
|
|
struct StackBlock;
|
|
|
|
struct StackMark;
|
2012-05-04 04:14:02 +04:00
|
|
|
class AutoStackArena;
|
2013-09-26 08:28:46 +04:00
|
|
|
|
|
|
|
// Private helper class for AutoStackArena.
|
2012-05-04 04:14:02 +04:00
|
|
|
class StackArena {
|
|
|
|
private:
|
|
|
|
friend class AutoStackArena;
|
|
|
|
StackArena();
|
|
|
|
~StackArena();
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Memory management functions.
|
2012-05-04 04:14:02 +04:00
|
|
|
void* Allocate(size_t aSize);
|
|
|
|
void Push();
|
|
|
|
void Pop();
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-05-04 04:14:02 +04:00
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Our current position in memory.
|
2012-05-04 04:14:02 +04:00
|
|
|
size_t mPos;
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// A list of memory blocks. Usually there is only one
|
2012-05-04 04:14:02 +04:00
|
|
|
// but if we overrun our stack size we can get more memory.
|
|
|
|
StackBlock* mBlocks;
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// The current block.
|
2012-05-04 04:14:02 +04:00
|
|
|
StackBlock* mCurBlock;
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Our stack of mark where push has been called.
|
2012-05-04 04:14:02 +04:00
|
|
|
StackMark* mMarks;
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// The current top of the mark list.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mStackTop;
|
2012-05-04 04:14:02 +04:00
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// The size of the mark array.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mMarkLength;
|
2012-05-04 04:14:02 +04:00
|
|
|
};
|
|
|
|
|
2013-09-26 08:28:46 +04:00
|
|
|
// Class for stack scoped arena memory allocations.
|
|
|
|
//
|
|
|
|
// Callers who wish to allocate memory whose lifetime corresponds to the
|
|
|
|
// lifetime of a stack-allocated object can use this class. First,
|
|
|
|
// declare an AutoStackArena object on the stack. Then all subsequent
|
|
|
|
// calls to Allocate will allocate memory from an arena pool that will
|
|
|
|
// be freed when that variable goes out of scope. Nesting is allowed.
|
|
|
|
//
|
|
|
|
// Individual allocations cannot exceed StackBlock::MAX_USABLE_SIZE
|
|
|
|
// bytes.
|
|
|
|
//
|
2016-03-28 15:34:26 +03:00
|
|
|
class MOZ_RAII AutoStackArena {
|
2012-05-04 04:14:02 +04:00
|
|
|
public:
|
2013-09-26 08:28:46 +04:00
|
|
|
AutoStackArena() : mOwnsStackArena(false) {
|
2012-05-04 04:14:02 +04:00
|
|
|
if (!gStackArena) {
|
|
|
|
gStackArena = new StackArena();
|
|
|
|
mOwnsStackArena = true;
|
|
|
|
}
|
|
|
|
gStackArena->Push();
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoStackArena() {
|
|
|
|
gStackArena->Pop();
|
|
|
|
if (mOwnsStackArena) {
|
|
|
|
delete gStackArena;
|
2012-07-30 18:20:58 +04:00
|
|
|
gStackArena = nullptr;
|
2012-05-04 04:14:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* Allocate(size_t aSize) { return gStackArena->Allocate(aSize); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static StackArena* gStackArena;
|
|
|
|
bool mOwnsStackArena;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2013-11-15 06:49:29 +04:00
|
|
|
|
|
|
|
#endif
|