1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #include "mozilla/Assertions.h"
12 #include "mozilla/MemoryReporting.h"
20 // Private helper class for AutoStackArena.
23 friend class AutoStackArena
;
27 // Memory management functions.
28 void* Allocate(size_t aSize
);
32 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
34 // Our current position in memory.
37 // A list of memory blocks. Usually there is only one
38 // but if we overrun our stack size we can get more memory.
42 StackBlock
* mCurBlock
;
44 // Our stack of mark where push has been called.
47 // The current top of the mark list.
50 // The size of the mark array.
54 // Class for stack scoped arena memory allocations.
56 // Callers who wish to allocate memory whose lifetime corresponds to the
57 // lifetime of a stack-allocated object can use this class. First,
58 // declare an AutoStackArena object on the stack. Then all subsequent
59 // calls to Allocate will allocate memory from an arena pool that will
60 // be freed when that variable goes out of scope. Nesting is allowed.
62 // Individual allocations cannot exceed StackBlock::MAX_USABLE_SIZE
65 class MOZ_RAII AutoStackArena
{
67 AutoStackArena() : mOwnsStackArena(false) {
69 gStackArena
= new StackArena();
70 mOwnsStackArena
= true;
77 if (mOwnsStackArena
) {
79 gStackArena
= nullptr;
83 static void* Allocate(size_t aSize
) { return gStackArena
->Allocate(aSize
); }
86 static StackArena
* gStackArena
;
90 } // namespace mozilla