Skip to content

Commit c466bea

Browse files
JoshuaMBayuxuanchen1997
authored andcommitted
[scudo] Add static vector functionality. (#98986)
Summary: 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. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250929
1 parent 9c50606 commit c466bea

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

compiler-rt/lib/scudo/standalone/string_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ScopedString {
4040
void appendString(int Width, int MaxChars, const char *S);
4141
void appendPointer(u64 ptr_value);
4242

43-
Vector<char> String;
43+
Vector<char, 256> String;
4444
};
4545

4646
void Printf(const char *Format, ...) FORMAT(1, 2);

compiler-rt/lib/scudo/standalone/tests/vector_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "vector.h"
1212

1313
TEST(ScudoVectorTest, Basic) {
14-
scudo::Vector<int> V;
14+
scudo::Vector<int, 64U> V;
1515
EXPECT_EQ(V.size(), 0U);
1616
V.push_back(42);
1717
EXPECT_EQ(V.size(), 1U);
@@ -23,7 +23,7 @@ TEST(ScudoVectorTest, Basic) {
2323
}
2424

2525
TEST(ScudoVectorTest, Stride) {
26-
scudo::Vector<scudo::uptr> V;
26+
scudo::Vector<scudo::uptr, 32U> V;
2727
for (scudo::uptr I = 0; I < 1000; I++) {
2828
V.push_back(I);
2929
EXPECT_EQ(V.size(), I + 1U);
@@ -34,7 +34,7 @@ TEST(ScudoVectorTest, Stride) {
3434
}
3535

3636
TEST(ScudoVectorTest, ResizeReduction) {
37-
scudo::Vector<int> V;
37+
scudo::Vector<int, 64U> V;
3838
V.push_back(0);
3939
V.push_back(0);
4040
EXPECT_EQ(V.size(), 2U);
@@ -48,7 +48,7 @@ TEST(ScudoVectorTest, ResizeReduction) {
4848

4949
// Verify that if the reallocate fails, nothing new is added.
5050
TEST(ScudoVectorTest, ReallocateFails) {
51-
scudo::Vector<char> V;
51+
scudo::Vector<char, 256U> V;
5252
scudo::uptr capacity = V.capacity();
5353

5454
// Get the current address space size.

compiler-rt/lib/scudo/standalone/vector.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace scudo {
2121
// implementation supports only POD types.
2222
//
2323
// NOTE: This class is not meant to be used directly, use Vector<T> instead.
24-
template <typename T> class VectorNoCtor {
24+
template <typename T, size_t StaticNumEntries> class VectorNoCtor {
2525
public:
2626
T &operator[](uptr I) {
2727
DCHECK_LT(I, Size);
@@ -116,18 +116,21 @@ template <typename T> class VectorNoCtor {
116116
uptr CapacityBytes = 0;
117117
uptr Size = 0;
118118

119-
T LocalData[256 / sizeof(T)] = {};
119+
T LocalData[StaticNumEntries] = {};
120120
MemMapT ExternalBuffer;
121121
};
122122

123-
template <typename T> class Vector : public VectorNoCtor<T> {
123+
template <typename T, size_t StaticNumEntries>
124+
class Vector : public VectorNoCtor<T, StaticNumEntries> {
124125
public:
125-
constexpr Vector() { VectorNoCtor<T>::init(); }
126+
static_assert(StaticNumEntries > 0U,
127+
"Vector must have a non-zero number of static entries.");
128+
constexpr Vector() { VectorNoCtor<T, StaticNumEntries>::init(); }
126129
explicit Vector(uptr Count) {
127-
VectorNoCtor<T>::init(Count);
130+
VectorNoCtor<T, StaticNumEntries>::init(Count);
128131
this->resize(Count);
129132
}
130-
~Vector() { VectorNoCtor<T>::destroy(); }
133+
~Vector() { VectorNoCtor<T, StaticNumEntries>::destroy(); }
131134
// Disallow copies and moves.
132135
Vector(const Vector &) = delete;
133136
Vector &operator=(const Vector &) = delete;

0 commit comments

Comments
 (0)