2009-08-18 07:21:06 +04: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 15:12:37 +04: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 07:21:06 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* arena allocation for the frame tree and closely-related objects */
|
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
// Even on 32-bit systems, we allocate objects from the frame arena
|
|
|
|
// that require 8-byte alignment. The cast to uintptr_t is needed
|
|
|
|
// because plarena isn't as careful about mask construction as it
|
|
|
|
// ought to be.
|
|
|
|
#define ALIGN_SHIFT 3
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK ((uintptr_t(1) << ALIGN_SHIFT) - 1)
|
|
|
|
#include "plarena.h"
|
|
|
|
// plarena.h needs to be included first to make it use the above
|
|
|
|
// PL_ARENA_CONST_ALIGN_MASK in this file.
|
|
|
|
|
2009-08-18 07:21:06 +04:00
|
|
|
#include "nsPresArena.h"
|
2013-05-07 22:48:59 +04:00
|
|
|
|
|
|
|
#include "mozilla/Poison.h"
|
2009-08-18 07:21:06 +04:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsDebug.h"
|
2012-06-06 21:29:16 +04:00
|
|
|
#include "nsArenaMemoryStats.h"
|
2009-12-02 01:48:23 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2012-08-30 05:27:01 +04:00
|
|
|
|
2009-09-15 04:26:08 +04:00
|
|
|
// Size to use for PLArena block allocations.
|
2011-08-22 10:34:11 +04:00
|
|
|
static const size_t ARENA_PAGE_SIZE = 8192;
|
2009-09-15 01:29:56 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
nsPresArena::nsPresArena()
|
2009-09-16 02:00:04 +04:00
|
|
|
{
|
2013-04-16 00:00:06 +04:00
|
|
|
mFreeLists.Init();
|
|
|
|
PL_INIT_ARENA_POOL(&mPool, "PresArena", ARENA_PAGE_SIZE);
|
2009-11-17 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
nsPresArena::~nsPresArena()
|
|
|
|
{
|
2013-02-15 03:50:11 +04:00
|
|
|
#if defined(MOZ_HAVE_MEM_CHECKS)
|
2013-04-16 00:00:06 +04:00
|
|
|
mFreeLists.EnumerateEntries(UnpoisonFreeList, nullptr);
|
2013-02-03 05:04:29 +04:00
|
|
|
#endif
|
2013-04-16 00:00:06 +04:00
|
|
|
PL_FinishArenaPool(&mPool);
|
|
|
|
}
|
2009-08-18 07:21:06 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
NS_HIDDEN_(void*)
|
2013-04-16 00:00:06 +04:00
|
|
|
nsPresArena::Allocate(uint32_t aCode, size_t aSize)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aSize > 0, "PresArena cannot allocate zero bytes");
|
2009-08-18 07:21:06 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
// We only hand out aligned sizes
|
|
|
|
aSize = PL_ARENA_ALIGN(&mPool, aSize);
|
2009-08-18 07:21:06 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
// If there is no free-list entry for this type already, we have
|
|
|
|
// to create one now, to record its size.
|
|
|
|
FreeList* list = mFreeLists.PutEntry(aCode);
|
2009-09-15 01:29:56 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
nsTArray<void*>::index_type len = list->mEntries.Length();
|
|
|
|
if (list->mEntrySize == 0) {
|
|
|
|
NS_ABORT_IF_FALSE(len == 0, "list with entries but no recorded size");
|
|
|
|
list->mEntrySize = aSize;
|
|
|
|
} else {
|
|
|
|
NS_ABORT_IF_FALSE(list->mEntrySize == aSize,
|
|
|
|
"different sizes for same object type code");
|
|
|
|
}
|
2009-09-16 02:00:04 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
void* result;
|
|
|
|
if (len > 0) {
|
|
|
|
// LIFO behavior for best cache utilization
|
|
|
|
result = list->mEntries.ElementAt(len - 1);
|
|
|
|
list->mEntries.RemoveElementAt(len - 1);
|
2013-02-15 03:50:11 +04:00
|
|
|
#if defined(DEBUG)
|
2013-04-16 00:00:06 +04:00
|
|
|
{
|
|
|
|
MOZ_MAKE_MEM_DEFINED(result, list->mEntrySize);
|
|
|
|
char* p = reinterpret_cast<char*>(result);
|
|
|
|
char* limit = p + list->mEntrySize;
|
|
|
|
for (; p < limit; p += sizeof(uintptr_t)) {
|
|
|
|
uintptr_t val = *reinterpret_cast<uintptr_t*>(p);
|
2013-05-07 22:48:59 +04:00
|
|
|
NS_ABORT_IF_FALSE(val == mozPoisonValue(),
|
2013-04-16 00:00:06 +04:00
|
|
|
nsPrintfCString("PresArena: poison overwritten; "
|
|
|
|
"wanted %.16llx "
|
|
|
|
"found %.16llx "
|
|
|
|
"errors in bits %.16llx",
|
2013-05-07 22:48:59 +04:00
|
|
|
uint64_t(mozPoisonValue()),
|
2013-04-16 00:00:06 +04:00
|
|
|
uint64_t(val),
|
2013-05-07 22:48:59 +04:00
|
|
|
uint64_t(mozPoisonValue() ^ val)
|
2013-04-16 00:00:06 +04:00
|
|
|
).get());
|
2009-09-16 02:00:04 +04:00
|
|
|
}
|
2012-11-07 23:55:53 +04:00
|
|
|
}
|
2013-04-16 00:00:06 +04:00
|
|
|
#endif
|
|
|
|
MOZ_MAKE_MEM_UNDEFINED(result, list->mEntrySize);
|
2009-08-18 07:21:06 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
// Allocate a new chunk from the arena
|
|
|
|
list->mEntriesEverAllocated++;
|
|
|
|
PL_ARENA_ALLOCATE(result, &mPool, aSize);
|
|
|
|
if (!result) {
|
|
|
|
NS_RUNTIMEABORT("out of memory");
|
2009-08-18 07:21:06 +04:00
|
|
|
}
|
2013-04-16 00:00:06 +04:00
|
|
|
return result;
|
|
|
|
}
|
2010-06-01 06:19:35 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
NS_HIDDEN_(void)
|
2013-04-16 00:00:06 +04:00
|
|
|
nsPresArena::Free(uint32_t aCode, void* aPtr)
|
|
|
|
{
|
|
|
|
// Try to recycle this entry.
|
|
|
|
FreeList* list = mFreeLists.GetEntry(aCode);
|
|
|
|
NS_ABORT_IF_FALSE(list, "no free list for pres arena object");
|
|
|
|
NS_ABORT_IF_FALSE(list->mEntrySize > 0, "PresArena cannot free zero bytes");
|
|
|
|
|
2013-05-07 22:48:59 +04:00
|
|
|
mozWritePoison(aPtr, list->mEntrySize);
|
2012-06-05 20:33:54 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
MOZ_MAKE_MEM_NOACCESS(aPtr, list->mEntrySize);
|
|
|
|
list->mEntries.AppendElement(aPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ size_t
|
|
|
|
nsPresArena::SizeOfFreeListEntryExcludingThis(
|
|
|
|
FreeList* aEntry, nsMallocSizeOfFun aMallocSizeOf, void*)
|
|
|
|
{
|
|
|
|
return aEntry->mEntries.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EnumerateData {
|
|
|
|
nsArenaMemoryStats* stats;
|
|
|
|
size_t total;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(MOZ_HAVE_MEM_CHECKS)
|
|
|
|
/* static */ PLDHashOperator
|
|
|
|
nsPresArena::UnpoisonFreeList(FreeList* aEntry, void*)
|
|
|
|
{
|
|
|
|
nsTArray<void*>::index_type len;
|
|
|
|
while ((len = aEntry->mEntries.Length())) {
|
|
|
|
void* result = aEntry->mEntries.ElementAt(len - 1);
|
|
|
|
aEntry->mEntries.RemoveElementAt(len - 1);
|
|
|
|
MOZ_MAKE_MEM_UNDEFINED(result, aEntry->mEntrySize);
|
2010-06-01 06:19:35 +04:00
|
|
|
}
|
2013-04-16 00:00:06 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
#endif
|
2012-06-06 21:29:16 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
/* static */ PLDHashOperator
|
|
|
|
nsPresArena::FreeListEnumerator(FreeList* aEntry, void* aData)
|
|
|
|
{
|
|
|
|
EnumerateData* data = static_cast<EnumerateData*>(aData);
|
|
|
|
// Note that we're not measuring the size of the entries on the free
|
|
|
|
// list here. The free list knows how many objects we've allocated
|
|
|
|
// ever (which includes any objects that may be on the FreeList's
|
|
|
|
// |mEntries| at this point) and we're using that to determine the
|
|
|
|
// total size of objects allocated with a given ID.
|
|
|
|
size_t totalSize = aEntry->mEntrySize * aEntry->mEntriesEverAllocated;
|
|
|
|
size_t* p;
|
|
|
|
|
|
|
|
switch (NS_PTR_TO_INT32(aEntry->mKey)) {
|
|
|
|
#define FRAME_ID(classname) \
|
|
|
|
case nsQueryFrame::classname##_id: \
|
|
|
|
p = &data->stats->FRAME_ID_STAT_FIELD(classname); \
|
|
|
|
break;
|
2012-06-06 21:29:16 +04:00
|
|
|
#include "nsFrameIdList.h"
|
|
|
|
#undef FRAME_ID
|
2013-04-16 00:00:06 +04:00
|
|
|
case nsLineBox_id:
|
|
|
|
p = &data->stats->mLineBoxes;
|
|
|
|
break;
|
|
|
|
case nsRuleNode_id:
|
|
|
|
p = &data->stats->mRuleNodes;
|
|
|
|
break;
|
|
|
|
case nsStyleContext_id:
|
|
|
|
p = &data->stats->mStyleContexts;
|
|
|
|
break;
|
|
|
|
default:
|
2012-06-06 21:29:16 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
*p += totalSize;
|
|
|
|
data->total += totalSize;
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
2010-06-01 06:19:35 +04:00
|
|
|
|
2012-06-06 21:29:16 +04:00
|
|
|
void
|
|
|
|
nsPresArena::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
|
|
|
|
nsArenaMemoryStats* aArenaStats)
|
2011-12-09 09:01:52 +04:00
|
|
|
{
|
2013-04-16 00:00:06 +04:00
|
|
|
// We do a complicated dance here because we want to measure the
|
|
|
|
// space taken up by the different kinds of objects in the arena,
|
|
|
|
// but we don't have pointers to those objects. And even if we did,
|
|
|
|
// we wouldn't be able to use aMallocSizeOf on them, since they were
|
|
|
|
// allocated out of malloc'd chunks of memory. So we compute the
|
|
|
|
// size of the arena as known by malloc and we add up the sizes of
|
|
|
|
// all the objects that we care about. Subtracting these two
|
|
|
|
// quantities gives us a catch-all "other" number, which includes
|
|
|
|
// slop in the arena itself as well as the size of objects that
|
|
|
|
// we've not measured explicitly.
|
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
size_t mallocSize = PL_SizeOfArenaPoolExcludingPool(&mPool, aMallocSizeOf);
|
|
|
|
mallocSize += mFreeLists.SizeOfExcludingThis(SizeOfFreeListEntryExcludingThis,
|
|
|
|
aMallocSizeOf);
|
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
EnumerateData data = { aArenaStats, 0 };
|
|
|
|
mFreeLists.EnumerateEntries(FreeListEnumerator, &data);
|
|
|
|
aArenaStats->mOther = mallocSize - data.total;
|
2009-08-18 07:21:06 +04:00
|
|
|
}
|