layout/base/StackArena.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:abfe85b3bc58
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/. */
4
5 #ifndef StackArena_h
6 #define StackArena_h
7
8 #include "nsError.h"
9 #include "mozilla/Assertions.h"
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/NullPtr.h"
12
13 namespace mozilla {
14
15 struct StackBlock;
16 struct StackMark;
17 class AutoStackArena;
18
19 // Private helper class for AutoStackArena.
20 class StackArena {
21 private:
22 friend class AutoStackArena;
23 StackArena();
24 ~StackArena();
25
26 nsresult Init() { return mBlocks ? NS_OK : NS_ERROR_OUT_OF_MEMORY; }
27
28 // Memory management functions.
29 void* Allocate(size_t aSize);
30 void Push();
31 void Pop();
32
33 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
34
35 // Our current position in memory.
36 size_t mPos;
37
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;
41
42 // The current block.
43 StackBlock* mCurBlock;
44
45 // Our stack of mark where push has been called.
46 StackMark* mMarks;
47
48 // The current top of the mark list.
49 uint32_t mStackTop;
50
51 // The size of the mark array.
52 uint32_t mMarkLength;
53 };
54
55 // Class for stack scoped arena memory allocations.
56 //
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.
62 //
63 // Individual allocations cannot exceed StackBlock::MAX_USABLE_SIZE
64 // bytes.
65 //
66 class MOZ_STACK_CLASS AutoStackArena {
67 public:
68 AutoStackArena()
69 : mOwnsStackArena(false)
70 {
71 if (!gStackArena) {
72 gStackArena = new StackArena();
73 mOwnsStackArena = true;
74 gStackArena->Init();
75 }
76 gStackArena->Push();
77 }
78
79 ~AutoStackArena() {
80 gStackArena->Pop();
81 if (mOwnsStackArena) {
82 delete gStackArena;
83 gStackArena = nullptr;
84 }
85 }
86
87 static void* Allocate(size_t aSize) {
88 return gStackArena->Allocate(aSize);
89 }
90
91 private:
92 static StackArena* gStackArena;
93 bool mOwnsStackArena;
94 };
95
96 } // namespace mozilla
97
98 #endif

mercurial