2 * Copyright (C) 2014 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 "Allocator.h"
28 #include "Deallocator.h"
30 #include "PerProcess.h"
38 Allocator::Allocator(Deallocator& deallocator)
39 : m_deallocator(deallocator)
41 for (unsigned short i = alignment; i <= smallMax; i += alignment)
42 m_smallAllocators[smallSizeClassFor(i)].init<SmallLine>(i);
44 for (unsigned short i = smallMax + alignment; i <= mediumMax; i += alignment)
45 m_mediumAllocators[mediumSizeClassFor(i)].init<MediumLine>(i);
48 Allocator::~Allocator()
53 void Allocator::scavenge()
55 for (auto& allocator : m_smallAllocators) {
56 while (allocator.canAllocate())
57 m_deallocator.deallocate(allocator.allocate());
61 for (auto& allocator : m_mediumAllocators) {
62 while (allocator.canAllocate())
63 m_deallocator.deallocate(allocator.allocate());
68 void* Allocator::allocateLarge(size_t size)
70 size = roundUpToMultipleOf<largeAlignment>(size);
71 std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex());
72 return PerProcess<Heap>::getFastCase()->allocateLarge(lock, size);
75 void* Allocator::allocateXLarge(size_t size)
77 size = roundUpToMultipleOf<largeAlignment>(size);
78 std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex());
79 return PerProcess<Heap>::getFastCase()->allocateXLarge(lock, size);
82 void* Allocator::allocateMedium(size_t size)
84 BumpAllocator& allocator = m_mediumAllocators[mediumSizeClassFor(size)];
86 if (!allocator.canAllocate())
87 allocator.refill(m_deallocator.allocateMediumLine());
88 return allocator.allocate();
91 void* Allocator::allocateSlowCase(size_t size)
95 BASSERT(!allocateFastCase(size, dummy));
97 if (size <= smallMax) {
98 size_t smallSizeClass = smallSizeClassFor(size);
99 BumpAllocator& allocator = m_smallAllocators[smallSizeClass];
100 allocator.refill(m_deallocator.allocateSmallLine(smallSizeClass));
101 return allocator.allocate();
104 if (size <= mediumMax)
105 return allocateMedium(size);
107 if (size <= largeMax)
108 return allocateLarge(size);
110 return allocateXLarge(size);
113 } // namespace bmalloc