Bug 1104435 part 2 - Make AnimationPlayer derive from nsISupports; r=smaug
[gecko.git] / layout / base / StackArena.h
blobc84e4617e8fffa97af91c8cb0bb9cbf8987d6577
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/. */
5 #ifndef StackArena_h
6 #define StackArena_h
8 #include "nsError.h"
9 #include "mozilla/Assertions.h"
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/NullPtr.h"
13 namespace mozilla {
15 struct StackBlock;
16 struct StackMark;
17 class AutoStackArena;
19 // Private helper class for AutoStackArena.
20 class StackArena {
21 private:
22 friend class AutoStackArena;
23 StackArena();
24 ~StackArena();
26 nsresult Init() { return mBlocks ? NS_OK : NS_ERROR_OUT_OF_MEMORY; }
28 // Memory management functions.
29 void* Allocate(size_t aSize);
30 void Push();
31 void Pop();
33 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
35 // Our current position in memory.
36 size_t mPos;
38 // A list of memory blocks. Usually there is only one
39 // but if we overrun our stack size we can get more memory.
40 StackBlock* mBlocks;
42 // The current block.
43 StackBlock* mCurBlock;
45 // Our stack of mark where push has been called.
46 StackMark* mMarks;
48 // The current top of the mark list.
49 uint32_t mStackTop;
51 // The size of the mark array.
52 uint32_t mMarkLength;
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
64 // bytes.
66 class MOZ_STACK_CLASS AutoStackArena {
67 public:
68 AutoStackArena()
69 : mOwnsStackArena(false)
71 if (!gStackArena) {
72 gStackArena = new StackArena();
73 mOwnsStackArena = true;
74 gStackArena->Init();
76 gStackArena->Push();
79 ~AutoStackArena() {
80 gStackArena->Pop();
81 if (mOwnsStackArena) {
82 delete gStackArena;
83 gStackArena = nullptr;
87 static void* Allocate(size_t aSize) {
88 return gStackArena->Allocate(aSize);
91 private:
92 static StackArena* gStackArena;
93 bool mOwnsStackArena;
96 } // namespace mozilla
98 #endif