Use std::optional for @nullable (AIDL)
Previously, nullable types were mapped to std::unique_ptr for C++
backend. But std::unique_ptr typically involves unnecessary alloc/dealloc.
For example, if nullable string is represented in unique_ptr<string>, we
should do "unique_ptr<string>(new string(value))" to set a value.
To avoid breaking all hand-written parcelables, only new read/write
functions are added to Parcel class and they are used only by
aidl-generated code and their implementations.
Bug: 144773267
Test: build/flash/boot
atest --test-mapping frameworks/native/libs/binder
Merged-In: I2c801e3b69f2f8ccf44267f15cbf79e1d8fbf19e
Change-Id: I2c801e3b69f2f8ccf44267f15cbf79e1d8fbf19e
(cherry picked from commit 1e1c5fbbbe8a76150fe832c8f974cbd543aa0860)
Exempt-From-Owner-Approval: CP from master
diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp
index f1077ae..1d94bd6 100644
--- a/libs/binder/Parcel.cpp
+++ b/libs/binder/Parcel.cpp
@@ -749,6 +749,13 @@
return NO_ERROR;
}
+status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) {
+ if (!str) {
+ return writeInt32(-1);
+ }
+ return writeUtf8AsUtf16(*str);
+}
+
status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
if (!str) {
return writeInt32(-1);
@@ -773,6 +780,12 @@
return writeByteVectorInternal(val.data(), val.size());
}
+status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val)
+{
+ if (!val) return writeInt32(-1);
+ return writeByteVectorInternal(val->data(), val->size());
+}
+
status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
{
if (!val) return writeInt32(-1);
@@ -783,6 +796,12 @@
return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val.data()), val.size());
}
+status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val)
+{
+ if (!val) return writeInt32(-1);
+ return writeByteVectorInternal(reinterpret_cast<const int8_t*>(val->data()), val->size());
+}
+
status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
{
if (!val) return writeInt32(-1);
@@ -794,6 +813,11 @@
return writeTypedVector(val, &Parcel::writeInt32);
}
+status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeInt32);
+}
+
status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeInt32);
@@ -804,6 +828,11 @@
return writeTypedVector(val, &Parcel::writeInt64);
}
+status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeInt64);
+}
+
status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeInt64);
@@ -814,6 +843,11 @@
return writeTypedVector(val, &Parcel::writeUint64);
}
+status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeUint64);
+}
+
status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeUint64);
@@ -824,6 +858,11 @@
return writeTypedVector(val, &Parcel::writeFloat);
}
+status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeFloat);
+}
+
status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeFloat);
@@ -834,6 +873,11 @@
return writeTypedVector(val, &Parcel::writeDouble);
}
+status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeDouble);
+}
+
status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeDouble);
@@ -844,6 +888,11 @@
return writeTypedVector(val, &Parcel::writeBool);
}
+status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeBool);
+}
+
status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeBool);
@@ -854,6 +903,11 @@
return writeTypedVector(val, &Parcel::writeChar);
}
+status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeChar);
+}
+
status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeChar);
@@ -865,12 +919,23 @@
}
status_t Parcel::writeString16Vector(
+ const std::optional<std::vector<std::optional<String16>>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeString16);
+}
+
+status_t Parcel::writeString16Vector(
const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeString16);
}
status_t Parcel::writeUtf8VectorAsUtf16Vector(
+ const std::optional<std::vector<std::optional<std::string>>>& val) {
+ return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
+}
+
+status_t Parcel::writeUtf8VectorAsUtf16Vector(
const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
}
@@ -995,6 +1060,15 @@
return err;
}
+status_t Parcel::writeString16(const std::optional<String16>& str)
+{
+ if (!str) {
+ return writeInt32(-1);
+ }
+
+ return writeString16(*str);
+}
+
status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
{
if (!str) {
@@ -1037,11 +1111,20 @@
return writeTypedVector(val, &Parcel::writeStrongBinder);
}
+status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val)
+{
+ return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
+}
+
status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
{
return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
}
+status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
+}
+
status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
}
@@ -1140,6 +1223,10 @@
return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
}
+status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) {
+ return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
+}
+
status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
}
@@ -1471,6 +1558,17 @@
return readByteVectorInternal(val, size);
}
+status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const {
+ size_t size;
+ if (status_t status = reserveOutVector(val, &size); status != OK) return status;
+ if (!*val) {
+ // reserveOutVector does not create the out vector if size is < 0.
+ // This occurs when writing a null byte vector.
+ return OK;
+ }
+ return readByteVectorInternal(&**val, size);
+}
+
status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
size_t size;
if (status_t status = reserveOutVector(val, &size); status != OK) return status;
@@ -1482,6 +1580,17 @@
return readByteVectorInternal(val->get(), size);
}
+status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const {
+ size_t size;
+ if (status_t status = reserveOutVector(val, &size); status != OK) return status;
+ if (!*val) {
+ // reserveOutVector does not create the out vector if size is < 0.
+ // This occurs when writing a null byte vector.
+ return OK;
+ }
+ return readByteVectorInternal(&**val, size);
+}
+
status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
size_t size;
if (status_t status = reserveOutVector(val, &size); status != OK) return status;
@@ -1493,6 +1602,10 @@
return readByteVectorInternal(val->get(), size);
}
+status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readInt32);
+}
+
status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
return readNullableTypedVector(val, &Parcel::readInt32);
}
@@ -1501,6 +1614,10 @@
return readTypedVector(val, &Parcel::readInt32);
}
+status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readInt64);
+}
+
status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
return readNullableTypedVector(val, &Parcel::readInt64);
}
@@ -1509,6 +1626,10 @@
return readTypedVector(val, &Parcel::readInt64);
}
+status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readUint64);
+}
+
status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const {
return readNullableTypedVector(val, &Parcel::readUint64);
}
@@ -1517,6 +1638,10 @@
return readTypedVector(val, &Parcel::readUint64);
}
+status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readFloat);
+}
+
status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
return readNullableTypedVector(val, &Parcel::readFloat);
}
@@ -1525,6 +1650,10 @@
return readTypedVector(val, &Parcel::readFloat);
}
+status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readDouble);
+}
+
status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
return readNullableTypedVector(val, &Parcel::readDouble);
}
@@ -1533,6 +1662,28 @@
return readTypedVector(val, &Parcel::readDouble);
}
+status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const {
+ const int32_t start = dataPosition();
+ int32_t size;
+ status_t status = readInt32(&size);
+ val->reset();
+
+ if (status != OK || size < 0) {
+ return status;
+ }
+
+ setDataPosition(start);
+ val->emplace();
+
+ status = readBoolVector(&**val);
+
+ if (status != OK) {
+ val->reset();
+ }
+
+ return status;
+}
+
status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
const int32_t start = dataPosition();
int32_t size;
@@ -1585,6 +1736,10 @@
return OK;
}
+status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readChar);
+}
+
status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
return readNullableTypedVector(val, &Parcel::readChar);
}
@@ -1594,6 +1749,11 @@
}
status_t Parcel::readString16Vector(
+ std::optional<std::vector<std::optional<String16>>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readString16);
+}
+
+status_t Parcel::readString16Vector(
std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
return readNullableTypedVector(val, &Parcel::readString16);
}
@@ -1603,6 +1763,11 @@
}
status_t Parcel::readUtf8VectorFromUtf16Vector(
+ std::optional<std::vector<std::optional<std::string>>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
+}
+
+status_t Parcel::readUtf8VectorFromUtf16Vector(
std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
}
@@ -1794,6 +1959,21 @@
return NO_ERROR;
}
+status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const {
+ const int32_t start = dataPosition();
+ int32_t size;
+ status_t status = readInt32(&size);
+ str->reset();
+
+ if (status != OK || size < 0) {
+ return status;
+ }
+
+ setDataPosition(start);
+ str->emplace();
+ return readUtf8FromUtf16(&**str);
+}
+
status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
const int32_t start = dataPosition();
int32_t size;
@@ -1870,6 +2050,29 @@
return String16();
}
+status_t Parcel::readString16(std::optional<String16>* pArg) const
+{
+ const int32_t start = dataPosition();
+ int32_t size;
+ status_t status = readInt32(&size);
+ pArg->reset();
+
+ if (status != OK || size < 0) {
+ return status;
+ }
+
+ setDataPosition(start);
+ pArg->emplace();
+
+ status = readString16(&**pArg);
+
+ if (status != OK) {
+ pArg->reset();
+ }
+
+ return status;
+}
+
status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
{
const int32_t start = dataPosition();
@@ -2075,6 +2278,10 @@
return OK;
}
+status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const {
+ return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
+}
+
status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
}