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;
}
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) {
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();
}
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;
}
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>
handle_version_ = 0;
index_version_ = 0;
- key_version_ = 0;
config_handler_.SetShmStatus(ps::ShmStatus::INVALID);
if (config_handler_.Clear() != ps::ShmError::NONE)
ret = false;
if (index_mapper_.Clear() != ps::ShmError::NONE)
ret = false;
- if (key_mapper_.Clear() != ps::ShmError::NONE)
- ret = false;
return ret;
}
}
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 {
}
}
- 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();
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");
}
ps::HandleMappingData data {
- .key_index = static_cast<uint32_t>(key_index),
.index = static_cast<uint32_t>(handle_insert_index),
.len = handle.GetDataSize(),
};
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;
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();