/src/mozilla-central/layout/base/nsPresArena.h
Line | Count | Source (jump to first uncovered line) |
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 |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | | */ |
7 | | |
8 | | /* arena allocation for the frame tree and closely-related objects */ |
9 | | |
10 | | #ifndef nsPresArena_h___ |
11 | | #define nsPresArena_h___ |
12 | | |
13 | | #include "mozilla/ArenaAllocator.h" |
14 | | #include "mozilla/ArenaObjectID.h" |
15 | | #include "mozilla/ArenaRefPtr.h" |
16 | | #include "mozilla/Assertions.h" |
17 | | #include "mozilla/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below |
18 | | #include "mozilla/MemoryReporting.h" |
19 | | #include <stdint.h> |
20 | | #include "nscore.h" |
21 | | #include "nsDataHashtable.h" |
22 | | #include "nsHashKeys.h" |
23 | | #include "nsTArray.h" |
24 | | #include "nsTHashtable.h" |
25 | | |
26 | | class nsWindowSizes; |
27 | | |
28 | | class nsPresArena { |
29 | | public: |
30 | | nsPresArena(); |
31 | | ~nsPresArena(); |
32 | | |
33 | | /** |
34 | | * Pool allocation with recycler lists indexed by frame-type ID. |
35 | | * Every aID must always be used with the same object size, aSize. |
36 | | */ |
37 | | void* AllocateByFrameID(nsQueryFrame::FrameIID aID, size_t aSize) |
38 | 0 | { |
39 | 0 | return Allocate(aID, aSize); |
40 | 0 | } |
41 | | void FreeByFrameID(nsQueryFrame::FrameIID aID, void* aPtr) |
42 | 0 | { |
43 | 0 | Free(aID, aPtr); |
44 | 0 | } |
45 | | |
46 | | /** |
47 | | * Pool allocation with recycler lists indexed by object-type ID (see above). |
48 | | * Every aID must always be used with the same object size, aSize. |
49 | | */ |
50 | | void* AllocateByObjectID(mozilla::ArenaObjectID aID, size_t aSize) |
51 | | { |
52 | | return Allocate(aID, aSize); |
53 | | } |
54 | | void FreeByObjectID(mozilla::ArenaObjectID aID, void* aPtr) |
55 | | { |
56 | | Free(aID, aPtr); |
57 | | } |
58 | | |
59 | | void* AllocateByCustomID(uint32_t aID, size_t aSize) |
60 | 0 | { |
61 | 0 | return Allocate(aID, aSize); |
62 | 0 | } |
63 | | void FreeByCustomID(uint32_t aID, void* ptr) |
64 | 0 | { |
65 | 0 | Free(aID, ptr); |
66 | 0 | } |
67 | | |
68 | | /** |
69 | | * Register an ArenaRefPtr to be cleared when this arena is about to |
70 | | * be destroyed. |
71 | | * |
72 | | * (Defined in ArenaRefPtrInlines.h.) |
73 | | * |
74 | | * @param aPtr The ArenaRefPtr to clear. |
75 | | * @param aObjectID The ArenaObjectID value that uniquely identifies |
76 | | * the type of object the ArenaRefPtr holds. |
77 | | */ |
78 | | template<typename T> |
79 | | void RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr); |
80 | | |
81 | | /** |
82 | | * Deregister an ArenaRefPtr that was previously registered with |
83 | | * RegisterArenaRefPtr. |
84 | | */ |
85 | | template<typename T> |
86 | | void DeregisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr) |
87 | | { |
88 | | MOZ_ASSERT(mArenaRefPtrs.Contains(aPtr)); |
89 | | mArenaRefPtrs.Remove(aPtr); |
90 | | } |
91 | | |
92 | | /** |
93 | | * Clears all currently registered ArenaRefPtrs. This will be called during |
94 | | * the destructor, but can be called by users of nsPresArena who want to |
95 | | * ensure arena-allocated objects are released earlier. |
96 | | */ |
97 | | void ClearArenaRefPtrs(); |
98 | | |
99 | | /** |
100 | | * Clears all currently registered ArenaRefPtrs for the given ArenaObjectID. |
101 | | * This is called when we reconstruct the rule tree so that ComputedStyles |
102 | | * pointing into the old rule tree aren't released afterwards, triggering an |
103 | | * assertion in ~ComputedStyle. |
104 | | */ |
105 | | void ClearArenaRefPtrs(mozilla::ArenaObjectID aObjectID); |
106 | | |
107 | | /** |
108 | | * Increment nsWindowSizes with sizes of interesting objects allocated in this |
109 | | * arena. |
110 | | */ |
111 | | void AddSizeOfExcludingThis(nsWindowSizes& aWindowSizes) const; |
112 | | |
113 | 0 | void Check() { |
114 | 0 | mPool.Check(); |
115 | 0 | } |
116 | | |
117 | | private: |
118 | | void* Allocate(uint32_t aCode, size_t aSize); |
119 | | void Free(uint32_t aCode, void* aPtr); |
120 | | |
121 | | inline void ClearArenaRefPtrWithoutDeregistering( |
122 | | void* aPtr, |
123 | | mozilla::ArenaObjectID aObjectID); |
124 | | |
125 | | class FreeList |
126 | | { |
127 | | public: |
128 | | nsTArray<void *> mEntries; |
129 | | size_t mEntrySize; |
130 | | size_t mEntriesEverAllocated; |
131 | | |
132 | | FreeList() |
133 | | : mEntrySize(0) |
134 | | , mEntriesEverAllocated(0) |
135 | 0 | {} |
136 | | |
137 | | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
138 | 0 | { return mEntries.ShallowSizeOfExcludingThis(aMallocSizeOf); } |
139 | | }; |
140 | | |
141 | | FreeList mFreeLists[mozilla::eArenaObjectID_COUNT]; |
142 | | mozilla::ArenaAllocator<8192, 8> mPool; |
143 | | nsDataHashtable<nsPtrHashKey<void>, mozilla::ArenaObjectID> mArenaRefPtrs; |
144 | | }; |
145 | | |
146 | | #endif |