forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitfitTests.cpp
More file actions
169 lines (140 loc) · 5.79 KB
/
BitfitTests.cpp
File metadata and controls
169 lines (140 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TestHarness.h"
#if PAS_ENABLE_ISO
#include "iso_heap.h"
#include "iso_heap_innards.h"
#include "pas_bitfit_heap.h"
#include "pas_bitfit_size_class.h"
#include "pas_heap.h"
#include <vector>
using namespace std;
namespace {
vector<size_t> getBitfitSizeClasses()
{
vector<size_t> result;
pas_bitfit_heap* heap = pas_compact_atomic_bitfit_heap_ptr_load_non_null(
&iso_common_primitive_heap.segregated_heap.bitfit_heap);
pas_bitfit_page_config_variant variant;
for (PAS_EACH_BITFIT_PAGE_CONFIG_VARIANT_ASCENDING(variant)) {
pas_bitfit_directory* directory = pas_bitfit_heap_get_directory(heap, variant);
for (pas_bitfit_size_class* sizeClass =
pas_compact_atomic_bitfit_size_class_ptr_load(&directory->largest_size_class);
sizeClass;
sizeClass = pas_compact_atomic_bitfit_size_class_ptr_load(&sizeClass->next_smaller))
result.push_back(sizeClass->size);
}
sort(result.begin(), result.end());
return result;
}
template<typename... Arguments>
void assertSizeClasses(Arguments... sizeClasses)
{
vector<size_t> expectedSizeClasses { sizeClasses... };
vector<size_t> actualSizeClasses = getBitfitSizeClasses();
if (actualSizeClasses.size() == expectedSizeClasses.size()) {
bool allGood = true;
for (size_t index = 0; index < actualSizeClasses.size(); ++index) {
if (actualSizeClasses[index] != expectedSizeClasses[index]) {
allGood = false;
break;
}
}
if (allGood)
return;
}
cout << "Expected size classes: ";
for (size_t index = 0; index < expectedSizeClasses.size(); ++index) {
if (index)
cout << ", ";
cout << expectedSizeClasses[index];
}
cout << "\n";
cout << "But got: ";
for (size_t index = 0; index < actualSizeClasses.size(); ++index) {
if (index)
cout << ", ";
cout << actualSizeClasses[index];
}
cout << "\n";
CHECK(!"Test failed");
}
void testAllocateAlignedSmallerThanSizeClassAndSmallerThanLargestAvailable(
size_t firstSize, size_t numFirstObjects, size_t indexOfObjectToFree,
size_t fillerObjectSize, size_t alignedSize, size_t numAlignedObjects)
{
static constexpr bool verbose = false;
vector<void*> objects;
void* freedObject;
uintptr_t freedObjectBegin;
uintptr_t freedObjectEnd;
for (size_t index = 0; index < numFirstObjects; ++index) {
void* ptr = iso_allocate_common_primitive(firstSize, pas_non_compact_allocation_mode);
if (verbose)
cout << "Adding first object " << ptr << "\n";
objects.push_back(ptr);
}
assertSizeClasses(firstSize);
freedObject = objects[indexOfObjectToFree];
iso_deallocate(freedObject);
freedObjectBegin = reinterpret_cast<uintptr_t>(freedObject);
freedObjectEnd = freedObjectBegin + firstSize;
vector<void*> fillerObjects;
bool didStartAllocatingInFreedObject = false;
for (;;) {
void* fillerObject = iso_allocate_common_primitive(fillerObjectSize, pas_non_compact_allocation_mode);
uintptr_t fillerObjectBegin = reinterpret_cast<uintptr_t>(fillerObject);
uintptr_t fillerObjectEnd = fillerObjectBegin + fillerObjectSize;
if (verbose)
cout << "Allocated filler object " << fillerObject << "\n";
if (fillerObjectBegin >= freedObjectBegin && fillerObjectEnd <= freedObjectEnd) {
didStartAllocatingInFreedObject = true;
fillerObjects.push_back(fillerObject);
} else {
if (didStartAllocatingInFreedObject)
break;
}
}
CHECK_EQUAL(fillerObjects.size(), firstSize / fillerObjectSize);
assertSizeClasses(fillerObjectSize, firstSize);
for (size_t index = 1; index < fillerObjects.size(); ++index)
iso_deallocate(fillerObjects[index]);
for (size_t index = 0; index < numAlignedObjects; ++index) {
void* ptr = iso_allocate_common_primitive_with_alignment(alignedSize, alignedSize, pas_non_compact_allocation_mode);;
if (verbose)
cout << "Allocated aligned object " << ptr << "\n";
}
assertSizeClasses(fillerObjectSize, firstSize);
}
} // anonymous namespace
#endif // PAS_ENABLE_ISO
void addBitfitTests()
{
#if PAS_ENABLE_ISO
ForceBitfit forceBitfit;
ADD_TEST(testAllocateAlignedSmallerThanSizeClassAndSmallerThanLargestAvailable(
1056, 100, 0, 16, 1024, 100));
#endif // PAS_ENABLE_ISO
}