Remove shared memory to save key 39/324239/1
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 14 May 2025 10:25:15 +0000 (19:25 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Wed, 14 May 2025 10:25:15 +0000 (19:25 +0900)
Since the index of the handle is already sorted based on the key,
the key can be retrived from the handle

Change-Id: I8e1588609bb72fea6fe4e65a3ffe5a82e6900863
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/shared_memory/shm_config.hh
src/common/shared_memory/shm_reader.cc
src/common/shared_memory/shm_reader.hh
src/server/shared_memory/shm_writer.cc
src/server/shared_memory/shm_writer.hh

index c3cbfdd648ee6936270f8302deeda1651b4758fa..a51efff461f2fdcd79804e1cb58d8ae006548915 100644 (file)
@@ -146,7 +146,6 @@ class EXPORT_API ShmConfigHandler {
 };
 
 struct HandleMappingData {
-  uint32_t key_index;
   uint32_t index;
   uint32_t len;
 };
index c536588f25876ba4e34113fd6f0dcaae634b9a3e..a25e23219a927ecebd5b0e9f1333f1abfedf5f68 100644 (file)
@@ -79,10 +79,6 @@ std::optional<ShmLockGuard> ShmReader<T>::Load(ShmError* result,
       if (ret != ShmError::NONE)
         LOGE("index_mapper_.Unload() fail");
 
-      ret = key_mapper_.Unload();
-      if (ret != ShmError::NONE)
-        LOGE("key_mapper_.Unload() fail");
-
       if (ret != ShmError::NONE) {
         *result = ret;
         return std::nullopt;
@@ -124,12 +120,6 @@ std::optional<ShmLockGuard> ShmReader<T>::Load(ShmError* result,
     return std::nullopt;
   }
 
-  *result = key_mapper_.LoadAll();
-  if (*result != ShmError::NONE) {
-    LOGE("Failed to load key");
-    return std::nullopt;
-  }
-
   *result = ShmError::NONE;
 
   return read_lock;
@@ -204,15 +194,17 @@ std::optional<T> ShmReader<T>::ReadHandle(
 
 template<typename T>
 HandleMappingData* ShmReader<T>::FindIndex(const char* key, ShmError* result) {
-  const char* key_ptr = reinterpret_cast<const char*>(key_mapper_.GetPtr());
   auto index_ptr = reinterpret_cast<HandleMappingData*>(index_mapper_.GetPtr());
+  auto handle_ptr = handle_mapper_.GetPtr();
 
   int l = 0;
   int r = index_mapper_.GetMemSize() / sizeof(HandleMappingData);
   HandleMappingData* result_ptr = nullptr;
   while (l < r) {
     int mid = (l + r) / 2;
-    int cmp_result = strcmp(key, key_ptr + (index_ptr[mid].key_index));
+    T handle(std::make_unique<tizen_base::Parcel>(
+        handle_ptr + index_ptr[mid].index, index_ptr[mid].len, false, false));
+    int cmp_result = strcmp(key, handle.GetKey());
     if (cmp_result == 0) {
       result_ptr = index_ptr + mid;
       break;
index e1f8676dc30fe38c5577b5e3950f2385884886cd..f34f93d5fc0b07fc4655706f5fb3e826c0dc4b7e 100644 (file)
@@ -24,8 +24,7 @@ class EXPORT_API ShmReader {
       base_name_(std::move(base_name)),
       config_handler_(base_name_ + "_config"),
       handle_mapper_(base_name_ + "_handle"),
-      index_mapper_(base_name_ + "_indexs"),
-      key_mapper_(base_name_ + "_keys") {}
+      index_mapper_(base_name_ + "_indexs") {}
   std::optional<std::vector<T>> GetHandles(ShmError* result);
   std::optional<T> GetHandle(const char* key, ShmError* result);
 
@@ -41,7 +40,6 @@ class EXPORT_API ShmReader {
   ShmConfigHandler config_handler_;
   ShmReadMapper handle_mapper_;
   ShmReadMapper index_mapper_;
-  ShmReadMapper key_mapper_;
   std::shared_mutex lock_;
 };
 
index ffacf3df341dfd85e92c86064845c8e737bb11a2..5af6b1807891b845ee755d85a4f360921950bfe8 100644 (file)
@@ -66,8 +66,8 @@ bool ShmWriter<T>::CreateSharedMemory(const std::vector<T>& handles) {
     return false;
   }
 
-  LOGW("handle(%zu) index(%zu) key(%zu)", handle_mapper_.GetMemSize(),
-      index_mapper_.GetMemSize(), key_mapper_.GetMemSize());
+  LOGW("handle(%zu) index(%zu)", handle_mapper_.GetMemSize(),
+      index_mapper_.GetMemSize());
 
   return true;
 }
@@ -75,14 +75,9 @@ bool ShmWriter<T>::CreateSharedMemory(const std::vector<T>& handles) {
 template<typename T>
 bool ShmWriter<T>::WriteHandles(const std::vector<T>& handles) {
   size_t handle_mem_size = 0;
-  size_t key_mem_size = 0;
-  std::vector<size_t> key_sizes;
-  for (const auto& it : handles) {
-    size_t key_size = strlen(it.GetKey()) + 1;
+  for (const auto& it : handles)
     handle_mem_size += it.GetDataSize();
-    key_mem_size += key_size;
-    key_sizes.emplace_back(key_size);
-  }
+
   LOGD("handle_mem_size : %zu", handle_mem_size);
 
   if (handle_mapper_.Init(handle_mem_size) != ps::ShmError::NONE) {
@@ -90,26 +85,17 @@ bool ShmWriter<T>::WriteHandles(const std::vector<T>& handles) {
     return false;
   }
 
-  if (key_mapper_.Init(key_mem_size) != ps::ShmError::NONE) {
-    LOGE("key_mapper_ init fail");
-    return false;
-  }
-
   int index = 0;
-  int key_index = 0;
   std::vector<ps::HandleMappingData> indexs;
   for (size_t i = 0; i < handles.size(); ++i) {
-    WriteKey(handles[i].GetKey(), key_sizes[i], key_index);
     WriteHandle(handles[i], index);
-    LOGD("inserted handle (%d, %d, %u, %s, %s)", key_index, index,
+    LOGD("inserted handle (%d, %u, %s, %s)", index,
         handles[i].GetDataSize(), handles[i].GetKey(), base_name_.c_str());
 
     indexs.emplace_back(ps::HandleMappingData {
-      .key_index = static_cast<uint32_t>(key_index),
       .index = static_cast<uint32_t>(index),
       .len = handles[i].GetDataSize()
     });
-    key_index += key_sizes[i];
     index += handles[i].GetDataSize();
   }
 
@@ -125,21 +111,15 @@ bool ShmWriter<T>::WriteHandles(const std::vector<T>& handles) {
 
 template<typename T>
 bool ShmWriter<T>::Erase(const std::string& key) {
-  auto [index, key_index] = GetHandleIndex(key.c_str());
-  if (index == -1 || key_index == -1) {
+  int index = GetHandleIndex(key.c_str());
+  if (index == -1) {
     LOGD("key : %s doesn't already exist", key.c_str());
     return true;
   }
 
-  LOGD("Erase key %s, handle_index : %d, key_index : %d",
-      key.c_str(), index, key_index);
+  LOGD("Erase key %s, handle_index : %d", key.c_str(), index);
 
-  if (!RemoveKey(key_index)) {
-    LOGE("Failed to remove key");
-    return false;
-  }
-
-  if (!RemoveIndex(index, key.length() + 1)) {
+  if (!RemoveIndex(index)) {
     LOGE("Failed to remove index");
     return false;
   }
@@ -149,13 +129,13 @@ bool ShmWriter<T>::Erase(const std::string& key) {
 
 template<typename T>
 bool ShmWriter<T>::Insert(const T& handle) {
-  auto [index, key_index] = GetHandleInsertIndex(handle.GetKey());
+  int index = GetHandleInsertIndex(handle.GetKey());
   if (index == -1) {
     LOGE("GetHandleInsertIndex fail");
     return false;
   }
 
-  return InsertHandle(handle, index, key_index);
+  return InsertHandle(handle, index);
 }
 
 template<typename T>
@@ -164,7 +144,6 @@ bool ShmWriter<T>::ClearSharedMemory() {
 
   handle_version_ = 0;
   index_version_ = 0;
-  key_version_ = 0;
 
   config_handler_.SetShmStatus(ps::ShmStatus::INVALID);
   if (config_handler_.Clear() != ps::ShmError::NONE)
@@ -173,8 +152,6 @@ bool ShmWriter<T>::ClearSharedMemory() {
     ret = false;
   if (index_mapper_.Clear() != ps::ShmError::NONE)
     ret = false;
-  if (key_mapper_.Clear() != ps::ShmError::NONE)
-    ret = false;
 
   return ret;
 }
@@ -203,40 +180,41 @@ bool ShmWriter<T>::WriteIndexs(
 }
 
 template<typename T>
-std::pair<int, int> ShmWriter<T>::GetHandleIndex(const char* key) const {
+int ShmWriter<T>::GetHandleIndex(const char* key) const {
   int l = 0;
   int r = GetHandleSize();
-  auto* ptr = (ps::HandleMappingData*)index_mapper_.GetPtr();
+  auto ptr = (ps::HandleMappingData*)index_mapper_.GetPtr();
+  auto handle_ptr = handle_mapper_.GetPtr();
 
   while (l < r) {
     int mid = (l + r) / 2;
-    const char* key_ptr = reinterpret_cast<const char*>(key_mapper_.GetPtr() + ptr[mid].key_index);
-    int result = strcmp(key, key_ptr);
+    T handle(std::make_unique<tizen_base::Parcel>(handle_ptr + ptr[mid].index, ptr[mid].len, false, false));
+    int result = strcmp(key, handle.GetKey());
     if (result == 0)
-      return std::make_pair(mid, ptr[mid].key_index);
+      return mid;
     else if (result < 0)
       r = mid;
     else
       l = mid + 1;
   }
 
-  return std::make_pair(-1, -1);
+  return -1;
 }
 
 template<typename T>
-std::pair<int, int> ShmWriter<T>::GetHandleInsertIndex(const char* key) const {
+int ShmWriter<T>::GetHandleInsertIndex(const char* key) const {
   int l = 0;
   int r = GetHandleSize();
-  auto* ptr = (ps::HandleMappingData*)index_mapper_.GetPtr();
+  auto ptr = (ps::HandleMappingData*)index_mapper_.GetPtr();
+  auto handle_ptr = handle_mapper_.GetPtr();
 
   while (l < r) {
     int mid = (l + r) / 2;
-    const char* key_ptr = reinterpret_cast<const char*>(
-        key_mapper_.GetPtr() + ptr[mid].key_index);
-    int result = strcmp(key, key_ptr);
+    T handle(std::make_unique<tizen_base::Parcel>(handle_ptr + ptr[mid].index, ptr[mid].len, false, false));
+    int result = strcmp(key, handle.GetKey());
     if (result == 0) {
       LOGE("The package[%s] to insert already exists", key);
-      return std::make_pair(-1, -1);
+      return -1;
     } else if (result < 0) {
       r = mid;
     } else {
@@ -244,13 +222,11 @@ std::pair<int, int> ShmWriter<T>::GetHandleInsertIndex(const char* key) const {
     }
   }
 
-  if (l == static_cast<int>(GetHandleSize()))
-    return std::make_pair(l, key_mapper_.GetMemSize());
-  return std::make_pair(l, ptr[l].key_index);
+  return l;
 }
 
 template<typename T>
-bool ShmWriter<T>::RemoveIndex(size_t index, size_t key_size) {
+bool ShmWriter<T>::RemoveIndex(size_t index) {
   auto* ptr = reinterpret_cast<ps::HandleMappingData*>(index_mapper_.GetPtr());
   size_t handle_size = GetHandleSize();
 
@@ -265,44 +241,11 @@ bool ShmWriter<T>::RemoveIndex(size_t index, size_t key_size) {
   index_mapper_.Resize(
       index_mapper_.GetMemSize() - sizeof(ps::HandleMappingData));
 
-  // Resize may change real ptr
-  ptr = reinterpret_cast<ps::HandleMappingData*>(index_mapper_.GetPtr());
-  for (size_t i = index; i < GetHandleSize(); ++i)
-    ptr[i].key_index -= key_size;
-
-  return true;
-}
-
-template<typename T>
-bool ShmWriter<T>::RemoveKey(int index) {
-  if (index < 0) {
-    LOGE("invalid index : %d", index);
-    return false;
-  }
-
-  const char* ptr = reinterpret_cast<const char*>(key_mapper_.GetPtr());
-  size_t handle_size = key_mapper_.GetMemSize();
-
-  size_t key_size = strlen(ptr + index) + 1;
-  size_t moved_num = handle_size - index - key_size;
-  if (moved_num > 0)
-    memmove((void*)(ptr + index), (void*)(ptr + index + key_size), moved_num);
-
-  LOGE("key_mapper size from : %zu", key_mapper_.GetMemSize());
-  key_mapper_.Resize(
-      key_mapper_.GetMemSize() - key_size);
-  LOGE("key_mapper size to : %zu", key_mapper_.GetMemSize());
-
   return true;
 }
 
 template<typename T>
-bool ShmWriter<T>::InsertHandle(const T& handle, int index, int key_index) {
-  if (!InsertKey(handle.GetKey(), key_index)) {
-    LOGE("Failed to insert key");
-    return false;
-  }
-
+bool ShmWriter<T>::InsertHandle(const T& handle, int index) {
   int handle_insert_index = PrepareHandleSpace(handle.GetDataSize());
   if (handle_insert_index < 0) {
     LOGE("Failed to prepare handle space");
@@ -310,7 +253,6 @@ bool ShmWriter<T>::InsertHandle(const T& handle, int index, int key_index) {
   }
 
   ps::HandleMappingData data {
-    .key_index = static_cast<uint32_t>(key_index),
     .index = static_cast<uint32_t>(handle_insert_index),
     .len = handle.GetDataSize(),
   };
@@ -321,7 +263,7 @@ bool ShmWriter<T>::InsertHandle(const T& handle, int index, int key_index) {
 
   WriteHandle(handle, handle_insert_index);
 
-  LOGD("inserted handle (%d, %d, %u, %s, %s)", key_index, index,
+  LOGD("inserted handle (%d, %u, %s, %s)", index,
       handle.GetDataSize(), handle.GetKey(), base_name_.c_str());
 
   return true;
@@ -342,40 +284,11 @@ bool ShmWriter<T>::InsertIndex(const ps::HandleMappingData& data,
   if (moved_num > 0)
     memmove((void*)&ptr[index + 1], (void*)&ptr[index],
         moved_num * sizeof(ps::HandleMappingData));
-
-  for (size_t i = index + 1; i < GetHandleSize(); ++i)
-    ptr[i].key_index += key_size;
   ptr[index] = data;
 
   return true;
 }
 
-template<typename T>
-bool ShmWriter<T>::InsertKey(const char* key, int index) {
-  char* ptr = reinterpret_cast<char*>(key_mapper_.GetPtr());
-  size_t key_size = strlen(key) + 1;
-  int moved_num = key_mapper_.GetMemSize() - index;
-  if (key_mapper_.Resize(key_mapper_.GetMemSize() + key_size)
-      != ps::ShmError::NONE) {
-    LOGE("Failed to resize index mapper");
-    return false;
-  }
-
-  // Resize may change real ptr
-  ptr = reinterpret_cast<char*>(key_mapper_.GetPtr());
-  if (moved_num > 0)
-    memmove((void*)(ptr + index + key_size), (void*)(ptr + index), moved_num);
-  memcpy(ptr + index, key, key_size);
-
-  return true;
-}
-
-template<typename T>
-void ShmWriter<T>::WriteKey(const char* key, size_t size, int index) {
-  uint8_t* ptr = key_mapper_.GetPtr();
-  memcpy(ptr + index, key, size);
-}
-
 template<typename T>
 void ShmWriter<T>::WriteHandle(const T& handle, int index) {
   uint8_t* ptr = handle_mapper_.GetPtr();
index f04f7834eef6566de462c0fd7c8790f936126e95..b842d558a5d525dd6a5b9fea9367037100314dcb 100644 (file)
@@ -51,10 +51,8 @@ class ShmWriter {
           config_handler_(base_name_ + "_config"),
           handle_mapper_(base_name_ + "_handle"),
           index_mapper_(base_name_ + "_indexs"),
-          key_mapper_(base_name_ + "_keys"),
           handle_version_(0),
-          index_version_(0),
-          key_version_(0) {}
+          index_version_(0) {}
   bool Init();
   bool CreateSharedMemory(const std::vector<T>& handles);
   bool Erase(const std::string& key);
@@ -67,27 +65,22 @@ class ShmWriter {
   bool WriteHandles(const std::vector<T>& handles);
   bool WriteIndexs(
       const std::vector<ps::HandleMappingData>& indexs);
-  bool RemoveIndex(size_t index, size_t key_size);
-  bool RemoveKey(int index);
-  bool InsertHandle(const T& pkg, int index, int key_index);
+  bool RemoveIndex(size_t index);
+  bool InsertHandle(const T& pkg, int index);
   bool InsertIndex(const ps::HandleMappingData& data, size_t index, size_t key_size);
-  bool InsertKey(const char* key, int index);
-  void WriteKey(const char* key, size_t size, int index);
   void WriteHandle(const T& pkg, int index);
   int PrepareHandleSpace(size_t size);
-  std::pair<int, int> GetHandleIndex(const char* pkgid) const;
-  std::pair<int, int> GetHandleInsertIndex(const char* pkgid) const;
+  int GetHandleIndex(const char* pkgid) const;
+  int GetHandleInsertIndex(const char* pkgid) const;
   size_t GetHandleSize() const;
 
   std::string base_name_;
   ps::ShmConfigHandler config_handler_;
   ps::ShmWriteMapper handle_mapper_;
   ps::ShmWriteMapper index_mapper_;
-  ps::ShmWriteMapper key_mapper_;
 
   size_t handle_version_;
   size_t index_version_;
-  size_t key_version_;
 
   std::set<std::pair<size_t, size_t>> free_space_;
 };