Skip to content

[scudo] Add static vector functionality. #98986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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