2
* Copyright (C) 2014-2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#include "HeapConstants.h"
31
DEFINE_STATIC_PER_PROCESS_STORAGE(HeapConstants);
33
HeapConstants::HeapConstants(const LockHolder&)
34
: m_vmPageSizePhysical { vmPageSizePhysical() }
36
RELEASE_BASSERT(m_vmPageSizePhysical >= smallPageSize);
37
RELEASE_BASSERT(vmPageSize() >= m_vmPageSizePhysical);
39
initializeLineMetadata();
40
initializePageMetadata();
44
constexpr void fillLineMetadata(C& container, size_t VMPageSize)
46
constexpr size_t clsCount = sizeClass(smallLineSize);
47
size_t lineCount = smallLineCount(VMPageSize);
49
for (size_t cls = 0; cls < clsCount; ++cls) {
50
size_t size = objectSize(cls);
51
size_t baseIndex = cls * lineCount;
53
while (object < VMPageSize) {
54
size_t line = object / smallLineSize;
55
size_t leftover = object % smallLineSize;
57
auto objectCount = divideRoundingUp(smallLineSize - leftover, size);
59
object += objectCount * size;
61
// Don't allow the last object in a page to escape the page.
62
if (object > VMPageSize) {
67
container[baseIndex + line] = { static_cast<unsigned char>(leftover), static_cast<unsigned char>(objectCount) };
72
template <size_t VMPageSize>
73
constexpr auto computeLineMetadata()
75
std::array<LineMetadata, sizeClass(smallLineSize) * smallLineCount(VMPageSize)> result;
76
fillLineMetadata(result, VMPageSize);
80
#if BUSE(PRECOMPUTED_CONSTANTS_VMPAGE4K)
81
constexpr auto kPrecalcuratedLineMetadata4k = computeLineMetadata<4 * kB>();
84
#if BUSE(PRECOMPUTED_CONSTANTS_VMPAGE16K)
85
constexpr auto kPrecalcuratedLineMetadata16k = computeLineMetadata<16 * kB>();
88
void HeapConstants::initializeLineMetadata()
90
#if BUSE(PRECOMPUTED_CONSTANTS_VMPAGE4K)
91
if (m_vmPageSizePhysical == 4 * kB) {
92
m_smallLineMetadata = &kPrecalcuratedLineMetadata4k[0];
97
#if BUSE(PRECOMPUTED_CONSTANTS_VMPAGE16K)
98
if (m_vmPageSizePhysical == 16 * kB) {
99
m_smallLineMetadata = &kPrecalcuratedLineMetadata16k[0];
104
size_t sizeClassCount = bmalloc::sizeClass(smallLineSize);
105
m_smallLineMetadataStorage.grow(sizeClassCount * smallLineCount());
106
fillLineMetadata(m_smallLineMetadataStorage, m_vmPageSizePhysical);
107
m_smallLineMetadata = &m_smallLineMetadataStorage[0];
110
void HeapConstants::initializePageMetadata()
112
auto computePageSize = [&](size_t sizeClass) {
113
size_t size = objectSize(sizeClass);
114
if (sizeClass < bmalloc::sizeClass(smallLineSize))
115
return m_vmPageSizePhysical;
117
for (size_t pageSize = m_vmPageSizePhysical; pageSize < pageSizeMax; pageSize += m_vmPageSizePhysical) {
118
RELEASE_BASSERT(pageSize <= chunkSize / 2);
119
size_t waste = pageSize % size;
120
if (waste <= pageSize / pageSizeWasteFactor)
127
for (size_t i = 0; i < sizeClassCount; ++i)
128
m_pageClasses[i] = (computePageSize(i) - 1) / smallPageSize;
131
} // namespace bmalloc