1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/Assertions.h"
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/NullPtr.h"
19 // Private helper class for AutoStackArena.
22 friend class AutoStackArena
;
26 nsresult
Init() { return mBlocks
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
; }
28 // Memory management functions.
29 void* Allocate(size_t aSize
);
33 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
35 // Our current position in memory.
38 // A list of memory blocks. Usually there is only one
39 // but if we overrun our stack size we can get more memory.
43 StackBlock
* mCurBlock
;
45 // Our stack of mark where push has been called.
48 // The current top of the mark list.
51 // The size of the mark array.
55 // Class for stack scoped arena memory allocations.
57 // Callers who wish to allocate memory whose lifetime corresponds to the
58 // lifetime of a stack-allocated object can use this class. First,
59 // declare an AutoStackArena object on the stack. Then all subsequent
60 // calls to Allocate will allocate memory from an arena pool that will
61 // be freed when that variable goes out of scope. Nesting is allowed.
63 // Individual allocations cannot exceed StackBlock::MAX_USABLE_SIZE
66 class MOZ_STACK_CLASS AutoStackArena
{
69 : mOwnsStackArena(false)
72 gStackArena
= new StackArena();
73 mOwnsStackArena
= true;
81 if (mOwnsStackArena
) {
83 gStackArena
= nullptr;
87 static void* Allocate(size_t aSize
) {
88 return gStackArena
->Allocate(aSize
);
92 static StackArena
* gStackArena
;
96 } // namespace mozilla