1/*
2 * Copyright (C) 2017-2018 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 "PerThread.h"
29#include <cstddef>
30
31namespace bmalloc {
32
33class IsoTLSEntry;
34template<typename Config> class IsoAllocator;
35template<typename Config> class IsoDeallocator;
36
37namespace api {
38template<typename Type> struct IsoHeap;
39}
40
41class IsoTLS {
42public:
43 template<typename Type>
44 static void* allocate(api::IsoHeap<Type>&, bool abortOnFailure);
45
46 template<typename Type>
47 static void deallocate(api::IsoHeap<Type>&, void* p);
48
49 template<typename Type>
50 static void ensureHeap(api::IsoHeap<Type>&);
51
52 static void scavenge();
53
54 template<typename Type>
55 static void scavenge(api::IsoHeap<Type>&);
56
57private:
58 IsoTLS();
59
60 template<typename Config, typename Type>
61 static void* allocateImpl(api::IsoHeap<Type>&, bool abortOnFailure);
62
63 template<typename Config>
64 void* allocateFast(unsigned offset, bool abortOnFailure);
65
66 template<typename Config, typename Type>
67 static void* allocateSlow(api::IsoHeap<Type>&, bool abortOnFailure);
68
69 template<typename Config, typename Type>
70 static void deallocateImpl(api::IsoHeap<Type>&, void* p);
71
72 template<typename Config, typename Type>
73 void deallocateFast(api::IsoHeap<Type>&, unsigned offset, void* p);
74
75 template<typename Config, typename Type>
76 static void deallocateSlow(api::IsoHeap<Type>&, void* p);
77
78 static IsoTLS* get();
79 static void set(IsoTLS*);
80
81 template<typename Type>
82 static IsoTLS* ensureHeapAndEntries(api::IsoHeap<Type>&);
83
84 BEXPORT static IsoTLS* ensureEntries(unsigned offset);
85
86 static void destructor(void* arg); // FIXME implement this
87
88 static size_t sizeForCapacity(unsigned capacity);
89 static unsigned capacityForSize(size_t size);
90
91 size_t size();
92
93 template<typename Func>
94 void forEachEntry(const Func&);
95
96 enum class MallocFallbackState : uint8_t {
97 Undecided,
98 FallBackToMalloc,
99 DoNotFallBack
100 };
101
102 BEXPORT static MallocFallbackState s_mallocFallbackState;
103
104 BEXPORT static void determineMallocFallbackState();
105
106 IsoTLSEntry* m_lastEntry { nullptr };
107 unsigned m_extent { 0 };
108 unsigned m_capacity { 0 };
109 char m_data[1];
110
111#if HAVE_PTHREAD_MACHDEP_H
112 static const pthread_key_t tlsKey = __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY1;
113#else
114 BEXPORT static bool s_didInitialize;
115 BEXPORT static pthread_key_t s_tlsKey;
116#endif
117};
118
119} // namespace bmalloc
120
121