mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
238 lines
8.5 KiB
Diff
238 lines
8.5 KiB
Diff
From be653ec8d4b0a5944fcf084a911389cb0c5cc205 Mon Sep 17 00:00:00 2001
|
|
From: PJ Reiniger <pj.reiniger@gmail.com>
|
|
Date: Sat, 7 May 2022 23:07:38 -0400
|
|
Subject: [PATCH 11/31] Remove allocator from collections
|
|
|
|
---
|
|
llvm/include/llvm/ADT/StringMap.h | 33 +++++++-------------------
|
|
llvm/include/llvm/ADT/StringMapEntry.h | 25 +++++++------------
|
|
llvm/unittests/ADT/StringMapTest.cpp | 14 +++++------
|
|
3 files changed, 23 insertions(+), 49 deletions(-)
|
|
|
|
diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h
|
|
index 5f463cfef943..3b40bba37f58 100644
|
|
--- a/llvm/include/llvm/ADT/StringMap.h
|
|
+++ b/llvm/include/llvm/ADT/StringMap.h
|
|
@@ -104,10 +104,8 @@ public:
|
|
/// keys that are "strings", which are basically ranges of bytes. This does some
|
|
/// funky memory allocation and hashing things to make it extremely efficient,
|
|
/// storing the string data *after* the value in the map.
|
|
-template <typename ValueTy, typename AllocatorTy = MallocAllocator>
|
|
+template<typename ValueTy>
|
|
class StringMap : public StringMapImpl {
|
|
- AllocatorTy Allocator;
|
|
-
|
|
public:
|
|
using MapEntryTy = StringMapEntry<ValueTy>;
|
|
|
|
@@ -116,14 +114,6 @@ public:
|
|
explicit StringMap(unsigned InitialSize)
|
|
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
|
|
|
|
- explicit StringMap(AllocatorTy A)
|
|
- : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {
|
|
- }
|
|
-
|
|
- StringMap(unsigned InitialSize, AllocatorTy A)
|
|
- : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
|
|
- Allocator(A) {}
|
|
-
|
|
StringMap(std::initializer_list<std::pair<std::string_view, ValueTy>> List)
|
|
: StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {
|
|
for (const auto &P : List) {
|
|
@@ -132,11 +122,10 @@ public:
|
|
}
|
|
|
|
StringMap(StringMap &&RHS)
|
|
- : StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
|
|
+ : StringMapImpl(std::move(RHS)) {}
|
|
|
|
- StringMap(const StringMap &RHS)
|
|
- : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
|
|
- Allocator(RHS.Allocator) {
|
|
+ StringMap(const StringMap &RHS) :
|
|
+ StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {
|
|
if (RHS.empty())
|
|
return;
|
|
|
|
@@ -156,7 +145,7 @@ public:
|
|
}
|
|
|
|
TheTable[I] = MapEntryTy::Create(
|
|
- static_cast<MapEntryTy *>(Bucket)->getKey(), Allocator,
|
|
+ static_cast<MapEntryTy *>(Bucket)->getKey(),
|
|
static_cast<MapEntryTy *>(Bucket)->getValue());
|
|
HashTable[I] = RHSHashTable[I];
|
|
}
|
|
@@ -171,7 +160,6 @@ public:
|
|
|
|
StringMap &operator=(StringMap RHS) {
|
|
StringMapImpl::swap(RHS);
|
|
- std::swap(Allocator, RHS.Allocator);
|
|
return *this;
|
|
}
|
|
|
|
@@ -183,16 +171,13 @@ public:
|
|
for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
|
|
StringMapEntryBase *Bucket = TheTable[I];
|
|
if (Bucket && Bucket != getTombstoneVal()) {
|
|
- static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
|
|
+ static_cast<MapEntryTy *>(Bucket)->Destroy();
|
|
}
|
|
}
|
|
}
|
|
free(TheTable);
|
|
}
|
|
|
|
- AllocatorTy &getAllocator() { return Allocator; }
|
|
- const AllocatorTy &getAllocator() const { return Allocator; }
|
|
-
|
|
using key_type = const char *;
|
|
using mapped_type = ValueTy;
|
|
using value_type = StringMapEntry<ValueTy>;
|
|
@@ -321,7 +306,7 @@ public:
|
|
|
|
if (Bucket == getTombstoneVal())
|
|
--NumTombstones;
|
|
- Bucket = MapEntryTy::Create(Key, Allocator, std::forward<ArgsTy>(Args)...);
|
|
+ Bucket = MapEntryTy::Create(Key, std::forward<ArgsTy>(Args)...);
|
|
++NumItems;
|
|
assert(NumItems + NumTombstones <= NumBuckets);
|
|
|
|
@@ -339,7 +324,7 @@ public:
|
|
for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
|
|
StringMapEntryBase *&Bucket = TheTable[I];
|
|
if (Bucket && Bucket != getTombstoneVal()) {
|
|
- static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
|
|
+ static_cast<MapEntryTy *>(Bucket)->Destroy();
|
|
}
|
|
Bucket = nullptr;
|
|
}
|
|
@@ -355,7 +340,7 @@ public:
|
|
void erase(iterator I) {
|
|
MapEntryTy &V = *I;
|
|
remove(&V);
|
|
- V.Destroy(Allocator);
|
|
+ V.Destroy();
|
|
}
|
|
|
|
bool erase(std::string_view Key) {
|
|
diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h
|
|
index 93e13b5bb16c..66a30698d787 100644
|
|
--- a/llvm/include/llvm/ADT/StringMapEntry.h
|
|
+++ b/llvm/include/llvm/ADT/StringMapEntry.h
|
|
@@ -33,22 +33,14 @@ protected:
|
|
/// Helper to tail-allocate \p Key. It'd be nice to generalize this so it
|
|
/// could be reused elsewhere, maybe even taking an llvm::function_ref to
|
|
/// type-erase the allocator and put it in a source file.
|
|
- template <typename AllocatorTy>
|
|
static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
|
|
- std::string_view Key, AllocatorTy &Allocator);
|
|
-};
|
|
-
|
|
-// Define out-of-line to dissuade inlining.
|
|
-template <typename AllocatorTy>
|
|
-void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
|
|
- std::string_view Key,
|
|
- AllocatorTy &Allocator) {
|
|
+ std::string_view Key) {
|
|
size_t KeyLength = Key.size();
|
|
|
|
// Allocate a new item with space for the string at the end and a null
|
|
// terminator.
|
|
size_t AllocSize = EntrySize + KeyLength + 1;
|
|
- void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
|
|
+ void *Allocation = safe_malloc(AllocSize);
|
|
assert(Allocation && "Unhandled out-of-memory");
|
|
|
|
// Copy the string information.
|
|
@@ -58,6 +50,7 @@ void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
|
|
Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
|
|
return Allocation;
|
|
}
|
|
+};
|
|
|
|
/// StringMapEntryStorage - Holds the value in a StringMapEntry.
|
|
///
|
|
@@ -117,11 +110,11 @@ public:
|
|
|
|
/// Create a StringMapEntry for the specified key construct the value using
|
|
/// \p InitiVals.
|
|
- template <typename AllocatorTy, typename... InitTy>
|
|
- static StringMapEntry *Create(std::string_view key, AllocatorTy &allocator,
|
|
+ template <typename... InitTy>
|
|
+ static StringMapEntry *Create(std::string_view key,
|
|
InitTy &&... initVals) {
|
|
return new (StringMapEntryBase::allocateWithKey(
|
|
- sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
|
|
+ sizeof(StringMapEntry), alignof(StringMapEntry), key))
|
|
StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
|
|
}
|
|
|
|
@@ -134,12 +127,10 @@ public:
|
|
|
|
/// Destroy - Destroy this StringMapEntry, releasing memory back to the
|
|
/// specified allocator.
|
|
- template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
|
|
+ void Destroy() {
|
|
// Free memory referenced by the item.
|
|
- size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
|
|
this->~StringMapEntry();
|
|
- allocator.Deallocate(static_cast<void *>(this), AllocSize,
|
|
- alignof(StringMapEntry));
|
|
+ std::free(static_cast<void *>(this));
|
|
}
|
|
};
|
|
|
|
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp
|
|
index 5211f01bbd73..28d710fe69e9 100644
|
|
--- a/llvm/unittests/ADT/StringMapTest.cpp
|
|
+++ b/llvm/unittests/ADT/StringMapTest.cpp
|
|
@@ -223,13 +223,12 @@ TEST_F(StringMapTest, IterationTest) {
|
|
|
|
// Test StringMapEntry::Create() method.
|
|
TEST_F(StringMapTest, StringMapEntryTest) {
|
|
- MallocAllocator Allocator;
|
|
StringMap<uint32_t>::value_type *entry =
|
|
StringMap<uint32_t>::value_type::Create(
|
|
std::string_view(testKeyFirst, testKeyLength), 1u);
|
|
EXPECT_STREQ(testKey, entry->first().data());
|
|
EXPECT_EQ(1u, entry->second);
|
|
- entry->Destroy(Allocator);
|
|
+ entry->Destroy();
|
|
}
|
|
|
|
// Test insert() method.
|
|
@@ -238,7 +237,7 @@ TEST_F(StringMapTest, InsertTest) {
|
|
testMap.insert(
|
|
StringMap<uint32_t>::value_type::Create(
|
|
std::string_view(testKeyFirst, testKeyLength),
|
|
- testMap.getAllocator(), 1u));
|
|
+ 1u));
|
|
assertSingleItemMap();
|
|
}
|
|
|
|
@@ -353,15 +352,14 @@ TEST_F(StringMapTest, MoveOnly) {
|
|
StringMap<MoveOnly> t;
|
|
t.insert(std::make_pair("Test", MoveOnly(42)));
|
|
std::string_view Key = "Test";
|
|
- StringMapEntry<MoveOnly>::Create(Key, t.getAllocator(), MoveOnly(42))
|
|
- ->Destroy(t.getAllocator());
|
|
+ StringMapEntry<MoveOnly>::Create(Key, MoveOnly(42))
|
|
+ ->Destroy();
|
|
}
|
|
|
|
TEST_F(StringMapTest, CtorArg) {
|
|
std::string_view Key = "Test";
|
|
- MallocAllocator Allocator;
|
|
- StringMapEntry<MoveOnly>::Create(Key, Allocator, Immovable())
|
|
- ->Destroy(Allocator);
|
|
+ StringMapEntry<MoveOnly>::Create(Key, Immovable())
|
|
+ ->Destroy();
|
|
}
|
|
|
|
TEST_F(StringMapTest, MoveConstruct) {
|
|
--
|
|
2.20.1.windows.1
|
|
|