1 | /* |
2 | * Copyright (C) 2017 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 | #pragma once |
27 | |
28 | #include "BInline.h" |
29 | #include "EligibilityResult.h" |
30 | #include "IsoAllocator.h" |
31 | #include "IsoHeapImplInlines.h" |
32 | #include "IsoPage.h" |
33 | |
34 | namespace bmalloc { |
35 | |
36 | template<typename Config> |
37 | IsoAllocator<Config>::IsoAllocator(IsoHeapImpl<Config>& heap) |
38 | : m_heap(&heap) |
39 | { |
40 | } |
41 | |
42 | template<typename Config> |
43 | IsoAllocator<Config>::~IsoAllocator() |
44 | { |
45 | } |
46 | |
47 | template<typename Config> |
48 | void* IsoAllocator<Config>::allocate(bool abortOnFailure) |
49 | { |
50 | static constexpr bool verbose = false; |
51 | void* result = m_freeList.allocate<Config>( |
52 | [&] () -> void* { |
53 | return allocateSlow(abortOnFailure); |
54 | }); |
55 | if (verbose) |
56 | fprintf(stderr, "%p: allocated %p of size %u\n" , m_heap, result, Config::objectSize); |
57 | return result; |
58 | } |
59 | |
60 | template<typename Config> |
61 | BNO_INLINE void* IsoAllocator<Config>::allocateSlow(bool abortOnFailure) |
62 | { |
63 | std::lock_guard<Mutex> locker(m_heap->lock); |
64 | |
65 | AllocationMode allocationMode = m_heap->updateAllocationMode(); |
66 | if (allocationMode == AllocationMode::Shared) { |
67 | if (m_currentPage) { |
68 | m_currentPage->stopAllocating(m_freeList); |
69 | m_currentPage = nullptr; |
70 | m_freeList.clear(); |
71 | } |
72 | return m_heap->allocateFromShared(locker, abortOnFailure); |
73 | } |
74 | |
75 | BASSERT(allocationMode == AllocationMode::Fast); |
76 | |
77 | EligibilityResult<Config> result = m_heap->takeFirstEligible(); |
78 | if (result.kind != EligibilityKind::Success) { |
79 | RELEASE_BASSERT(result.kind == EligibilityKind::OutOfMemory); |
80 | RELEASE_BASSERT(!abortOnFailure); |
81 | return nullptr; |
82 | } |
83 | |
84 | if (m_currentPage) |
85 | m_currentPage->stopAllocating(m_freeList); |
86 | |
87 | m_currentPage = result.page; |
88 | m_freeList = m_currentPage->startAllocating(); |
89 | |
90 | return m_freeList.allocate<Config>([] () { BCRASH(); return nullptr; }); |
91 | } |
92 | |
93 | template<typename Config> |
94 | void IsoAllocator<Config>::scavenge() |
95 | { |
96 | if (m_currentPage) { |
97 | std::lock_guard<Mutex> locker(m_heap->lock); |
98 | m_currentPage->stopAllocating(m_freeList); |
99 | m_currentPage = nullptr; |
100 | m_freeList.clear(); |
101 | } |
102 | } |
103 | |
104 | } // namespace bmalloc |
105 | |
106 | |