2009-08-18 03:21:06 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: set ts=2 sw=2 et tw=78:
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-08-18 03:21:06 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-15 20:00:06 +00:00
|
|
|
/* arena allocation for the frame tree and closely-related objects */
|
|
|
|
|
2009-08-18 03:21:06 +00:00
|
|
|
#ifndef nsPresArena_h___
|
|
|
|
#define nsPresArena_h___
|
|
|
|
|
2015-09-17 02:08:19 +00:00
|
|
|
#include "mozilla/ArenaObjectID.h"
|
2015-09-17 02:08:19 +00:00
|
|
|
#include "mozilla/ArenaRefPtr.h"
|
2013-08-20 21:48:00 +00:00
|
|
|
#include "mozilla/MemoryChecking.h" // Note: Do not remove this, needed for MOZ_HAVE_MEM_CHECKS below
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-07-30 14:25:31 +00:00
|
|
|
#include <stdint.h>
|
2009-08-18 03:21:06 +00:00
|
|
|
#include "nscore.h"
|
2015-09-17 02:08:19 +00:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2013-04-15 20:00:06 +00:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "plarena.h"
|
2012-04-12 00:17:44 +00:00
|
|
|
|
2012-06-13 14:43:38 +00:00
|
|
|
struct nsArenaMemoryStats;
|
2012-06-06 17:29:16 +00:00
|
|
|
|
2009-08-18 03:21:06 +00:00
|
|
|
class nsPresArena {
|
|
|
|
public:
|
|
|
|
nsPresArena();
|
|
|
|
~nsPresArena();
|
|
|
|
|
2013-04-15 20:00:06 +00:00
|
|
|
/**
|
|
|
|
* Pool allocation with recycler lists indexed by object size, aSize.
|
|
|
|
*/
|
2014-06-02 12:08:21 +00:00
|
|
|
void* AllocateBySize(size_t aSize)
|
2013-04-15 20:00:06 +00:00
|
|
|
{
|
2015-09-17 02:08:19 +00:00
|
|
|
return Allocate(uint32_t(aSize) |
|
|
|
|
uint32_t(mozilla::eArenaObjectID_NON_OBJECT_MARKER), aSize);
|
2013-04-15 20:00:06 +00:00
|
|
|
}
|
2014-06-02 12:08:21 +00:00
|
|
|
void FreeBySize(size_t aSize, void* aPtr)
|
2013-04-15 20:00:06 +00:00
|
|
|
{
|
2015-09-17 02:08:19 +00:00
|
|
|
Free(uint32_t(aSize) |
|
|
|
|
uint32_t(mozilla::eArenaObjectID_NON_OBJECT_MARKER), aPtr);
|
2013-04-15 20:00:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 20:00:06 +00:00
|
|
|
/**
|
|
|
|
* Pool allocation with recycler lists indexed by frame-type ID.
|
|
|
|
* Every aID must always be used with the same object size, aSize.
|
|
|
|
*/
|
2014-06-02 12:08:21 +00:00
|
|
|
void* AllocateByFrameID(nsQueryFrame::FrameIID aID, size_t aSize)
|
2013-04-15 20:00:06 +00:00
|
|
|
{
|
|
|
|
return Allocate(aID, aSize);
|
|
|
|
}
|
2014-06-02 12:08:21 +00:00
|
|
|
void FreeByFrameID(nsQueryFrame::FrameIID aID, void* aPtr)
|
2013-04-15 20:00:06 +00:00
|
|
|
{
|
|
|
|
Free(aID, aPtr);
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:00:06 +00:00
|
|
|
/**
|
|
|
|
* Pool allocation with recycler lists indexed by object-type ID (see above).
|
|
|
|
* Every aID must always be used with the same object size, aSize.
|
|
|
|
*/
|
2015-09-17 02:08:19 +00:00
|
|
|
void* AllocateByObjectID(mozilla::ArenaObjectID aID, size_t aSize)
|
2013-04-15 20:00:06 +00:00
|
|
|
{
|
|
|
|
return Allocate(aID, aSize);
|
|
|
|
}
|
2015-09-17 02:08:19 +00:00
|
|
|
void FreeByObjectID(mozilla::ArenaObjectID aID, void* aPtr)
|
2013-04-15 20:00:06 +00:00
|
|
|
{
|
|
|
|
Free(aID, aPtr);
|
|
|
|
}
|
2012-05-04 00:14:02 +00:00
|
|
|
|
2015-09-17 02:08:19 +00:00
|
|
|
/**
|
|
|
|
* Register an ArenaRefPtr to be cleared when this arena is about to
|
|
|
|
* be destroyed.
|
|
|
|
*
|
|
|
|
* (Defined in ArenaRefPtrInlines.h.)
|
|
|
|
*
|
|
|
|
* @param aPtr The ArenaRefPtr to clear.
|
2015-09-17 02:08:20 +00:00
|
|
|
* @param aObjectID The ArenaObjectID value that uniquely identifies
|
2015-09-17 02:08:19 +00:00
|
|
|
* the type of object the ArenaRefPtr holds.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void RegisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister an ArenaRefPtr that was previously registered with
|
|
|
|
* RegisterArenaRefPtr.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void DeregisterArenaRefPtr(mozilla::ArenaRefPtr<T>* aPtr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mArenaRefPtrs.Contains(aPtr));
|
|
|
|
mArenaRefPtrs.Remove(aPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all currently registered ArenaRefPtrs. This will be called during
|
|
|
|
* the destructor, but can be called by users of nsPresArena who want to
|
|
|
|
* ensure arena-allocated objects are released earlier.
|
|
|
|
*/
|
|
|
|
void ClearArenaRefPtrs();
|
|
|
|
|
2015-09-17 02:08:20 +00:00
|
|
|
/**
|
|
|
|
* Clears all currently registered ArenaRefPtrs for the given ArenaObjectID.
|
|
|
|
* This is called when we reconstruct the rule tree so that style contexts
|
|
|
|
* pointing into the old rule tree aren't released afterwards, triggering an
|
|
|
|
* assertion in ~nsStyleContext.
|
|
|
|
*/
|
|
|
|
void ClearArenaRefPtrs(mozilla::ArenaObjectID aObjectID);
|
|
|
|
|
2012-06-06 17:29:16 +00:00
|
|
|
/**
|
2013-09-30 23:20:23 +00:00
|
|
|
* Increment aArenaStats with sizes of interesting objects allocated in this
|
|
|
|
* arena and its mOther field with the size of everything else.
|
2012-06-06 17:29:16 +00:00
|
|
|
*/
|
2013-09-30 23:20:23 +00:00
|
|
|
void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
|
|
|
|
nsArenaMemoryStats* aArenaStats);
|
2010-06-01 02:19:35 +00:00
|
|
|
|
2009-08-18 03:21:06 +00:00
|
|
|
private:
|
2014-06-02 12:08:21 +00:00
|
|
|
void* Allocate(uint32_t aCode, size_t aSize);
|
|
|
|
void Free(uint32_t aCode, void* aPtr);
|
2013-04-15 20:00:06 +00:00
|
|
|
|
2015-09-17 02:08:19 +00:00
|
|
|
inline void ClearArenaRefPtrWithoutDeregistering(
|
|
|
|
void* aPtr,
|
|
|
|
mozilla::ArenaObjectID aObjectID);
|
|
|
|
|
2013-04-15 20:00:06 +00:00
|
|
|
// All keys to this hash table fit in 32 bits (see below) so we do not
|
|
|
|
// bother actually hashing them.
|
|
|
|
class FreeList : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef uint32_t KeyType;
|
|
|
|
nsTArray<void *> mEntries;
|
|
|
|
size_t mEntrySize;
|
|
|
|
size_t mEntriesEverAllocated;
|
|
|
|
|
|
|
|
typedef const void* KeyTypePointer;
|
|
|
|
KeyTypePointer mKey;
|
|
|
|
|
2014-08-07 23:48:38 +00:00
|
|
|
explicit FreeList(KeyTypePointer aKey)
|
2013-04-15 20:00:06 +00:00
|
|
|
: mEntrySize(0), mEntriesEverAllocated(0), mKey(aKey) {}
|
|
|
|
// Default copy constructor and destructor are ok.
|
|
|
|
|
|
|
|
bool KeyEquals(KeyTypePointer const aKey) const
|
|
|
|
{ return mKey == aKey; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey)
|
|
|
|
{ return NS_INT32_TO_PTR(aKey); }
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{ return NS_PTR_TO_INT32(aKey); }
|
|
|
|
|
2014-08-05 20:27:41 +00:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2015-07-29 06:24:24 +00:00
|
|
|
{ return mEntries.ShallowSizeOfExcludingThis(aMallocSizeOf); }
|
2014-08-05 20:27:41 +00:00
|
|
|
|
2013-04-15 20:00:06 +00:00
|
|
|
enum { ALLOW_MEMMOVE = false };
|
|
|
|
};
|
|
|
|
|
|
|
|
nsTHashtable<FreeList> mFreeLists;
|
|
|
|
PLArenaPool mPool;
|
2015-09-17 02:08:19 +00:00
|
|
|
nsDataHashtable<nsPtrHashKey<void>, mozilla::ArenaObjectID> mArenaRefPtrs;
|
2009-08-18 03:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|