| 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 "DeferredDecommitInlines.h" |
| 29 | #include "DeferredTriggerInlines.h" |
| 30 | #include "EligibilityResultInlines.h" |
| 31 | #include "FreeListInlines.h" |
| 32 | #include "IsoAllocatorInlines.h" |
| 33 | #include "IsoDeallocatorInlines.h" |
| 34 | #include "IsoDirectoryInlines.h" |
| 35 | #include "IsoDirectoryPageInlines.h" |
| 36 | #include "IsoHeapImplInlines.h" |
| 37 | #include "IsoHeap.h" |
| 38 | #include "IsoPageInlines.h" |
| 39 | #include "IsoTLSAllocatorEntryInlines.h" |
| 40 | #include "IsoTLSDeallocatorEntryInlines.h" |
| 41 | #include "IsoTLSEntryInlines.h" |
| 42 | #include "IsoTLSInlines.h" |
| 43 | |
| 44 | namespace bmalloc { namespace api { |
| 45 | |
| 46 | template<typename Type> |
| 47 | void* IsoHeap<Type>::allocate() |
| 48 | { |
| 49 | bool abortOnFailure = true; |
| 50 | return IsoTLS::allocate(*this, abortOnFailure); |
| 51 | } |
| 52 | |
| 53 | template<typename Type> |
| 54 | void* IsoHeap<Type>::tryAllocate() |
| 55 | { |
| 56 | bool abortOnFailure = false; |
| 57 | return IsoTLS::allocate(*this, abortOnFailure); |
| 58 | } |
| 59 | |
| 60 | template<typename Type> |
| 61 | void IsoHeap<Type>::deallocate(void* p) |
| 62 | { |
| 63 | IsoTLS::deallocate(*this, p); |
| 64 | } |
| 65 | |
| 66 | template<typename Type> |
| 67 | void IsoHeap<Type>::scavenge() |
| 68 | { |
| 69 | IsoTLS::scavenge(*this); |
| 70 | } |
| 71 | |
| 72 | template<typename Type> |
| 73 | bool IsoHeap<Type>::isInitialized() |
| 74 | { |
| 75 | std::atomic<unsigned>* atomic = |
| 76 | reinterpret_cast<std::atomic<unsigned>*>(&m_allocatorOffsetPlusOne); |
| 77 | return !!atomic->load(std::memory_order_acquire); |
| 78 | } |
| 79 | |
| 80 | template<typename Type> |
| 81 | auto IsoHeap<Type>::impl() -> IsoHeapImpl<Config>& |
| 82 | { |
| 83 | IsoTLS::ensureHeap(*this); |
| 84 | return *m_impl; |
| 85 | } |
| 86 | |
| 87 | // This is most appropraite for template classes. |
| 88 | #define MAKE_BISO_MALLOCED_INLINE(isoType) \ |
| 89 | public: \ |
| 90 | static ::bmalloc::api::IsoHeap<isoType>& bisoHeap() \ |
| 91 | { \ |
| 92 | static ::bmalloc::api::IsoHeap<isoType> heap; \ |
| 93 | return heap; \ |
| 94 | } \ |
| 95 | \ |
| 96 | void* operator new(size_t, void* p) { return p; } \ |
| 97 | void* operator new[](size_t, void* p) { return p; } \ |
| 98 | \ |
| 99 | void* operator new(size_t size) \ |
| 100 | { \ |
| 101 | RELEASE_BASSERT(size == sizeof(isoType)); \ |
| 102 | return bisoHeap().allocate(); \ |
| 103 | } \ |
| 104 | \ |
| 105 | void operator delete(void* p) \ |
| 106 | { \ |
| 107 | bisoHeap().deallocate(p); \ |
| 108 | } \ |
| 109 | \ |
| 110 | void* operator new[](size_t size) = delete; \ |
| 111 | void operator delete[](void* p) = delete; \ |
| 112 | private: \ |
| 113 | typedef int __makeBisoMallocedInlineMacroSemicolonifier |
| 114 | |
| 115 | #define MAKE_BISO_MALLOCED_IMPL(isoType) \ |
| 116 | ::bmalloc::api::IsoHeap<isoType>& isoType::bisoHeap() \ |
| 117 | { \ |
| 118 | static ::bmalloc::api::IsoHeap<isoType> heap; \ |
| 119 | return heap; \ |
| 120 | } \ |
| 121 | \ |
| 122 | void* isoType::operator new(size_t size) \ |
| 123 | { \ |
| 124 | RELEASE_BASSERT(size == sizeof(isoType)); \ |
| 125 | return bisoHeap().allocate(); \ |
| 126 | } \ |
| 127 | \ |
| 128 | void isoType::operator delete(void* p) \ |
| 129 | { \ |
| 130 | bisoHeap().deallocate(p); \ |
| 131 | } \ |
| 132 | \ |
| 133 | struct MakeBisoMallocedImplMacroSemicolonifier##isoType { } |
| 134 | |
| 135 | #define MAKE_BISO_MALLOCED_IMPL_TEMPLATE(isoType) \ |
| 136 | template<> \ |
| 137 | ::bmalloc::api::IsoHeap<isoType>& isoType::bisoHeap() \ |
| 138 | { \ |
| 139 | static ::bmalloc::api::IsoHeap<isoType> heap; \ |
| 140 | return heap; \ |
| 141 | } \ |
| 142 | \ |
| 143 | template<> \ |
| 144 | void* isoType::operator new(size_t size) \ |
| 145 | { \ |
| 146 | RELEASE_BASSERT(size == sizeof(isoType)); \ |
| 147 | return bisoHeap().allocate(); \ |
| 148 | } \ |
| 149 | \ |
| 150 | template<> \ |
| 151 | void isoType::operator delete(void* p) \ |
| 152 | { \ |
| 153 | bisoHeap().deallocate(p); \ |
| 154 | } \ |
| 155 | \ |
| 156 | struct MakeBisoMallocedImplMacroSemicolonifier##isoType { } |
| 157 | |
| 158 | } } // namespace bmalloc::api |
| 159 | |
| 160 | |