Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 */
8 /* arena allocation for the frame tree and closely-related objects */
10 #ifndef nsPresArena_h___
11 #define nsPresArena_h___
13 #include "mozilla/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below
14 #include "mozilla/MemoryReporting.h"
15 #include <stdint.h>
16 #include "nscore.h"
17 #include "nsQueryFrame.h"
18 #include "nsTArray.h"
19 #include "nsTHashtable.h"
20 #include "plarena.h"
22 struct nsArenaMemoryStats;
24 class nsPresArena {
25 public:
26 nsPresArena();
27 ~nsPresArena();
29 enum ObjectID {
30 nsLineBox_id = nsQueryFrame::NON_FRAME_MARKER,
31 nsRuleNode_id,
32 nsStyleContext_id,
33 nsFrameList_id,
35 /**
36 * The PresArena implementation uses this bit to distinguish objects
37 * allocated by size from objects allocated by type ID (that is, frames
38 * using AllocateByFrameID and other objects using AllocateByObjectID).
39 * It should not collide with any Object ID (above) or frame ID (in
40 * nsQueryFrame.h). It is not 0x80000000 to avoid the question of
41 * whether enumeration constants are signed.
42 */
43 NON_OBJECT_MARKER = 0x40000000
44 };
46 /**
47 * Pool allocation with recycler lists indexed by object size, aSize.
48 */
49 NS_HIDDEN_(void*) AllocateBySize(size_t aSize)
50 {
51 return Allocate(uint32_t(aSize) | uint32_t(NON_OBJECT_MARKER), aSize);
52 }
53 NS_HIDDEN_(void) FreeBySize(size_t aSize, void* aPtr)
54 {
55 Free(uint32_t(aSize) | uint32_t(NON_OBJECT_MARKER), aPtr);
56 }
58 /**
59 * Pool allocation with recycler lists indexed by frame-type ID.
60 * Every aID must always be used with the same object size, aSize.
61 */
62 NS_HIDDEN_(void*) AllocateByFrameID(nsQueryFrame::FrameIID aID, size_t aSize)
63 {
64 return Allocate(aID, aSize);
65 }
66 NS_HIDDEN_(void) FreeByFrameID(nsQueryFrame::FrameIID aID, void* aPtr)
67 {
68 Free(aID, aPtr);
69 }
71 /**
72 * Pool allocation with recycler lists indexed by object-type ID (see above).
73 * Every aID must always be used with the same object size, aSize.
74 */
75 NS_HIDDEN_(void*) AllocateByObjectID(ObjectID aID, size_t aSize)
76 {
77 return Allocate(aID, aSize);
78 }
79 NS_HIDDEN_(void) FreeByObjectID(ObjectID aID, void* aPtr)
80 {
81 Free(aID, aPtr);
82 }
84 /**
85 * Increment aArenaStats with sizes of interesting objects allocated in this
86 * arena and its mOther field with the size of everything else.
87 */
88 void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
89 nsArenaMemoryStats* aArenaStats);
91 private:
92 NS_HIDDEN_(void*) Allocate(uint32_t aCode, size_t aSize);
93 NS_HIDDEN_(void) Free(uint32_t aCode, void* aPtr);
95 // All keys to this hash table fit in 32 bits (see below) so we do not
96 // bother actually hashing them.
97 class FreeList : public PLDHashEntryHdr
98 {
99 public:
100 typedef uint32_t KeyType;
101 nsTArray<void *> mEntries;
102 size_t mEntrySize;
103 size_t mEntriesEverAllocated;
105 typedef const void* KeyTypePointer;
106 KeyTypePointer mKey;
108 FreeList(KeyTypePointer aKey)
109 : mEntrySize(0), mEntriesEverAllocated(0), mKey(aKey) {}
110 // Default copy constructor and destructor are ok.
112 bool KeyEquals(KeyTypePointer const aKey) const
113 { return mKey == aKey; }
115 static KeyTypePointer KeyToPointer(KeyType aKey)
116 { return NS_INT32_TO_PTR(aKey); }
118 static PLDHashNumber HashKey(KeyTypePointer aKey)
119 { return NS_PTR_TO_INT32(aKey); }
121 enum { ALLOW_MEMMOVE = false };
122 };
124 #if defined(MOZ_HAVE_MEM_CHECKS)
125 static PLDHashOperator UnpoisonFreeList(FreeList* aEntry, void*);
126 #endif
127 static PLDHashOperator FreeListEnumerator(FreeList* aEntry, void* aData);
128 static size_t SizeOfFreeListEntryExcludingThis(FreeList* aEntry,
129 mozilla::MallocSizeOf aMallocSizeOf,
130 void*);
132 nsTHashtable<FreeList> mFreeLists;
133 PLArenaPool mPool;
134 };
136 #endif