2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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 */
|
|
|
|
|
|
|
|
#include "nsPresArena.h"
|
2013-05-07 22:48:59 +04:00
|
|
|
|
|
|
|
#include "mozilla/Poison.h"
|
2009-08-18 07:21:06 +04:00
|
|
|
#include "nsDebug.h"
|
2019-10-24 01:10:11 +03:00
|
|
|
#include "nsDisplayList.h"
|
2009-12-02 01:48:23 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2017-05-24 16:41:41 +03:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2018-03-22 21:20:41 +03:00
|
|
|
#include "mozilla/ComputedStyle.h"
|
|
|
|
#include "mozilla/ComputedStyleInlines.h"
|
2017-08-10 07:14:09 +03:00
|
|
|
#include "nsWindowSizes.h"
|
2012-08-30 05:27:01 +04:00
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2015-09-17 05:08:19 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
template <size_t ArenaSize, typename ObjectId, size_t ObjectIdCount>
|
|
|
|
nsPresArena<ArenaSize, ObjectId, ObjectIdCount>::~nsPresArena() {
|
2013-02-15 03:50:11 +04:00
|
|
|
#if defined(MOZ_HAVE_MEM_CHECKS)
|
2017-05-24 16:41:41 +03:00
|
|
|
for (FreeList* entry = mFreeLists; entry != ArrayEnd(mFreeLists); ++entry) {
|
2020-06-10 13:46:14 +03:00
|
|
|
for (void* result : entry->mEntries) {
|
2015-07-15 12:05:06 +03:00
|
|
|
MOZ_MAKE_MEM_UNDEFINED(result, entry->mEntrySize);
|
|
|
|
}
|
2020-06-10 13:46:14 +03:00
|
|
|
entry->mEntries.Clear();
|
2015-07-15 12:05:06 +03:00
|
|
|
}
|
2013-02-03 05:04:29 +04:00
|
|
|
#endif
|
2013-04-16 00:00:06 +04:00
|
|
|
}
|
2009-08-18 07:21:06 +04:00
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
template <size_t ArenaSize, typename ObjectId, size_t ObjectIdCount>
|
|
|
|
void* nsPresArena<ArenaSize, ObjectId, ObjectIdCount>::Allocate(ObjectId aCode,
|
|
|
|
size_t aSize) {
|
2018-06-07 23:39:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aSize > 0, "PresArena cannot allocate zero bytes");
|
2019-07-02 05:46:09 +03:00
|
|
|
MOZ_ASSERT(size_t(aCode) < ArrayLength(mFreeLists));
|
2009-08-18 07:21:06 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
// We only hand out aligned sizes
|
2017-04-01 00:06:33 +03:00
|
|
|
aSize = mPool.AlignedSize(aSize);
|
2009-08-18 07:21:06 +04:00
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
FreeList* list = &mFreeLists[size_t(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) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(len == 0, "list with entries but no recorded size");
|
2013-04-16 00:00:06 +04:00
|
|
|
list->mEntrySize = aSize;
|
|
|
|
} else {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(list->mEntrySize == aSize,
|
|
|
|
"different sizes for same object type code");
|
2013-04-16 00:00:06 +04:00
|
|
|
}
|
2009-09-16 02:00:04 +04:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
void* result;
|
|
|
|
if (len > 0) {
|
2017-06-24 20:23:07 +03:00
|
|
|
// Remove from the end of the mEntries array to avoid memmoving entries,
|
|
|
|
// and use SetLengthAndRetainStorage to avoid a lot of malloc/free
|
|
|
|
// from ShrinkCapacity on smaller sizes. 500 pointers means the malloc size
|
|
|
|
// for the array is 4096 bytes or more on a 64-bit system. The next smaller
|
|
|
|
// size is 2048 (with jemalloc), which we consider not worth compacting.
|
2013-04-16 00:00:06 +04:00
|
|
|
result = list->mEntries.ElementAt(len - 1);
|
2017-06-24 20:23:07 +03:00
|
|
|
if (list->mEntries.Capacity() > 500) {
|
|
|
|
list->mEntries.RemoveElementAt(len - 1);
|
|
|
|
} else {
|
|
|
|
list->mEntries.SetLengthAndRetainStorage(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);
|
2015-02-10 04:05:37 +03:00
|
|
|
if (val != mozPoisonValue()) {
|
|
|
|
MOZ_ReportAssertionFailure(
|
|
|
|
nsPrintfCString("PresArena: poison overwritten; "
|
|
|
|
"wanted %.16" PRIx64 " "
|
|
|
|
"found %.16" PRIx64 " "
|
|
|
|
"errors in bits %.16" PRIx64 " ",
|
|
|
|
uint64_t(mozPoisonValue()), uint64_t(val),
|
|
|
|
uint64_t(mozPoisonValue() ^ val))
|
|
|
|
.get(),
|
|
|
|
__FILE__, __LINE__);
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
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++;
|
2017-04-01 00:06:33 +03:00
|
|
|
return mPool.Allocate(aSize);
|
2013-04-16 00:00:06 +04:00
|
|
|
}
|
2010-06-01 06:19:35 +04:00
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
template <size_t ArenaSize, typename ObjectId, size_t ObjectIdCount>
|
|
|
|
void nsPresArena<ArenaSize, ObjectId, ObjectIdCount>::Free(ObjectId aCode,
|
|
|
|
void* aPtr) {
|
2018-06-07 23:39:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-07-02 05:46:09 +03:00
|
|
|
MOZ_ASSERT(size_t(aCode) < ArrayLength(mFreeLists));
|
2017-05-24 16:41:41 +03:00
|
|
|
|
2013-04-16 00:00:06 +04:00
|
|
|
// Try to recycle this entry.
|
2019-07-02 05:46:09 +03:00
|
|
|
FreeList* list = &mFreeLists[size_t(aCode)];
|
2017-05-24 16:41:41 +03:00
|
|
|
MOZ_ASSERT(list->mEntrySize > 0, "object of this type was never allocated");
|
2013-04-16 00:00:06 +04:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
template <size_t ArenaSize, typename ObjectId, size_t ObjectIdCount>
|
|
|
|
void nsPresArena<ArenaSize, ObjectId, ObjectIdCount>::AddSizeOfExcludingThis(
|
|
|
|
nsWindowSizes& aSizes, ArenaKind aKind) const {
|
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,
|
2017-08-10 07:14:09 +03:00
|
|
|
// we wouldn't be able to use mMallocSizeOf on them, since they were
|
2013-04-16 00:00:06 +04:00
|
|
|
// 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.
|
|
|
|
|
2017-08-10 07:14:09 +03:00
|
|
|
size_t mallocSize = mPool.SizeOfExcludingThis(aSizes.mState.mMallocSizeOf);
|
2013-04-16 00:00:06 +04:00
|
|
|
|
2015-07-15 12:05:06 +03:00
|
|
|
size_t totalSizeInFreeLists = 0;
|
2017-08-10 07:14:09 +03:00
|
|
|
for (const FreeList* entry = mFreeLists; entry != ArrayEnd(mFreeLists);
|
|
|
|
++entry) {
|
|
|
|
mallocSize += entry->SizeOfExcludingThis(aSizes.mState.mMallocSizeOf);
|
2015-07-15 12:05:06 +03:00
|
|
|
|
|
|
|
// 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 = entry->mEntrySize * entry->mEntriesEverAllocated;
|
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
if (aKind == ArenaKind::PresShell) {
|
|
|
|
switch (entry - mFreeLists) {
|
|
|
|
#define PRES_ARENA_OBJECT(name_) \
|
|
|
|
case eArenaObjectID_##name_: \
|
|
|
|
aSizes.mArenaSizes.NS_ARENA_SIZES_FIELD(name_) += totalSize; \
|
2015-07-15 12:05:06 +03:00
|
|
|
break;
|
2019-07-02 05:46:09 +03:00
|
|
|
#include "nsPresArenaObjectList.h"
|
|
|
|
#undef PRES_ARENA_OBJECT
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown arena object type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aKind == ArenaKind::DisplayList);
|
|
|
|
switch (DisplayListArenaObjectId(entry - mFreeLists)) {
|
|
|
|
#define DISPLAY_LIST_ARENA_OBJECT(name_) \
|
|
|
|
case DisplayListArenaObjectId::name_: \
|
|
|
|
aSizes.mArenaSizes.NS_ARENA_SIZES_FIELD(name_) += totalSize; \
|
|
|
|
break;
|
|
|
|
#include "nsDisplayListArenaTypes.h"
|
|
|
|
#undef DISPLAY_LIST_ARENA_OBJECT
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown display item arena type");
|
|
|
|
}
|
2015-07-15 12:05:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
totalSizeInFreeLists += totalSize;
|
|
|
|
}
|
|
|
|
|
2019-07-02 05:46:09 +03:00
|
|
|
auto& field = aKind == ArenaKind::PresShell
|
|
|
|
? aSizes.mLayoutPresShellSize
|
|
|
|
: aSizes.mLayoutRetainedDisplayListSize;
|
|
|
|
|
|
|
|
field += mallocSize - totalSizeInFreeLists;
|
2009-08-18 07:21:06 +04:00
|
|
|
}
|
2018-12-12 22:09:14 +03:00
|
|
|
|
|
|
|
// Explicitly instantiate templates for the used nsPresArena allocator sizes.
|
|
|
|
// This is needed because nsPresArena definition is split across multiple files.
|
2019-07-02 05:46:09 +03:00
|
|
|
template class nsPresArena<8192, ArenaObjectID, eArenaObjectID_COUNT>;
|
|
|
|
template class nsPresArena<32768, DisplayListArenaObjectId,
|
|
|
|
size_t(DisplayListArenaObjectId::COUNT)>;
|