Skip to content

Commit

Permalink
[scudo] Add static vector functionality. (#98986)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JoshuaMBa authored and yuxuanchen1997 committed Jul 25, 2024
1 parent 9c50606 commit c466bea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler-rt/lib/scudo/standalone/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions compiler-rt/lib/scudo/standalone/tests/vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions compiler-rt/lib/scudo/standalone/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c466bea

Please sign in to comment.