[compiler-rt] d08527e - [scudo] Add static vector functionality. (#98986)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 17 15:21:57 PDT 2024
Author: Joshua Baehring
Date: 2024-07-17T15:21:52-07:00
New Revision: d08527ee3ee2dc1e90d2afcc6e5982d0997dad20
URL: https://github.com/llvm/llvm-project/commit/d08527ee3ee2dc1e90d2afcc6e5982d0997dad20
DIFF: https://github.com/llvm/llvm-project/commit/d08527ee3ee2dc1e90d2afcc6e5982d0997dad20.diff
LOG: [scudo] Add static vector functionality. (#98986)
The scudo vector implementation maintains static local data before
switching to dynamically allocated data as the array size grows.
Users of the vector must now specify the size of the static local data
through the vector template (the default size has been removed).
If 0 is specified for the size of the static local data, an assertion
will
be triggered.
Added:
Modified:
compiler-rt/lib/scudo/standalone/string_utils.h
compiler-rt/lib/scudo/standalone/tests/vector_test.cpp
compiler-rt/lib/scudo/standalone/vector.h
Removed:
################################################################################
diff --git a/compiler-rt/lib/scudo/standalone/string_utils.h b/compiler-rt/lib/scudo/standalone/string_utils.h
index 6e00b63779737..cf61e150f20e5 100644
--- a/compiler-rt/lib/scudo/standalone/string_utils.h
+++ b/compiler-rt/lib/scudo/standalone/string_utils.h
@@ -40,7 +40,7 @@ class ScopedString {
void appendString(int Width, int MaxChars, const char *S);
void appendPointer(u64 ptr_value);
- Vector<char> String;
+ Vector<char, 256> String;
};
void Printf(const char *Format, ...) FORMAT(1, 2);
diff --git a/compiler-rt/lib/scudo/standalone/tests/vector_test.cpp b/compiler-rt/lib/scudo/standalone/tests/vector_test.cpp
index 1547824c11763..a972d24a62688 100644
--- a/compiler-rt/lib/scudo/standalone/tests/vector_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/vector_test.cpp
@@ -11,7 +11,7 @@
#include "vector.h"
TEST(ScudoVectorTest, Basic) {
- scudo::Vector<int> V;
+ scudo::Vector<int, 64U> V;
EXPECT_EQ(V.size(), 0U);
V.push_back(42);
EXPECT_EQ(V.size(), 1U);
@@ -23,7 +23,7 @@ TEST(ScudoVectorTest, Basic) {
}
TEST(ScudoVectorTest, Stride) {
- scudo::Vector<scudo::uptr> V;
+ scudo::Vector<scudo::uptr, 32U> V;
for (scudo::uptr I = 0; I < 1000; I++) {
V.push_back(I);
EXPECT_EQ(V.size(), I + 1U);
@@ -34,7 +34,7 @@ TEST(ScudoVectorTest, Stride) {
}
TEST(ScudoVectorTest, ResizeReduction) {
- scudo::Vector<int> V;
+ scudo::Vector<int, 64U> V;
V.push_back(0);
V.push_back(0);
EXPECT_EQ(V.size(), 2U);
@@ -48,7 +48,7 @@ TEST(ScudoVectorTest, ResizeReduction) {
// Verify that if the reallocate fails, nothing new is added.
TEST(ScudoVectorTest, ReallocateFails) {
- scudo::Vector<char> V;
+ scudo::Vector<char, 256U> V;
scudo::uptr capacity = V.capacity();
// Get the current address space size.
diff --git a/compiler-rt/lib/scudo/standalone/vector.h b/compiler-rt/lib/scudo/standalone/vector.h
index ca10cc281d770..98b3db4ad6980 100644
--- a/compiler-rt/lib/scudo/standalone/vector.h
+++ b/compiler-rt/lib/scudo/standalone/vector.h
@@ -21,7 +21,7 @@ namespace scudo {
// implementation supports only POD types.
//
// NOTE: This class is not meant to be used directly, use Vector<T> instead.
-template <typename T> class VectorNoCtor {
+template <typename T, size_t StaticNumEntries> class VectorNoCtor {
public:
T &operator[](uptr I) {
DCHECK_LT(I, Size);
@@ -116,18 +116,21 @@ template <typename T> class VectorNoCtor {
uptr CapacityBytes = 0;
uptr Size = 0;
- T LocalData[256 / sizeof(T)] = {};
+ T LocalData[StaticNumEntries] = {};
MemMapT ExternalBuffer;
};
-template <typename T> class Vector : public VectorNoCtor<T> {
+template <typename T, size_t StaticNumEntries>
+class Vector : public VectorNoCtor<T, StaticNumEntries> {
public:
- constexpr Vector() { VectorNoCtor<T>::init(); }
+ static_assert(StaticNumEntries > 0U,
+ "Vector must have a non-zero number of static entries.");
+ constexpr Vector() { VectorNoCtor<T, StaticNumEntries>::init(); }
explicit Vector(uptr Count) {
- VectorNoCtor<T>::init(Count);
+ VectorNoCtor<T, StaticNumEntries>::init(Count);
this->resize(Count);
}
- ~Vector() { VectorNoCtor<T>::destroy(); }
+ ~Vector() { VectorNoCtor<T, StaticNumEntries>::destroy(); }
// Disallow copies and moves.
Vector(const Vector &) = delete;
Vector &operator=(const Vector &) = delete;
More information about the llvm-commits
mailing list