Remove unnecessary memory copy 06/325006/1
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 28 May 2025 10:35:25 +0000 (19:35 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Fri, 30 May 2025 07:36:03 +0000 (16:36 +0900)
because the index data is already stored in the buffer
so index is only pointed to as a pointer

Change-Id: I4b65d4a3d940ccb870f5bb9ea89d7a3faf50f643
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/pkgmgr_info_handle.cc
src/common/pkgmgr_info_handle.hh

index a4f5d8a37d4325f93672915e9497bd6d1b3f19e6..628f41b360ac13cf1e3edebdb13a53903b879f9f 100644 (file)
 
 #define GET_CSTRING(CLASS_NAME, FUNC_NAME, ELEMENT_NAME)                       \
     const char* CLASS_NAME::FUNC_NAME() const {                                \
-        return GetCString(index_.ELEMENT_NAME);                                \
+        return GetCString(index_->ELEMENT_NAME);                                \
     }
 
 #define GET_LIST_ITERATOR(TYPE, FUNC_NAME, CLASS_NAME, ELEMENT_NAME)           \
     Iterable<TYPE> CLASS_NAME::FUNC_NAME() const {                             \
-        int index = index_.ELEMENT_NAME;                                       \
+        int index = index_->ELEMENT_NAME;                                       \
         if (index < 0)                                                         \
             return Iterable<TYPE>(parcel_, -1);                                \
         return Iterable<TYPE>(parcel_, index);                                 \
@@ -111,6 +111,14 @@ bool SerializedBase::GetBuf(int index, void* buf, uint32_t size) const {
   return true;
 }
 
+uint8_t* SerializedBase::GetRaw() const {
+  return parcel_->GetData();
+}
+
+size_t SerializedBase::GetRawSize() const {
+  return parcel_->GetDataSize();
+}
+
 GET_CSTRING(Label, Name, name)
 GET_CSTRING(Label, Text, text)
 GET_CSTRING(Label, Lang, lang)
@@ -579,7 +587,7 @@ std::string PkgInfoHandle::ToString(package_x* info) {
 }
 
 PkgInfoHandle::PkgInfoHandle(std::unique_ptr<tizen_base::Parcel> parcel)
-    : Serialized<PkgInfoIndex>(parcel.get(), parcel->GetDataSize() - sizeof(index_)),
+    : Serialized<PkgInfoIndex>(parcel.get(), parcel->GetDataSize() - sizeof(*index_)),
       base_parcel_(std::move(parcel)) {}
 
 PkgInfoHandle& PkgInfoHandle::operator=(PkgInfoHandle&& h) noexcept {
@@ -1049,7 +1057,7 @@ std::string AppInfoHandle::ToString(application_x* handle) {
 }
 
 AppInfoHandle::AppInfoHandle(std::unique_ptr<tizen_base::Parcel> parcel)
-    : Serialized<AppInfoIndex>(parcel.get(), parcel->GetDataSize() - sizeof(index_)),
+    : Serialized<AppInfoIndex>(parcel.get(), parcel->GetDataSize() - sizeof(*index_)),
       base_parcel_(std::move(parcel)) {}
 
 AppInfoHandle& AppInfoHandle::operator=(AppInfoHandle&& h) noexcept {
index 68179b9fadfa23a60e7c9acc6c0e1930e1d61500..b5b18d9c654931b96c4b28aeef0119f71bec6cd3 100644 (file)
@@ -28,6 +28,8 @@ class EXPORT_API SerializedBase {
   const char* GetCString(int index) const;
   bool GetInt(int index, int* result) const;
   bool GetBuf(int index, void* buf, uint32_t size) const;
+  uint8_t* GetRaw() const;
+  size_t GetRawSize() const;
 
  protected:
   const tizen_base::Parcel* parcel_;
@@ -36,14 +38,14 @@ class EXPORT_API SerializedBase {
 template<typename T>
 class EXPORT_API Serialized : public SerializedBase {
  public:
-  Serialized(const tizen_base::Parcel* parcel, int data_index) : SerializedBase(parcel) {
-    if (data_index < 0) {
-      memset(&index_, -1, sizeof(index_));
+  Serialized(const tizen_base::Parcel* parcel, int data_index) : SerializedBase(parcel), index_(nullptr) {
+    if (size_t(data_index + sizeof(T)) > GetRawSize())
+      return;
+
+    if (data_index < 0)
       return;
-    }
 
-    if (!GetBuf(data_index, &index_, sizeof(index_)))
-      memset(&index_, -1, sizeof(index_));
+    index_ = (T*)(GetRaw() + data_index);
   }
 
   Serialized(const Serialized& h) = delete;
@@ -53,13 +55,13 @@ class EXPORT_API Serialized : public SerializedBase {
     if (this != &h) {
       SerializedBase::operator=(std::move(h));
       index_ = h.index_;
-      memset(&h.index_, -1, sizeof(h.index_));
+      h.index_ = nullptr;
     }
     return *this;
   }
 
  protected:
-  T index_;
+  T* index_;
 };
 
 template<typename T>