Use modified tizen_base::Parcel 66/281966/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 23 Sep 2022 11:06:39 +0000 (11:06 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 27 Sep 2022 02:05:59 +0000 (02:05 +0000)
To improve the performance of the parcel creation, the implementation of
the Parcel is changed. It uses malloc() instead of std::vector.

Requires:
 - https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/281779/

Change-Id: Iaf01e6e94e34e10a36b8ca17081c6aba3b3b51e1
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/debug-port-internal.cc
src/parcel-internal.cc
src/parcel-internal.hh
src/proxy-internal.cc
src/rpc-port-parcel.cc
src/stub-internal.cc

index e5c1802..06e44dc 100644 (file)
@@ -327,7 +327,7 @@ void DebugPortImpl::CreateThread() {
       do {
         std::shared_ptr<tizen_base::Parcel> parcel;
         queue_.WaitAndPop(parcel);
-        int len = parcel->GetRaw().size();
+        int len = parcel->GetDataSize();
         if (len == 0) {
           _W("Done");
           break;
@@ -343,7 +343,7 @@ void DebugPortImpl::CreateThread() {
           continue;
         }
 
-        ret = port_->Write(&*parcel->GetRaw().cbegin(), len);
+        ret = port_->Write(parcel->GetData(), len);
         if (ret < 0) {
           _E("Failed to write data");
           SetConnectionStatus(false);
index ba6ba66..75f5805 100644 (file)
 namespace rpc_port {
 namespace internal {
 
-Parcel::Parcel() {
-  parcel_create(&handle_);
-}
+Parcel::Parcel() {}
 
-Parcel::~Parcel() {
-  if (handle_)
-    parcel_destroy(handle_);
-}
+Parcel::~Parcel() {}
 
 void Parcel::WriteToParcel(tizen_base::Parcel* parcel) const {
   parcel->WriteParcelable(header_);
 
-  void* raw = nullptr;
-  uint32_t size = 0;
-  parcel_get_raw(handle_, &raw, &size);
-  parcel->WriteUInt32(size);
-  parcel->Write(raw, size);
+  parcel->WriteUInt32(handle_.GetDataSize());
+  parcel->Write(handle_.GetData(), handle_.GetDataSize());
 }
 
 void Parcel::ReadFromParcel(tizen_base::Parcel* parcel) {
@@ -47,15 +39,14 @@ void Parcel::ReadFromParcel(tizen_base::Parcel* parcel) {
   uint32_t size = 0;
   parcel->ReadUInt32(&size);
   if (size > 0) {
-    auto buf = new (std::nothrow) uint8_t[size];
+    auto* buf = static_cast<uint8_t*>(malloc(size));
     if (buf == nullptr) {
       _E("Out of memory");
       return;
     }
 
-    std::unique_ptr<uint8_t[]> ptr(buf);
     parcel->Read(buf, size);
-    parcel_reset(handle_, buf, size);
+    handle_ = std::move(tizen_base::Parcel(buf, size, false));
   }
 }
 
@@ -64,7 +55,7 @@ const ParcelHeader* Parcel::GetParcelHeader() {
 }
 
 parcel_h Parcel::GetHandle() const {
-  return handle_;
+  return static_cast<parcel_h>(const_cast<tizen_base::Parcel*>(&handle_));
 }
 
 }  // namespace internal
index 77507fb..6922003 100644 (file)
@@ -19,6 +19,8 @@
 
 #include <parcel.h>
 
+#include <parcel.hh>
+
 #include <parcelable.hh>
 
 #include "parcel-header-internal.hh"
@@ -39,7 +41,7 @@ class Parcel : public tizen_base::Parcelable {
 
  private:
   ParcelHeader header_;
-  parcel_h handle_ = nullptr;
+  tizen_base::Parcel handle_;
 };
 
 }  // namespace internal
index 771a303..8ddb569 100644 (file)
@@ -53,15 +53,14 @@ std::string GenInstance() {
 int SendRequest(ClientSocket* client, const Request& request) {
   tizen_base::Parcel parcel;
   parcel.WriteParcelable(const_cast<Request&>(request));
-  const std::vector<uint8_t>& raw = parcel.GetRaw();
-  size_t size = raw.size();
+  size_t size = parcel.GetDataSize();
   int ret = client->Send(reinterpret_cast<void*>(&size), sizeof(size));
   if (ret != 0) {
     _E("Send() is failed. error(%d)", ret);
     return -1;
   }
 
-  ret = client->Send(raw.data(), raw.size());
+  ret = client->Send(parcel.GetData(), size);
   if (ret != 0) {
     _E("Send() is failed. error(%d)", ret);
     return -1;
@@ -78,14 +77,20 @@ int ReceiveResponse(ClientSocket* client, Response** response) {
     return -1;
   }
 
-  std::vector<uint8_t> buf(size);
-  ret = client->Receive(buf.data(), size);
+  uint8_t* buf = static_cast<uint8_t*>(malloc(size));
+  if (buf == nullptr) {
+    _E("Out of memory");
+    return -1;
+  }
+
+  ret = client->Receive(buf, size);
   if (ret != 0) {
     _E("Receive() is failed. error(%d)", ret);
+    free(buf);
     return -1;
   }
 
-  tizen_base::Parcel parcel(buf.data(), buf.size());
+  tizen_base::Parcel parcel(buf, size, false);
   *response = new (std::nothrow) Response();
   if (*response == nullptr) {
     _E("Out of memory");
index 35361db..86af7b3 100644 (file)
@@ -62,7 +62,7 @@ RPC_API int rpc_port_parcel_create_from_port(rpc_port_parcel_h* h,
     if (len <= 0 || len > MAX_PARCEL_SIZE)
       return RPC_PORT_ERROR_IO_ERROR;
 
-    buf = new (std::nothrow) unsigned char[len];
+    buf = static_cast<unsigned char*>(malloc(len));
     if (buf == nullptr) {
       _E("Out of memory");
       return RPC_PORT_ERROR_IO_ERROR;
@@ -70,7 +70,7 @@ RPC_API int rpc_port_parcel_create_from_port(rpc_port_parcel_h* h,
 
     ret = rpc_port_read(port, buf, len);
     if (ret != 0) {
-      delete[] buf;
+      free(buf);
       return ret;
     }
   }
@@ -78,13 +78,12 @@ RPC_API int rpc_port_parcel_create_from_port(rpc_port_parcel_h* h,
   auto* parcel = new (std::nothrow) internal::Parcel();
   if (parcel == nullptr) {
     _E("Out of memory");
-    delete[] buf;
+    free(buf);
     return RPC_PORT_ERROR_IO_ERROR;
   }
 
-  tizen_base::Parcel raw_parcel(buf, len);
+  tizen_base::Parcel raw_parcel(buf, len, false);
   raw_parcel.ReadParcelable(parcel);
-  delete[] buf;
   *h = static_cast<rpc_port_parcel_h>(parcel);
   return RPC_PORT_ERROR_NONE;
 }
@@ -96,9 +95,8 @@ RPC_API int rpc_port_parcel_send(rpc_port_parcel_h h, rpc_port_h port) {
   auto* parcel = static_cast<internal::Parcel*>(h);
   tizen_base::Parcel raw_parcel;
   raw_parcel.WriteParcelable(*parcel);
-  const std::vector<uint8_t>& raw_data = raw_parcel.GetRaw();
-  void* raw = reinterpret_cast<void*>(const_cast<uint8_t*>(&raw_data[0]));
-  uint32_t len = raw_data.size();
+  void* raw = reinterpret_cast<void*>(raw_parcel.GetData());
+  uint32_t len = raw_parcel.GetDataSize();
   if (len <= 0)
     return RPC_PORT_ERROR_INVALID_PARAMETER;
 
index 5a9d014..5508bc4 100644 (file)
@@ -69,15 +69,14 @@ int ReceiveRequest(ClientSocket* client, Request** request) {
 int SendResponse(ClientSocket* client, const Response& response) {
   tizen_base::Parcel parcel;
   parcel.WriteParcelable(const_cast<Response&>(response));
-  const std::vector<uint8_t>& raw = parcel.GetRaw();
-  size_t size = raw.size();
+  size_t size = parcel.GetDataSize();
   int ret = client->Send(reinterpret_cast<void*>(&size), sizeof(size));
   if (ret != 0) {
     _E("Send() is failed. error(%d)", ret);
     return -1;
   }
 
-  ret = client->Send(raw.data(), raw.size());
+  ret = client->Send(parcel.GetData(), size);
   if (ret != 0) {
     _E("Send() is failed. error(%d)", ret);
     return -1;