@@ -68,101 +68,6 @@ static int compareIntegers(T left, T right) {
68
68
return (left == right ? 0 : left < right ? -1 : 1 );
69
69
}
70
70
71
- static uintptr_t swift_pageSize () {
72
- #if defined(__APPLE__)
73
- return vm_page_size;
74
- #elif defined(_MSC_VER)
75
- SYSTEM_INFO SystemInfo;
76
- GetSystemInfo (&SystemInfo);
77
- return SystemInfo.dwPageSize ;
78
- #else
79
- return sysconf (_SC_PAGESIZE);
80
- #endif
81
- }
82
-
83
- // allocate memory up to a nearby page boundary
84
- static void *swift_allocateMetadataRoundingToPage (size_t size) {
85
- const uintptr_t PageSizeMask = SWIFT_LAZY_CONSTANT (swift_pageSize ()) - 1 ;
86
- size = (size + PageSizeMask) & ~PageSizeMask;
87
- #if defined(_MSC_VER)
88
- auto mem = VirtualAlloc (
89
- nullptr , size, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
90
- #else
91
- auto mem = mmap (nullptr , size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
92
- VM_TAG_FOR_SWIFT_METADATA, 0 );
93
- if (mem == MAP_FAILED)
94
- mem = nullptr ;
95
- #endif
96
- return mem;
97
- }
98
-
99
- // free memory allocated by swift_allocateMetadataRoundingToPage()
100
- static void swift_freeMetadata (void *addr, size_t size) {
101
- #if defined(_MSC_VER)
102
- // On success, VirtualFree() returns nonzero, on failure 0
103
- int result = VirtualFree (addr, 0 , MEM_RELEASE);
104
- if (result == 0 )
105
- fatalError (/* flags = */ 0 , " swift_freePage: VirtualFree() failed" );
106
- #else
107
- // On success, munmap() returns 0, on failure -1
108
- int result = munmap (addr, size);
109
- if (result != 0 )
110
- fatalError (/* flags = */ 0 , " swift_freePage: munmap() failed" );
111
- #endif
112
- }
113
-
114
- void *MetadataAllocator::Allocate (size_t size, size_t /* alignment*/ ) {
115
- const uintptr_t PageSize = SWIFT_LAZY_CONSTANT (swift_pageSize ());
116
- // If the requested size is a page or larger, map page(s) for it
117
- // specifically.
118
- if (LLVM_UNLIKELY (size >= PageSize)) {
119
- void *mem = swift_allocateMetadataRoundingToPage (size);
120
- if (!mem)
121
- crash (" unable to allocate memory for metadata cache" );
122
- return mem;
123
- }
124
-
125
- uintptr_t curValue = NextValue.load (std::memory_order_relaxed);
126
- while (true ) {
127
- char *next = reinterpret_cast <char *>(curValue);
128
- char *end = next + size;
129
-
130
- // If we wrap over the end of the page, allocate a new page.
131
- void *allocation = nullptr ;
132
- const uintptr_t PageSizeMask = PageSize - 1 ;
133
- if (LLVM_UNLIKELY (((uintptr_t )next & ~PageSizeMask)
134
- != (((uintptr_t )end & ~PageSizeMask)))) {
135
- // Allocate a new page if we haven't already.
136
- allocation = swift_allocateMetadataRoundingToPage (PageSize);
137
-
138
- if (!allocation)
139
- crash (" unable to allocate memory for metadata cache" );
140
-
141
- next = (char *) allocation;
142
- end = next + size;
143
- }
144
-
145
- // Swap it into place.
146
- if (LLVM_LIKELY (std::atomic_compare_exchange_weak_explicit (
147
- &NextValue, &curValue, reinterpret_cast <uintptr_t >(end),
148
- std::memory_order_relaxed, std::memory_order_relaxed))) {
149
- return next;
150
- }
151
-
152
- // If that didn't succeed, and we allocated, free the allocation.
153
- // This potentially causes us to perform multiple mmaps under contention,
154
- // but it keeps the fast path pristine.
155
- if (allocation) {
156
- swift_freeMetadata (allocation, PageSize);
157
- }
158
- }
159
- }
160
-
161
- void MetadataAllocator::Deallocate (const void *ptr, size_t size) {
162
- // Borrowed from llvm::BumpPtrAllocator.
163
- __asan_poison_memory_region (ptr, size);
164
- }
165
-
166
71
namespace {
167
72
struct GenericCacheEntry ;
168
73
@@ -375,7 +280,7 @@ namespace {
375
280
}
376
281
377
282
// / The uniquing structure for ObjC class-wrapper metadata.
378
- static ConcurrentMap <ObjCClassCacheEntry, false > ObjCClassWrappers;
283
+ static SimpleGlobalCache <ObjCClassCacheEntry> ObjCClassWrappers;
379
284
380
285
#endif
381
286
@@ -459,7 +364,7 @@ class FunctionCacheEntry {
459
364
} // end anonymous namespace
460
365
461
366
// / The uniquing structure for function type metadata.
462
- static ConcurrentMap <FunctionCacheEntry, false > FunctionTypes;
367
+ static SimpleGlobalCache <FunctionCacheEntry> FunctionTypes;
463
368
464
369
const FunctionTypeMetadata *
465
370
swift::swift_getFunctionTypeMetadata1 (FunctionTypeFlags flags,
@@ -624,7 +529,7 @@ class TupleCacheEntry {
624
529
}
625
530
626
531
// / The uniquing structure for tuple type metadata.
627
- static ConcurrentMap <TupleCacheEntry, false > TupleTypes;
532
+ static SimpleGlobalCache <TupleCacheEntry> TupleTypes;
628
533
629
534
// / Given a metatype pointer, produce the value-witness table for it.
630
535
// / This is equivalent to metatype->ValueWitnesses but more efficient.
@@ -1904,7 +1809,7 @@ namespace {
1904
1809
}
1905
1810
1906
1811
// / The uniquing structure for metatype type metadata.
1907
- static ConcurrentMap <MetatypeCacheEntry, false > MetatypeTypes;
1812
+ static SimpleGlobalCache <MetatypeCacheEntry> MetatypeTypes;
1908
1813
1909
1814
// / \brief Fetch a uniqued metadata for a metatype type.
1910
1815
SWIFT_RUNTIME_EXPORT
@@ -1972,11 +1877,11 @@ class ExistentialMetatypeCacheEntry {
1972
1877
} // end anonymous namespace
1973
1878
1974
1879
// / The uniquing structure for existential metatype value witness tables.
1975
- static ConcurrentMap <ExistentialMetatypeValueWitnessTableCacheEntry, false >
1880
+ static SimpleGlobalCache <ExistentialMetatypeValueWitnessTableCacheEntry>
1976
1881
ExistentialMetatypeValueWitnessTables;
1977
1882
1978
1883
// / The uniquing structure for existential metatype type metadata.
1979
- static ConcurrentMap <ExistentialMetatypeCacheEntry, false > ExistentialMetatypes;
1884
+ static SimpleGlobalCache <ExistentialMetatypeCacheEntry> ExistentialMetatypes;
1980
1885
1981
1886
static const ExtraInhabitantsValueWitnessTable
1982
1887
ExistentialMetatypeValueWitnesses_1 =
@@ -2154,15 +2059,15 @@ class ClassExistentialValueWitnessTableCacheEntry {
2154
2059
} // end anonymous namespace
2155
2060
2156
2061
// / The uniquing structure for existential type metadata.
2157
- static ConcurrentMap <ExistentialCacheEntry, false > ExistentialTypes;
2062
+ static SimpleGlobalCache <ExistentialCacheEntry> ExistentialTypes;
2158
2063
2159
2064
static const ValueWitnessTable OpaqueExistentialValueWitnesses_0 =
2160
2065
ValueWitnessTableForBox<OpaqueExistentialBox<0 >>::table;
2161
2066
static const ValueWitnessTable OpaqueExistentialValueWitnesses_1 =
2162
2067
ValueWitnessTableForBox<OpaqueExistentialBox<1 >>::table;
2163
2068
2164
2069
// / The uniquing structure for opaque existential value witness tables.
2165
- static ConcurrentMap <OpaqueExistentialValueWitnessTableCacheEntry, false >
2070
+ static SimpleGlobalCache <OpaqueExistentialValueWitnessTableCacheEntry>
2166
2071
OpaqueExistentialValueWitnessTables;
2167
2072
2168
2073
// / Instantiate a value witness table for an opaque existential container with
@@ -2208,7 +2113,7 @@ static const ExtraInhabitantsValueWitnessTable ClassExistentialValueWitnesses_2
2208
2113
ValueWitnessTableForBox<ClassExistentialBox<2 >>::table;
2209
2114
2210
2115
// / The uniquing structure for class existential value witness tables.
2211
- static ConcurrentMap <ClassExistentialValueWitnessTableCacheEntry, false >
2116
+ static SimpleGlobalCache <ClassExistentialValueWitnessTableCacheEntry>
2212
2117
ClassExistentialValueWitnessTables;
2213
2118
2214
2119
// / Instantiate a value witness table for a class-constrained existential
0 commit comments