1 | /* |
2 | * Copyright (C) 2014-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 | #include "BInline.h" |
27 | #include "Cache.h" |
28 | #include "DebugHeap.h" |
29 | #include "Environment.h" |
30 | #include "Heap.h" |
31 | #include "PerProcess.h" |
32 | |
33 | namespace bmalloc { |
34 | |
35 | void Cache::scavenge(HeapKind heapKind) |
36 | { |
37 | PerHeapKind<Cache>* caches = PerThread<PerHeapKind<Cache>>::getFastCase(); |
38 | if (!caches) |
39 | return; |
40 | if (!isActiveHeapKind(heapKind)) |
41 | return; |
42 | |
43 | caches->at(heapKind).allocator().scavenge(); |
44 | caches->at(heapKind).deallocator().scavenge(); |
45 | } |
46 | |
47 | Cache::Cache(HeapKind heapKind) |
48 | : m_deallocator(PerProcess<PerHeapKind<Heap>>::get()->at(heapKind)) |
49 | , m_allocator(PerProcess<PerHeapKind<Heap>>::get()->at(heapKind), m_deallocator) |
50 | { |
51 | BASSERT(!Environment::get()->isDebugHeapEnabled()); |
52 | } |
53 | |
54 | BNO_INLINE void* Cache::tryAllocateSlowCaseNullCache(HeapKind heapKind, size_t size) |
55 | { |
56 | if (auto* debugHeap = DebugHeap::tryGet()) { |
57 | constexpr bool crashOnFailure = false; |
58 | return debugHeap->malloc(size, crashOnFailure); |
59 | } |
60 | return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().tryAllocate(size); |
61 | } |
62 | |
63 | BNO_INLINE void* Cache::allocateSlowCaseNullCache(HeapKind heapKind, size_t size) |
64 | { |
65 | if (auto* debugHeap = DebugHeap::tryGet()) { |
66 | constexpr bool crashOnFailure = true; |
67 | return debugHeap->malloc(size, crashOnFailure); |
68 | } |
69 | return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().allocate(size); |
70 | } |
71 | |
72 | BNO_INLINE void* Cache::tryAllocateSlowCaseNullCache(HeapKind heapKind, size_t alignment, size_t size) |
73 | { |
74 | if (auto* debugHeap = DebugHeap::tryGet()) { |
75 | constexpr bool crashOnFailure = false; |
76 | return debugHeap->memalign(alignment, size, crashOnFailure); |
77 | } |
78 | return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().tryAllocate(alignment, size); |
79 | } |
80 | |
81 | BNO_INLINE void* Cache::allocateSlowCaseNullCache(HeapKind heapKind, size_t alignment, size_t size) |
82 | { |
83 | if (auto* debugHeap = DebugHeap::tryGet()) { |
84 | constexpr bool crashOnFailure = true; |
85 | return debugHeap->memalign(alignment, size, crashOnFailure); |
86 | } |
87 | return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().allocate(alignment, size); |
88 | } |
89 | |
90 | BNO_INLINE void Cache::deallocateSlowCaseNullCache(HeapKind heapKind, void* object) |
91 | { |
92 | if (auto* debugHeap = DebugHeap::tryGet()) { |
93 | debugHeap->free(object); |
94 | return; |
95 | } |
96 | PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).deallocator().deallocate(object); |
97 | } |
98 | |
99 | BNO_INLINE void* Cache::tryReallocateSlowCaseNullCache(HeapKind heapKind, void* object, size_t newSize) |
100 | { |
101 | if (auto* debugHeap = DebugHeap::tryGet()) { |
102 | constexpr bool crashOnFailure = false; |
103 | return debugHeap->realloc(object, newSize, crashOnFailure); |
104 | } |
105 | return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().tryReallocate(object, newSize); |
106 | } |
107 | |
108 | BNO_INLINE void* Cache::reallocateSlowCaseNullCache(HeapKind heapKind, void* object, size_t newSize) |
109 | { |
110 | if (auto* debugHeap = DebugHeap::tryGet()) { |
111 | constexpr bool crashOnFailure = true; |
112 | return debugHeap->realloc(object, newSize, crashOnFailure); |
113 | } |
114 | return PerThread<PerHeapKind<Cache>>::getSlowCase()->at(mapToActiveHeapKind(heapKind)).allocator().reallocate(object, newSize); |
115 | } |
116 | |
117 | } // namespace bmalloc |
118 | |