[libc-commits] [libc] 19b4e9d - [libc][cpp] Add a constructor to ArrayRef to construct from void * data.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Mon Feb 14 09:03:12 PST 2022
Author: Siva Chandra Reddy
Date: 2022-02-14T17:02:54Z
New Revision: 19b4e9d76ecc9a5343c093bc54d965734b996518
URL: https://github.com/llvm/llvm-project/commit/19b4e9d76ecc9a5343c093bc54d965734b996518
DIFF: https://github.com/llvm/llvm-project/commit/19b4e9d76ecc9a5343c093bc54d965734b996518.diff
LOG: [libc][cpp] Add a constructor to ArrayRef to construct from void * data.
Also modified operator[] to return a reference to the array element.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D119725
Added:
Modified:
libc/src/__support/CPP/ArrayRef.h
libc/test/src/__support/CPP/arrayref_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/CPP/ArrayRef.h b/libc/src/__support/CPP/ArrayRef.h
index d6833e6969ef4..64e072fd60583 100644
--- a/libc/src/__support/CPP/ArrayRef.h
+++ b/libc/src/__support/CPP/ArrayRef.h
@@ -58,7 +58,7 @@ template <typename QualifiedT> class ArrayRefBase {
bool empty() const { return size() == 0; }
- auto operator[](size_t Index) const { return data()[Index]; }
+ auto &operator[](size_t Index) const { return data()[Index]; }
// slice(n, m) - Chop off the first N elements of the array, and keep M
// elements in the array.
@@ -115,6 +115,11 @@ template <typename T> struct ArrayRef : public internal::ArrayRefBase<const T> {
using Impl::Impl;
public:
+ // Construct an ArrayRef from void * pointer.
+ // |Length| is the byte length of the array pointed to by |Data|.
+ ArrayRef(const void *Data, size_t Length)
+ : Impl(reinterpret_cast<const T *>(Data), Length / sizeof(T)) {}
+
// From Array.
template <size_t N> ArrayRef(const Array<T, N> &Arr) : Impl(Arr.Data, N) {}
};
@@ -129,6 +134,11 @@ struct MutableArrayRef : public internal::ArrayRefBase<T> {
using Impl::Impl;
public:
+ // Construct an ArrayRef from void * pointer.
+ // |Length| is the byte length of the array pointed to by |Data|.
+ MutableArrayRef(void *Data, size_t Length)
+ : Impl(reinterpret_cast<T *>(Data), Length / sizeof(T)) {}
+
// From Array.
template <size_t N> MutableArrayRef(Array<T, N> &Arr) : Impl(Arr.Data, N) {}
diff --git a/libc/test/src/__support/CPP/arrayref_test.cpp b/libc/test/src/__support/CPP/arrayref_test.cpp
index 79466c7d23626..7cdff32d49520 100644
--- a/libc/test/src/__support/CPP/arrayref_test.cpp
+++ b/libc/test/src/__support/CPP/arrayref_test.cpp
@@ -218,5 +218,22 @@ TYPED_TEST(LlvmLibcArrayRefTest, TakeBack, Types) {
}
}
+TEST(LlvmLibcArrayRefTest, ConstructFromVoidPtr) {
+ unsigned data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+ void *ptr = data;
+ const void *const_ptr = data;
+ ArrayRef<unsigned> ref(const_ptr, sizeof(data));
+ MutableArrayRef<unsigned> mutable_ref(ptr, sizeof(data));
+ ASSERT_EQ(ref.size(), sizeof(data) / sizeof(unsigned));
+ ASSERT_EQ(mutable_ref.size(), sizeof(data) / sizeof(unsigned));
+
+ unsigned val = 123;
+ for (size_t i = 0; i < sizeof(data) / sizeof(unsigned); ++i)
+ mutable_ref[i] = val;
+
+ for (size_t i = 0; i < sizeof(data) / sizeof(unsigned); ++i)
+ ASSERT_EQ(ref[i], val);
+}
+
} // namespace cpp
} // namespace __llvm_libc
More information about the libc-commits
mailing list