From d51f890197c608e8a3eb329efbbf0777334a13a0 Mon Sep 17 00:00:00 2001 From: Igor Pissolati Date: Sun, 4 Jan 2026 22:45:41 -0300 Subject: [PATCH] Use vector instead of large array --- src/emulator/serialization.hpp | 49 ++++++++++++++++++++++ src/windows-emulator/user_handle_table.hpp | 7 ++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/emulator/serialization.hpp b/src/emulator/serialization.hpp index 29df1b4e..caf502cb 100644 --- a/src/emulator/serialization.hpp +++ b/src/emulator/serialization.hpp @@ -160,6 +160,36 @@ namespace utils this->write_span(std::span(vec)); } + void write_vector(const std::vector& vec) + { + this->write(static_cast(vec.size())); + + uint8_t byte = 0; + uint8_t bit_index = 0; + + for (const bool b : vec) + { + if (b) + { + byte |= (1u << bit_index); + } + + ++bit_index; + + if (bit_index == 8) + { + this->write(byte); + byte = 0; + bit_index = 0; + } + } + + if (bit_index != 0) + { + this->write(byte); + } + } + template void write_list(const std::list& vec) { @@ -385,6 +415,25 @@ namespace utils } } + void read_vector(std::vector& result) + { + const auto bit_count = this->read(); + result.clear(); + result.reserve(static_cast(bit_count)); + + const auto size = (bit_count + 7) / 8; + + for (uint64_t i = 0; i < size; ++i) + { + const auto byte = this->read(); + + for (uint8_t bit = 0; bit < 8 && result.size() < bit_count; ++bit) + { + result.push_back((byte >> bit) & 1u); + } + } + } + template std::vector read_vector() { diff --git a/src/windows-emulator/user_handle_table.hpp b/src/windows-emulator/user_handle_table.hpp index d03cd22d..68780919 100644 --- a/src/windows-emulator/user_handle_table.hpp +++ b/src/windows-emulator/user_handle_table.hpp @@ -10,6 +10,7 @@ class user_handle_table void setup(memory_manager& memory) { memory_ = &memory; + used_indices_.resize(MAX_HANDLES, false); const auto server_info_size = static_cast(page_align_up(sizeof(USER_SERVERINFO))); server_info_addr_ = memory.allocate_memory(server_info_size, memory_permission::read); @@ -87,7 +88,7 @@ class user_handle_table buffer.write(server_info_addr_); buffer.write(handle_table_addr_); buffer.write(display_info_addr_); - buffer.write(used_indices_); + buffer.write_vector(used_indices_); } void deserialize(utils::buffer_deserializer& buffer) @@ -95,7 +96,7 @@ class user_handle_table buffer.read(server_info_addr_); buffer.read(handle_table_addr_); buffer.read(display_info_addr_); - buffer.read(used_indices_); + buffer.read_vector(used_indices_); } private: @@ -127,7 +128,7 @@ class user_handle_table uint64_t server_info_addr_{}; uint64_t handle_table_addr_{}; uint64_t display_info_addr_{}; - std::array used_indices_{}; + std::vector used_indices_{}; memory_manager* memory_{}; };