Add ownership parameter to parcel constructor 33/319933/3
authorIlho Kim <ilho159.kim@samsung.com>
Fri, 14 Feb 2025 09:54:43 +0000 (18:54 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Thu, 20 Feb 2025 04:23:27 +0000 (13:23 +0900)
To support the parcel type that doesn't have the ownership of the buffer

Change-Id: Idb841472268135e325a065046e91199eedd868f7

src/parcel/parcel.cc
src/parcel/parcel.hh
tests/parcel_unittests/test_parcel_cpp.cc

index 9725e5138bab72afc713454a2f258a607d2e7a31..96281f063009725d1ff8e9471f5a5877841e3405 100644 (file)
@@ -213,13 +213,14 @@ Parcel::Parcel() : impl_(new Impl(this, kDataCapacity, nullptr)) {
   if (impl_->data_ == nullptr) throw std::bad_alloc();
 }
 
-Parcel::Parcel(const void* buf, uint32_t size, bool copy)
+Parcel::Parcel(const void* buf, uint32_t size, bool copy, bool ownership)
     : impl_(new Impl(
           this, size,
           copy ? nullptr : static_cast<uint8_t*>(const_cast<void*>(buf)))) {
   if (impl_->data_ == nullptr) throw std::bad_alloc();
 
   if (copy) impl_->Write(buf, size);
+  else impl_->ownership_ = ownership;
 }
 
 Parcel::Parcel(const Parcel& parcel, uint32_t start_pos, uint32_t size)
index 1fc1e381bea0ce8f72410313d8fa984e04428c55..c8859ffab89f031a8a69ec829cf2504733838e97 100644 (file)
@@ -63,9 +63,11 @@ class EXPORT Parcel final {
    * @param[in] buf The bytes to write
    * @param[in] size the size of bytes to write
    * @param[in] copy If @c is true, this object copies bytes.
-   *                 If @c is false, this object takes ownership of @buf.
+   *                 If @c is false, this object takes ownership of @buf if @ownership is true
+   * @param[in] ownership Only meaningful when the @copy is false
+   *                      If @c is false, this object doesn't have ownership of buf.
    */
-  Parcel(const void* buf, uint32_t size, bool copy = true);
+  Parcel(const void* buf, uint32_t size, bool copy = true, bool ownership = true);
 
   /**
    * @brief Construct from Parcel object.
index ff872d906e00f4aa84439aa5f36ef314728b8786..56600fbed24dff72d8136912763523191c5348e5 100644 (file)
@@ -376,3 +376,38 @@ TEST_F(ParcelCppTest, SubParcel_N) {
 
   EXPECT_THROW(Parcel w1(p, 0, 100), std::bad_alloc);
 }
+
+TEST_F(ParcelCppTest, NonOwnershipParcel) {
+  auto& p = GetHandle();
+  int pos0 = p.GetDataSize();
+
+  p.WriteString("Test non_ownership_parcel1 1");
+  p.WriteString("Test non_ownership_parcel1 2");
+  int pos1 = p.GetDataSize();
+  p.WriteString("Test non_ownership_parcel2 1");
+  p.WriteString("Test non_ownership_parcel2 2");
+  int pos2 = p.GetDataSize();
+  p.WriteString("Test non_ownership_parcel3 1");
+  p.WriteString("Test non_ownership_parcel3 2");
+  int pos3 = p.GetDataSize();
+  p.WriteString("Test non_ownership_parcel4 1");
+  p.WriteString("Test non_ownership_parcel4 2");
+  int pos4 = p.GetDataSize();
+
+  Parcel r1(p.GetData() + pos0, pos1 - pos0, false, false);
+  Parcel r2(p.GetData() + pos1, pos2 - pos1, false, false);
+  Parcel r3(p.GetData() + pos2, pos3 - pos2, false, false);
+  Parcel r4(p.GetData() + pos3, pos4 - pos3, false, false);
+
+  ASSERT_EQ(r1.ReadString(), "Test non_ownership_parcel1 1");
+  ASSERT_EQ(r1.ReadString(), "Test non_ownership_parcel1 2");
+
+  ASSERT_EQ(r3.ReadString(), "Test non_ownership_parcel3 1");
+  ASSERT_EQ(r3.ReadString(), "Test non_ownership_parcel3 2");
+
+  ASSERT_EQ(r2.ReadString(), "Test non_ownership_parcel2 1");
+  ASSERT_EQ(r2.ReadString(), "Test non_ownership_parcel2 2");
+
+  ASSERT_EQ(r4.ReadString(), "Test non_ownership_parcel4 1");
+  ASSERT_EQ(r4.ReadString(), "Test non_ownership_parcel4 2");
+}