1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/base/nsPresArena.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: set ts=2 sw=2 et tw=78: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + */ 1.10 + 1.11 +/* arena allocation for the frame tree and closely-related objects */ 1.12 + 1.13 +#ifndef nsPresArena_h___ 1.14 +#define nsPresArena_h___ 1.15 + 1.16 +#include "mozilla/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below 1.17 +#include "mozilla/MemoryReporting.h" 1.18 +#include <stdint.h> 1.19 +#include "nscore.h" 1.20 +#include "nsQueryFrame.h" 1.21 +#include "nsTArray.h" 1.22 +#include "nsTHashtable.h" 1.23 +#include "plarena.h" 1.24 + 1.25 +struct nsArenaMemoryStats; 1.26 + 1.27 +class nsPresArena { 1.28 +public: 1.29 + nsPresArena(); 1.30 + ~nsPresArena(); 1.31 + 1.32 + enum ObjectID { 1.33 + nsLineBox_id = nsQueryFrame::NON_FRAME_MARKER, 1.34 + nsRuleNode_id, 1.35 + nsStyleContext_id, 1.36 + nsFrameList_id, 1.37 + 1.38 + /** 1.39 + * The PresArena implementation uses this bit to distinguish objects 1.40 + * allocated by size from objects allocated by type ID (that is, frames 1.41 + * using AllocateByFrameID and other objects using AllocateByObjectID). 1.42 + * It should not collide with any Object ID (above) or frame ID (in 1.43 + * nsQueryFrame.h). It is not 0x80000000 to avoid the question of 1.44 + * whether enumeration constants are signed. 1.45 + */ 1.46 + NON_OBJECT_MARKER = 0x40000000 1.47 + }; 1.48 + 1.49 + /** 1.50 + * Pool allocation with recycler lists indexed by object size, aSize. 1.51 + */ 1.52 + NS_HIDDEN_(void*) AllocateBySize(size_t aSize) 1.53 + { 1.54 + return Allocate(uint32_t(aSize) | uint32_t(NON_OBJECT_MARKER), aSize); 1.55 + } 1.56 + NS_HIDDEN_(void) FreeBySize(size_t aSize, void* aPtr) 1.57 + { 1.58 + Free(uint32_t(aSize) | uint32_t(NON_OBJECT_MARKER), aPtr); 1.59 + } 1.60 + 1.61 + /** 1.62 + * Pool allocation with recycler lists indexed by frame-type ID. 1.63 + * Every aID must always be used with the same object size, aSize. 1.64 + */ 1.65 + NS_HIDDEN_(void*) AllocateByFrameID(nsQueryFrame::FrameIID aID, size_t aSize) 1.66 + { 1.67 + return Allocate(aID, aSize); 1.68 + } 1.69 + NS_HIDDEN_(void) FreeByFrameID(nsQueryFrame::FrameIID aID, void* aPtr) 1.70 + { 1.71 + Free(aID, aPtr); 1.72 + } 1.73 + 1.74 + /** 1.75 + * Pool allocation with recycler lists indexed by object-type ID (see above). 1.76 + * Every aID must always be used with the same object size, aSize. 1.77 + */ 1.78 + NS_HIDDEN_(void*) AllocateByObjectID(ObjectID aID, size_t aSize) 1.79 + { 1.80 + return Allocate(aID, aSize); 1.81 + } 1.82 + NS_HIDDEN_(void) FreeByObjectID(ObjectID aID, void* aPtr) 1.83 + { 1.84 + Free(aID, aPtr); 1.85 + } 1.86 + 1.87 + /** 1.88 + * Increment aArenaStats with sizes of interesting objects allocated in this 1.89 + * arena and its mOther field with the size of everything else. 1.90 + */ 1.91 + void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf, 1.92 + nsArenaMemoryStats* aArenaStats); 1.93 + 1.94 +private: 1.95 + NS_HIDDEN_(void*) Allocate(uint32_t aCode, size_t aSize); 1.96 + NS_HIDDEN_(void) Free(uint32_t aCode, void* aPtr); 1.97 + 1.98 + // All keys to this hash table fit in 32 bits (see below) so we do not 1.99 + // bother actually hashing them. 1.100 + class FreeList : public PLDHashEntryHdr 1.101 + { 1.102 + public: 1.103 + typedef uint32_t KeyType; 1.104 + nsTArray<void *> mEntries; 1.105 + size_t mEntrySize; 1.106 + size_t mEntriesEverAllocated; 1.107 + 1.108 + typedef const void* KeyTypePointer; 1.109 + KeyTypePointer mKey; 1.110 + 1.111 + FreeList(KeyTypePointer aKey) 1.112 + : mEntrySize(0), mEntriesEverAllocated(0), mKey(aKey) {} 1.113 + // Default copy constructor and destructor are ok. 1.114 + 1.115 + bool KeyEquals(KeyTypePointer const aKey) const 1.116 + { return mKey == aKey; } 1.117 + 1.118 + static KeyTypePointer KeyToPointer(KeyType aKey) 1.119 + { return NS_INT32_TO_PTR(aKey); } 1.120 + 1.121 + static PLDHashNumber HashKey(KeyTypePointer aKey) 1.122 + { return NS_PTR_TO_INT32(aKey); } 1.123 + 1.124 + enum { ALLOW_MEMMOVE = false }; 1.125 + }; 1.126 + 1.127 +#if defined(MOZ_HAVE_MEM_CHECKS) 1.128 + static PLDHashOperator UnpoisonFreeList(FreeList* aEntry, void*); 1.129 +#endif 1.130 + static PLDHashOperator FreeListEnumerator(FreeList* aEntry, void* aData); 1.131 + static size_t SizeOfFreeListEntryExcludingThis(FreeList* aEntry, 1.132 + mozilla::MallocSizeOf aMallocSizeOf, 1.133 + void*); 1.134 + 1.135 + nsTHashtable<FreeList> mFreeLists; 1.136 + PLArenaPool mPool; 1.137 +}; 1.138 + 1.139 +#endif