bmalloc: Rename SmallPage to SmallRun
[WebKit-https.git] / Source / bmalloc / bmalloc / Heap.cpp
1 /*
2  * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "Heap.h"
27 #include "BumpAllocator.h"
28 #include "LargeChunk.h"
29 #include "LargeObject.h"
30 #include "PerProcess.h"
31 #include "SmallChunk.h"
32 #include "SmallLine.h"
33 #include "SmallRun.h"
34 #include <thread>
35
36 namespace bmalloc {
37
38 Heap::Heap(std::lock_guard<StaticMutex>&)
39     : m_largeObjects(VMState::HasPhysical::True)
40     , m_isAllocatingPages(false)
41     , m_scavenger(*this, &Heap::concurrentScavenge)
42 {
43     initializeSmallRunMetadata();
44 }
45
46 void Heap::initializeSmallRunMetadata()
47 {
48     // We assume that m_smallRunMetadata is zero-filled.
49
50     for (size_t size = alignment; size <= smallMax; size += alignment) {
51         size_t sizeClass = bmalloc::sizeClass(size);
52         auto& metadata = m_smallRunMetadata[sizeClass];
53
54         size_t object = 0;
55         size_t line = 0;
56         while (object < vmPageSize) {
57             line = object / smallLineSize;
58             size_t leftover = object % smallLineSize;
59
60             size_t objectCount;
61             size_t remainder;
62             divideRoundingUp(smallLineSize - leftover, size, objectCount, remainder);
63
64             metadata[line] = { static_cast<unsigned short>(leftover), static_cast<unsigned short>(objectCount) };
65
66             object += objectCount * size;
67         }
68
69         // Don't allow the last object in a page to escape the page.
70         if (object > vmPageSize) {
71             BASSERT(metadata[line].objectCount);
72             --metadata[line].objectCount;
73         }
74     }
75 }
76
77 void Heap::concurrentScavenge()
78 {
79     std::unique_lock<StaticMutex> lock(PerProcess<Heap>::mutex());
80     scavenge(lock, scavengeSleepDuration);
81 }
82
83 void Heap::scavenge(std::unique_lock<StaticMutex>& lock, std::chrono::milliseconds sleepDuration)
84 {
85     waitUntilFalse(lock, sleepDuration, m_isAllocatingPages);
86
87     scavengeSmallRuns(lock, sleepDuration);
88     scavengeLargeObjects(lock, sleepDuration);
89     scavengeXLargeObjects(lock, sleepDuration);
90
91     sleep(lock, sleepDuration);
92 }
93
94 void Heap::scavengeSmallRuns(std::unique_lock<StaticMutex>& lock, std::chrono::milliseconds sleepDuration)
95 {
96     while (!m_smallRuns.isEmpty()) {
97         m_vmHeap.deallocateSmallRun(lock, m_smallRuns.pop());
98         waitUntilFalse(lock, sleepDuration, m_isAllocatingPages);
99     }
100 }
101
102 void Heap::scavengeLargeObjects(std::unique_lock<StaticMutex>& lock, std::chrono::milliseconds sleepDuration)
103 {
104     while (LargeObject largeObject = m_largeObjects.takeGreedy()) {
105         m_vmHeap.deallocateLargeObject(lock, largeObject);
106         waitUntilFalse(lock, sleepDuration, m_isAllocatingPages);
107     }
108 }
109
110 void Heap::scavengeXLargeObjects(std::unique_lock<StaticMutex>& lock, std::chrono::milliseconds sleepDuration)
111 {
112     while (XLargeRange range = m_xLargeMap.takePhysical()) {
113         lock.unlock();
114         vmDeallocatePhysicalPagesSloppy(range.begin(), range.size());
115         lock.lock();
116         
117         range.setVMState(VMState::Virtual);
118         m_xLargeMap.addVirtual(range);
119
120         waitUntilFalse(lock, sleepDuration, m_isAllocatingPages);
121     }
122
123     m_xLargeMap.shrinkToFit();
124 }
125
126 void Heap::allocateSmallBumpRanges(std::lock_guard<StaticMutex>& lock, size_t sizeClass, BumpAllocator& allocator, BumpRangeCache& rangeCache)
127 {
128     BASSERT(!rangeCache.size());
129     SmallRun* run = allocateSmallRun(lock, sizeClass);
130     SmallLine* lines = run->begin();
131     BASSERT(run->hasFreeLines(lock));
132
133     // Find a free line.
134     for (size_t lineNumber = 0; lineNumber < smallLineCount; ++lineNumber) {
135         if (lines[lineNumber].refCount(lock))
136             continue;
137
138         LineMetadata& lineMetadata = m_smallRunMetadata[sizeClass][lineNumber];
139         if (!lineMetadata.objectCount)
140             continue;
141
142         // In a fragmented run, some free ranges might not fit in the cache.
143         if (rangeCache.size() == rangeCache.capacity()) {
144             m_smallRunsWithFreeLines[sizeClass].push(run);
145             BASSERT(allocator.canAllocate());
146             return;
147         }
148
149         char* begin = lines[lineNumber].begin() + lineMetadata.startOffset;
150         unsigned short objectCount = lineMetadata.objectCount;
151         lines[lineNumber].ref(lock, lineMetadata.objectCount);
152         run->ref(lock);
153
154         // Merge with subsequent free lines.
155         while (++lineNumber < smallLineCount) {
156             if (lines[lineNumber].refCount(lock))
157                 break;
158
159             LineMetadata& lineMetadata = m_smallRunMetadata[sizeClass][lineNumber];
160             if (!lineMetadata.objectCount)
161                 continue;
162
163             objectCount += lineMetadata.objectCount;
164             lines[lineNumber].ref(lock, lineMetadata.objectCount);
165             run->ref(lock);
166         }
167
168         if (!allocator.canAllocate())
169             allocator.refill({ begin, objectCount });
170         else
171             rangeCache.push({ begin, objectCount });
172     }
173
174     BASSERT(allocator.canAllocate());
175     run->setHasFreeLines(lock, false);
176 }
177
178 SmallRun* Heap::allocateSmallRun(std::lock_guard<StaticMutex>& lock, size_t sizeClass)
179 {
180     if (!m_smallRunsWithFreeLines[sizeClass].isEmpty())
181         return m_smallRunsWithFreeLines[sizeClass].pop();
182
183     SmallRun* run = [this, &lock]() {
184         if (!m_smallRuns.isEmpty())
185             return m_smallRuns.pop();
186
187         m_isAllocatingPages = true;
188         return m_vmHeap.allocateSmallRun(lock);
189     }();
190
191     run->setSizeClass(sizeClass);
192     return run;
193 }
194
195 void Heap::deallocateSmallLine(std::lock_guard<StaticMutex>& lock, SmallLine* line)
196 {
197     BASSERT(!line->refCount(lock));
198     SmallRun* run = SmallRun::get(line);
199     run->deref(lock);
200
201     if (!run->hasFreeLines(lock)) {
202         run->setHasFreeLines(lock, true);
203         m_smallRunsWithFreeLines[run->sizeClass()].push(run);
204
205         BASSERT(run->refCount(lock));
206         return;
207     }
208
209     if (run->refCount(lock))
210         return;
211
212     m_smallRunsWithFreeLines[run->sizeClass()].remove(run);
213     m_smallRuns.push(run);
214     m_scavenger.run();
215 }
216
217 inline LargeObject& Heap::splitAndAllocate(LargeObject& largeObject, size_t size)
218 {
219     BASSERT(largeObject.isFree());
220
221     LargeObject nextLargeObject;
222
223     if (largeObject.size() - size > largeMin) {
224         std::pair<LargeObject, LargeObject> split = largeObject.split(size);
225         largeObject = split.first;
226         nextLargeObject = split.second;
227     }
228
229     largeObject.setFree(false);
230
231     if (nextLargeObject) {
232         BASSERT(!nextLargeObject.nextCanMerge());
233         m_largeObjects.insert(nextLargeObject);
234     }
235
236     return largeObject;
237 }
238
239 inline LargeObject& Heap::splitAndAllocate(LargeObject& largeObject, size_t alignment, size_t size)
240 {
241     LargeObject prevLargeObject;
242     LargeObject nextLargeObject;
243
244     size_t alignmentMask = alignment - 1;
245     if (test(largeObject.begin(), alignmentMask)) {
246         size_t prefixSize = roundUpToMultipleOf(alignment, largeObject.begin() + largeMin) - largeObject.begin();
247         std::pair<LargeObject, LargeObject> pair = largeObject.split(prefixSize);
248         prevLargeObject = pair.first;
249         largeObject = pair.second;
250     }
251
252     BASSERT(largeObject.isFree());
253
254     if (largeObject.size() - size > largeMin) {
255         std::pair<LargeObject, LargeObject> split = largeObject.split(size);
256         largeObject = split.first;
257         nextLargeObject = split.second;
258     }
259
260     largeObject.setFree(false);
261
262     if (prevLargeObject) {
263         LargeObject merged = prevLargeObject.merge();
264         m_largeObjects.insert(merged);
265     }
266
267     if (nextLargeObject) {
268         LargeObject merged = nextLargeObject.merge();
269         m_largeObjects.insert(merged);
270     }
271
272     return largeObject;
273 }
274
275 void* Heap::allocateLarge(std::lock_guard<StaticMutex>& lock, size_t size)
276 {
277     BASSERT(size <= largeMax);
278     BASSERT(size >= largeMin);
279     BASSERT(size == roundUpToMultipleOf<largeAlignment>(size));
280
281     LargeObject largeObject = m_largeObjects.take(size);
282     if (!largeObject)
283         largeObject = m_vmHeap.allocateLargeObject(lock, size);
284
285     if (largeObject.vmState().hasVirtual()) {
286         m_isAllocatingPages = true;
287         // We commit before we split in order to avoid split/merge commit/decommit churn.
288         vmAllocatePhysicalPagesSloppy(largeObject.begin(), largeObject.size());
289         largeObject.setVMState(VMState::Physical);
290     }
291
292     largeObject = splitAndAllocate(largeObject, size);
293
294     return largeObject.begin();
295 }
296
297 void* Heap::allocateLarge(std::lock_guard<StaticMutex>& lock, size_t alignment, size_t size, size_t unalignedSize)
298 {
299     BASSERT(size <= largeMax);
300     BASSERT(size >= largeMin);
301     BASSERT(size == roundUpToMultipleOf<largeAlignment>(size));
302     BASSERT(unalignedSize <= largeMax);
303     BASSERT(unalignedSize >= largeMin);
304     BASSERT(unalignedSize == roundUpToMultipleOf<largeAlignment>(unalignedSize));
305     BASSERT(alignment <= largeChunkSize / 2);
306     BASSERT(alignment >= largeAlignment);
307     BASSERT(isPowerOfTwo(alignment));
308
309     LargeObject largeObject = m_largeObjects.take(alignment, size, unalignedSize);
310     if (!largeObject)
311         largeObject = m_vmHeap.allocateLargeObject(lock, alignment, size, unalignedSize);
312
313     if (largeObject.vmState().hasVirtual()) {
314         m_isAllocatingPages = true;
315         // We commit before we split in order to avoid split/merge commit/decommit churn.
316         vmAllocatePhysicalPagesSloppy(largeObject.begin(), largeObject.size());
317         largeObject.setVMState(VMState::Physical);
318     }
319
320     largeObject = splitAndAllocate(largeObject, alignment, size);
321
322     return largeObject.begin();
323 }
324
325 void Heap::deallocateLarge(std::lock_guard<StaticMutex>&, const LargeObject& largeObject)
326 {
327     BASSERT(!largeObject.isFree());
328     largeObject.setFree(true);
329     
330     LargeObject merged = largeObject.merge();
331     m_largeObjects.insert(merged);
332     m_scavenger.run();
333 }
334
335 void Heap::deallocateLarge(std::lock_guard<StaticMutex>& lock, void* object)
336 {
337     LargeObject largeObject(object);
338     deallocateLarge(lock, largeObject);
339 }
340
341 void* Heap::allocateXLarge(std::lock_guard<StaticMutex>& lock, size_t alignment, size_t size)
342 {
343     void* result = tryAllocateXLarge(lock, alignment, size);
344     RELEASE_BASSERT(result);
345     return result;
346 }
347
348 void* Heap::allocateXLarge(std::lock_guard<StaticMutex>& lock, size_t size)
349 {
350     return allocateXLarge(lock, alignment, size);
351 }
352
353 XLargeRange Heap::splitAndAllocate(XLargeRange& range, size_t alignment, size_t size)
354 {
355     XLargeRange prev;
356     XLargeRange next;
357
358     size_t alignmentMask = alignment - 1;
359     if (test(range.begin(), alignmentMask)) {
360         size_t prefixSize = roundUpToMultipleOf(alignment, range.begin()) - range.begin();
361         std::pair<XLargeRange, XLargeRange> pair = range.split(prefixSize);
362         prev = pair.first;
363         range = pair.second;
364     }
365
366     if (range.size() - size >= xLargeAlignment) {
367         size_t alignedSize = roundUpToMultipleOf<xLargeAlignment>(size);
368         std::pair<XLargeRange, XLargeRange> pair = range.split(alignedSize);
369         range = pair.first;
370         next = pair.second;
371     }
372
373     // At this point our range might contain an unused tail fragment. This is
374     // common. We can't allocate the tail fragment because it's aligned to less
375     // than xLargeAlignment. So, we pair the allocation with its tail fragment
376     // in the allocated list. This is an important optimization because it
377     // keeps the free list short, speeding up allocation and merging.
378
379     std::pair<XLargeRange, XLargeRange> allocated = range.split(roundUpToMultipleOf<vmPageSize>(size));
380     if (allocated.first.vmState().hasVirtual()) {
381         vmAllocatePhysicalPagesSloppy(allocated.first.begin(), allocated.first.size());
382         allocated.first.setVMState(VMState::Physical);
383     }
384
385     m_xLargeMap.addAllocated(prev, allocated, next);
386     return allocated.first;
387 }
388
389 void* Heap::tryAllocateXLarge(std::lock_guard<StaticMutex>&, size_t alignment, size_t size)
390 {
391     BASSERT(isPowerOfTwo(alignment));
392     BASSERT(alignment < xLargeMax);
393
394     m_isAllocatingPages = true;
395
396     alignment = roundUpToMultipleOf<xLargeAlignment>(alignment);
397
398     XLargeRange range = m_xLargeMap.takeFree(alignment, size);
399     if (!range) {
400         // We allocate VM in aligned multiples to increase the chances that
401         // the OS will provide contiguous ranges that we can merge.
402         size_t alignedSize = roundUpToMultipleOf<xLargeAlignment>(size);
403
404         void* begin = tryVMAllocate(alignment, alignedSize);
405         if (!begin)
406             return nullptr;
407         range = XLargeRange(begin, alignedSize, VMState::Virtual);
408     }
409
410     return splitAndAllocate(range, alignment, size).begin();
411 }
412
413 size_t Heap::xLargeSize(std::unique_lock<StaticMutex>&, void* object)
414 {
415     return m_xLargeMap.getAllocated(object).size();
416 }
417
418 void Heap::shrinkXLarge(std::unique_lock<StaticMutex>&, const Range& object, size_t newSize)
419 {
420     BASSERT(object.size() > newSize);
421
422     if (object.size() - newSize < vmPageSize)
423         return;
424     
425     XLargeRange range = m_xLargeMap.takeAllocated(object.begin());
426     splitAndAllocate(range, xLargeAlignment, newSize);
427
428     m_scavenger.run();
429 }
430
431 void Heap::deallocateXLarge(std::unique_lock<StaticMutex>&, void* object)
432 {
433     XLargeRange range = m_xLargeMap.takeAllocated(object);
434     m_xLargeMap.addFree(range);
435     
436     m_scavenger.run();
437 }
438
439 } // namespace bmalloc