Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: set ts=2 sw=2 et tw=78:
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/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below
17 : #include "mozilla/MemoryReporting.h"
18 : #include <stdint.h>
19 : #include "nscore.h"
20 : #include "nsDataHashtable.h"
21 : #include "nsHashKeys.h"
22 : #include "nsTArray.h"
23 : #include "nsTHashtable.h"
24 :
25 : struct nsArenaMemoryStats;
26 :
27 : class nsPresArena {
28 : public:
29 : nsPresArena();
30 : ~nsPresArena();
31 :
32 : /**
33 : * Pool allocation with recycler lists indexed by frame-type ID.
34 : * Every aID must always be used with the same object size, aSize.
35 : */
36 666 : void* AllocateByFrameID(nsQueryFrame::FrameIID aID, size_t aSize)
37 : {
38 666 : return Allocate(aID, aSize);
39 : }
40 126 : void FreeByFrameID(nsQueryFrame::FrameIID aID, void* aPtr)
41 : {
42 126 : Free(aID, aPtr);
43 126 : }
44 :
45 : /**
46 : * Pool allocation with recycler lists indexed by object-type ID (see above).
47 : * Every aID must always be used with the same object size, aSize.
48 : */
49 11293 : void* AllocateByObjectID(mozilla::ArenaObjectID aID, size_t aSize)
50 : {
51 11293 : return Allocate(aID, aSize);
52 : }
53 6726 : void FreeByObjectID(mozilla::ArenaObjectID aID, void* aPtr)
54 : {
55 6726 : Free(aID, aPtr);
56 6726 : }
57 :
58 : /**
59 : * Register an ArenaRefPtr to be cleared when this arena is about to
60 : * be destroyed.
61 : *
62 : * (Defined in ArenaRefPtrInlines.h.)
63 : *
64 : * @param aPtr The ArenaRefPtr to clear.
65 : * @param aObjectID The ArenaObjectID value that uniquely identifies
66 : * the type of object the ArenaRefPtr holds.
67 : */
68 : template<typename T>
69 : void RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr);
70 :
71 : /**
72 : * Deregister an ArenaRefPtr that was previously registered with
73 : * RegisterArenaRefPtr.
74 : */
75 : template<typename T>
76 4 : void DeregisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
77 : {
78 4 : MOZ_ASSERT(mArenaRefPtrs.Contains(aPtr));
79 4 : mArenaRefPtrs.Remove(aPtr);
80 4 : }
81 :
82 : /**
83 : * Clears all currently registered ArenaRefPtrs. This will be called during
84 : * the destructor, but can be called by users of nsPresArena who want to
85 : * ensure arena-allocated objects are released earlier.
86 : */
87 : void ClearArenaRefPtrs();
88 :
89 : /**
90 : * Clears all currently registered ArenaRefPtrs for the given ArenaObjectID.
91 : * This is called when we reconstruct the rule tree so that style contexts
92 : * pointing into the old rule tree aren't released afterwards, triggering an
93 : * assertion in ~nsStyleContext.
94 : */
95 : void ClearArenaRefPtrs(mozilla::ArenaObjectID aObjectID);
96 :
97 : /**
98 : * Increment aArenaStats with sizes of interesting objects allocated in this
99 : * arena and its mOther field with the size of everything else.
100 : */
101 : void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
102 : nsArenaMemoryStats* aArenaStats);
103 :
104 : private:
105 : void* Allocate(uint32_t aCode, size_t aSize);
106 : void Free(uint32_t aCode, void* aPtr);
107 :
108 : inline void ClearArenaRefPtrWithoutDeregistering(
109 : void* aPtr,
110 : mozilla::ArenaObjectID aObjectID);
111 :
112 852 : class FreeList
113 : {
114 : public:
115 : nsTArray<void *> mEntries;
116 : size_t mEntrySize;
117 : size_t mEntriesEverAllocated;
118 :
119 5964 : FreeList()
120 5964 : : mEntrySize(0)
121 5964 : , mEntriesEverAllocated(0)
122 5964 : {}
123 :
124 4473 : size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
125 4473 : { return mEntries.ShallowSizeOfExcludingThis(aMallocSizeOf); }
126 : };
127 :
128 : FreeList mFreeLists[mozilla::eArenaObjectID_COUNT];
129 : mozilla::ArenaAllocator<8192, 8> mPool;
130 : nsDataHashtable<nsPtrHashKey<void>, mozilla::ArenaObjectID> mArenaRefPtrs;
131 : };
132 :
133 : #endif
|