hidl_vec: move elements on resize
To avoid extra copies. Doing this also requires adding move
constructors and move assignment operators to hidl_array.
Bug: 135207394
Test: libhidl_test
Test: hidl_test
Test: reduces image size on aosp_walleye
$ cat installed-files-before.txt | awk '{s+=$1}END{print s}'
1135438223
$ cat installed-files-after.txt | awk '{s+=$1}END{print s}'
1135434327
Change-Id: I231f5c9cc959b605e6f102ff7217f338c95d5d5e
diff --git a/test_main.cpp b/test_main.cpp
index 2b9f52c..6e96a42 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -319,6 +319,37 @@
EXPECT_EQ(sum, 1 + 2 + 3);
}
+struct FailsIfCopied {
+ FailsIfCopied() {}
+
+ // add failure if copied since in general this can be expensive
+ FailsIfCopied(const FailsIfCopied& o) { *this = o; }
+ FailsIfCopied& operator=(const FailsIfCopied&) {
+ ADD_FAILURE() << "FailsIfCopied copied";
+ return *this;
+ }
+
+ // fine to move this type since in general this is cheaper
+ FailsIfCopied(FailsIfCopied&& o) = default;
+ FailsIfCopied& operator=(FailsIfCopied&&) = default;
+};
+
+TEST_F(LibHidlTest, VecResizeNoCopy) {
+ using android::hardware::hidl_vec;
+
+ hidl_vec<FailsIfCopied> noCopies;
+ noCopies.resize(3); // instantiates three elements
+
+ FailsIfCopied* oldPointer = noCopies.data();
+
+ noCopies.resize(6); // should move three elements, not copy
+
+ // oldPointer should be invalidated at this point.
+ // hidl_vec doesn't currently try to realloc but if it ever switches
+ // to an implementation that does, this test wouldn't do anything.
+ EXPECT_NE(oldPointer, noCopies.data());
+}
+
TEST_F(LibHidlTest, ArrayTest) {
using android::hardware::hidl_array;
int32_t array[] = {5, 6, 7};