@@ -36,25 +36,38 @@ class String {
36
36
std::array<char , 0x20 > ExtraData;
37
37
};
38
38
39
- static thread_local BumpPtrAllocator ThreadLocalAllocator;
40
- class PerThreadAllocator : public AllocatorBase <PerThreadAllocator> {
39
+ class SimpleAllocator : public AllocatorBase <SimpleAllocator> {
41
40
public:
42
41
inline LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate (size_t Size,
43
42
size_t Alignment) {
44
- return ThreadLocalAllocator.Allocate (Size, Align (Alignment));
43
+ #if LLVM_ENABLE_THREADS
44
+ std::lock_guard<std::mutex> Guard (AllocatorMutex);
45
+ #endif
46
+
47
+ return Allocator.Allocate (Size, Align (Alignment));
45
48
}
46
- inline size_t getBytesAllocated () const {
47
- return ThreadLocalAllocator.getBytesAllocated ();
49
+ inline size_t getBytesAllocated () {
50
+ #if LLVM_ENABLE_THREADS
51
+ std::lock_guard<std::mutex> Guard (AllocatorMutex);
52
+ #endif
53
+
54
+ return Allocator.getBytesAllocated ();
48
55
}
49
56
50
57
// Pull in base class overloads.
51
- using AllocatorBase<PerThreadAllocator>::Allocate;
58
+ using AllocatorBase<SimpleAllocator>::Allocate;
59
+
60
+ protected:
61
+ #if LLVM_ENABLE_THREADS
62
+ std::mutex AllocatorMutex;
63
+ #endif
64
+ BumpPtrAllocator Allocator;
52
65
} Allocator;
53
66
54
67
TEST (ConcurrentHashTableTest, AddStringEntries) {
55
68
ConcurrentHashTableByPtr<
56
- std::string, String, PerThreadAllocator ,
57
- ConcurrentHashTableInfoByPtr<std::string, String, PerThreadAllocator >>
69
+ std::string, String, SimpleAllocator ,
70
+ ConcurrentHashTableInfoByPtr<std::string, String, SimpleAllocator >>
58
71
HashTable (Allocator, 10 );
59
72
60
73
size_t AllocatedBytesAtStart = Allocator.getBytesAllocated ();
@@ -102,8 +115,8 @@ TEST(ConcurrentHashTableTest, AddStringEntries) {
102
115
TEST (ConcurrentHashTableTest, AddStringMultiplueEntries) {
103
116
const size_t NumElements = 10000 ;
104
117
ConcurrentHashTableByPtr<
105
- std::string, String, PerThreadAllocator ,
106
- ConcurrentHashTableInfoByPtr<std::string, String, PerThreadAllocator >>
118
+ std::string, String, SimpleAllocator ,
119
+ ConcurrentHashTableInfoByPtr<std::string, String, SimpleAllocator >>
107
120
HashTable (Allocator);
108
121
109
122
// Check insertion.
@@ -147,8 +160,8 @@ TEST(ConcurrentHashTableTest, AddStringMultiplueEntriesWithResize) {
147
160
// Number of elements exceeds original size, thus hashtable should be resized.
148
161
const size_t NumElements = 20000 ;
149
162
ConcurrentHashTableByPtr<
150
- std::string, String, PerThreadAllocator ,
151
- ConcurrentHashTableInfoByPtr<std::string, String, PerThreadAllocator >>
163
+ std::string, String, SimpleAllocator ,
164
+ ConcurrentHashTableInfoByPtr<std::string, String, SimpleAllocator >>
152
165
HashTable (Allocator, 100 );
153
166
154
167
// Check insertion.
@@ -191,8 +204,8 @@ TEST(ConcurrentHashTableTest, AddStringMultiplueEntriesWithResize) {
191
204
TEST (ConcurrentHashTableTest, AddStringEntriesParallel) {
192
205
const size_t NumElements = 10000 ;
193
206
ConcurrentHashTableByPtr<
194
- std::string, String, PerThreadAllocator ,
195
- ConcurrentHashTableInfoByPtr<std::string, String, PerThreadAllocator >>
207
+ std::string, String, SimpleAllocator ,
208
+ ConcurrentHashTableInfoByPtr<std::string, String, SimpleAllocator >>
196
209
HashTable (Allocator);
197
210
198
211
// Check parallel insertion.
@@ -235,8 +248,8 @@ TEST(ConcurrentHashTableTest, AddStringEntriesParallel) {
235
248
TEST (ConcurrentHashTableTest, AddStringEntriesParallelWithResize) {
236
249
const size_t NumElements = 20000 ;
237
250
ConcurrentHashTableByPtr<
238
- std::string, String, PerThreadAllocator ,
239
- ConcurrentHashTableInfoByPtr<std::string, String, PerThreadAllocator >>
251
+ std::string, String, SimpleAllocator ,
252
+ ConcurrentHashTableInfoByPtr<std::string, String, SimpleAllocator >>
240
253
HashTable (Allocator, 100 );
241
254
242
255
// Check parallel insertion.
0 commit comments