[libc-commits] [PATCH] D119725: [libc][cpp] Add a constructor to ArrayRef to construct from void * data.
Siva Chandra via Phabricator via libc-commits
libc-commits at lists.llvm.org
Mon Feb 14 07:49:50 PST 2022
sivachandra created this revision.
sivachandra added reviewers: lntue, michaelrj.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added a project: libc-project.
sivachandra requested review of this revision.
Also modified operator[] to return a reference to the array element.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D119725
Files:
libc/src/__support/CPP/ArrayRef.h
libc/test/src/__support/CPP/arrayref_test.cpp
Index: libc/test/src/__support/CPP/arrayref_test.cpp
===================================================================
--- libc/test/src/__support/CPP/arrayref_test.cpp
+++ libc/test/src/__support/CPP/arrayref_test.cpp
@@ -218,5 +218,22 @@
}
}
+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
Index: libc/src/__support/CPP/ArrayRef.h
===================================================================
--- libc/src/__support/CPP/ArrayRef.h
+++ libc/src/__support/CPP/ArrayRef.h
@@ -58,7 +58,7 @@
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 @@
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 @@
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) {}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119725.408421.patch
Type: text/x-patch
Size: 2181 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220214/ccb82ec8/attachment.bin>
More information about the libc-commits
mailing list