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
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: set ts=2 sw=2 et tw=78: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | /* arena allocation for the frame tree and closely-related objects */ |
michael@0 | 9 | |
michael@0 | 10 | #ifndef nsPresArena_h___ |
michael@0 | 11 | #define nsPresArena_h___ |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below |
michael@0 | 14 | #include "mozilla/MemoryReporting.h" |
michael@0 | 15 | #include <stdint.h> |
michael@0 | 16 | #include "nscore.h" |
michael@0 | 17 | #include "nsQueryFrame.h" |
michael@0 | 18 | #include "nsTArray.h" |
michael@0 | 19 | #include "nsTHashtable.h" |
michael@0 | 20 | #include "plarena.h" |
michael@0 | 21 | |
michael@0 | 22 | struct nsArenaMemoryStats; |
michael@0 | 23 | |
michael@0 | 24 | class nsPresArena { |
michael@0 | 25 | public: |
michael@0 | 26 | nsPresArena(); |
michael@0 | 27 | ~nsPresArena(); |
michael@0 | 28 | |
michael@0 | 29 | enum ObjectID { |
michael@0 | 30 | nsLineBox_id = nsQueryFrame::NON_FRAME_MARKER, |
michael@0 | 31 | nsRuleNode_id, |
michael@0 | 32 | nsStyleContext_id, |
michael@0 | 33 | nsFrameList_id, |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * The PresArena implementation uses this bit to distinguish objects |
michael@0 | 37 | * allocated by size from objects allocated by type ID (that is, frames |
michael@0 | 38 | * using AllocateByFrameID and other objects using AllocateByObjectID). |
michael@0 | 39 | * It should not collide with any Object ID (above) or frame ID (in |
michael@0 | 40 | * nsQueryFrame.h). It is not 0x80000000 to avoid the question of |
michael@0 | 41 | * whether enumeration constants are signed. |
michael@0 | 42 | */ |
michael@0 | 43 | NON_OBJECT_MARKER = 0x40000000 |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * Pool allocation with recycler lists indexed by object size, aSize. |
michael@0 | 48 | */ |
michael@0 | 49 | NS_HIDDEN_(void*) AllocateBySize(size_t aSize) |
michael@0 | 50 | { |
michael@0 | 51 | return Allocate(uint32_t(aSize) | uint32_t(NON_OBJECT_MARKER), aSize); |
michael@0 | 52 | } |
michael@0 | 53 | NS_HIDDEN_(void) FreeBySize(size_t aSize, void* aPtr) |
michael@0 | 54 | { |
michael@0 | 55 | Free(uint32_t(aSize) | uint32_t(NON_OBJECT_MARKER), aPtr); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Pool allocation with recycler lists indexed by frame-type ID. |
michael@0 | 60 | * Every aID must always be used with the same object size, aSize. |
michael@0 | 61 | */ |
michael@0 | 62 | NS_HIDDEN_(void*) AllocateByFrameID(nsQueryFrame::FrameIID aID, size_t aSize) |
michael@0 | 63 | { |
michael@0 | 64 | return Allocate(aID, aSize); |
michael@0 | 65 | } |
michael@0 | 66 | NS_HIDDEN_(void) FreeByFrameID(nsQueryFrame::FrameIID aID, void* aPtr) |
michael@0 | 67 | { |
michael@0 | 68 | Free(aID, aPtr); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Pool allocation with recycler lists indexed by object-type ID (see above). |
michael@0 | 73 | * Every aID must always be used with the same object size, aSize. |
michael@0 | 74 | */ |
michael@0 | 75 | NS_HIDDEN_(void*) AllocateByObjectID(ObjectID aID, size_t aSize) |
michael@0 | 76 | { |
michael@0 | 77 | return Allocate(aID, aSize); |
michael@0 | 78 | } |
michael@0 | 79 | NS_HIDDEN_(void) FreeByObjectID(ObjectID aID, void* aPtr) |
michael@0 | 80 | { |
michael@0 | 81 | Free(aID, aPtr); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Increment aArenaStats with sizes of interesting objects allocated in this |
michael@0 | 86 | * arena and its mOther field with the size of everything else. |
michael@0 | 87 | */ |
michael@0 | 88 | void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf, |
michael@0 | 89 | nsArenaMemoryStats* aArenaStats); |
michael@0 | 90 | |
michael@0 | 91 | private: |
michael@0 | 92 | NS_HIDDEN_(void*) Allocate(uint32_t aCode, size_t aSize); |
michael@0 | 93 | NS_HIDDEN_(void) Free(uint32_t aCode, void* aPtr); |
michael@0 | 94 | |
michael@0 | 95 | // All keys to this hash table fit in 32 bits (see below) so we do not |
michael@0 | 96 | // bother actually hashing them. |
michael@0 | 97 | class FreeList : public PLDHashEntryHdr |
michael@0 | 98 | { |
michael@0 | 99 | public: |
michael@0 | 100 | typedef uint32_t KeyType; |
michael@0 | 101 | nsTArray<void *> mEntries; |
michael@0 | 102 | size_t mEntrySize; |
michael@0 | 103 | size_t mEntriesEverAllocated; |
michael@0 | 104 | |
michael@0 | 105 | typedef const void* KeyTypePointer; |
michael@0 | 106 | KeyTypePointer mKey; |
michael@0 | 107 | |
michael@0 | 108 | FreeList(KeyTypePointer aKey) |
michael@0 | 109 | : mEntrySize(0), mEntriesEverAllocated(0), mKey(aKey) {} |
michael@0 | 110 | // Default copy constructor and destructor are ok. |
michael@0 | 111 | |
michael@0 | 112 | bool KeyEquals(KeyTypePointer const aKey) const |
michael@0 | 113 | { return mKey == aKey; } |
michael@0 | 114 | |
michael@0 | 115 | static KeyTypePointer KeyToPointer(KeyType aKey) |
michael@0 | 116 | { return NS_INT32_TO_PTR(aKey); } |
michael@0 | 117 | |
michael@0 | 118 | static PLDHashNumber HashKey(KeyTypePointer aKey) |
michael@0 | 119 | { return NS_PTR_TO_INT32(aKey); } |
michael@0 | 120 | |
michael@0 | 121 | enum { ALLOW_MEMMOVE = false }; |
michael@0 | 122 | }; |
michael@0 | 123 | |
michael@0 | 124 | #if defined(MOZ_HAVE_MEM_CHECKS) |
michael@0 | 125 | static PLDHashOperator UnpoisonFreeList(FreeList* aEntry, void*); |
michael@0 | 126 | #endif |
michael@0 | 127 | static PLDHashOperator FreeListEnumerator(FreeList* aEntry, void* aData); |
michael@0 | 128 | static size_t SizeOfFreeListEntryExcludingThis(FreeList* aEntry, |
michael@0 | 129 | mozilla::MallocSizeOf aMallocSizeOf, |
michael@0 | 130 | void*); |
michael@0 | 131 | |
michael@0 | 132 | nsTHashtable<FreeList> mFreeLists; |
michael@0 | 133 | PLArenaPool mPool; |
michael@0 | 134 | }; |
michael@0 | 135 | |
michael@0 | 136 | #endif |