From: Joel Rosdahl Date: Sun, 9 Nov 2025 12:49:22 +0000 (+0100) Subject: refactor: Improve Bytes::{erase,insert} signatures X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=509a20dac2b3073676c45651d4a4f7aaf381503e;p=thirdparty%2Fccache.git refactor: Improve Bytes::{erase,insert} signatures --- diff --git a/src/ccache/util/bytes.cpp b/src/ccache/util/bytes.cpp index e4f54630..66d1e9fc 100644 --- a/src/ccache/util/bytes.cpp +++ b/src/ccache/util/bytes.cpp @@ -127,35 +127,30 @@ Bytes::reserve(size_t size) noexcept } void -Bytes::insert(const uint8_t* pos, - const uint8_t* first, - const uint8_t* last) noexcept +Bytes::insert(const void* pos, const void* data, size_t size) noexcept { - const size_t inserted_size = last - first; - if (inserted_size == 0) { + if (size == 0) { return; } - const size_t offset = pos - m_data.get(); - if (m_size + inserted_size > m_capacity) { - m_capacity = std::max(2 * m_capacity, m_size + inserted_size); + const size_t offset = reinterpret_cast(pos) - m_data.get(); + if (m_size + size > m_capacity) { + m_capacity = std::max(2 * m_capacity, m_size + size); // In C++20, use std::make_unique_for_overwrite instead. auto new_data = std::unique_ptr(new uint8_t[m_capacity]); if (offset > 0) { std::memcpy(new_data.get(), m_data.get(), offset); } if (m_size > offset) { - std::memcpy(new_data.get() + offset + inserted_size, - m_data.get() + offset, - m_size - offset); + std::memcpy( + new_data.get() + offset + size, m_data.get() + offset, m_size - offset); } m_data = std::move(new_data); } else if (m_size > offset) { - std::memmove(m_data.get() + offset + inserted_size, - m_data.get() + offset, - m_size - offset); + std::memmove( + m_data.get() + offset + size, m_data.get() + offset, m_size - offset); } - std::memcpy(m_data.get() + offset, first, inserted_size); - m_size += inserted_size; + std::memcpy(m_data.get() + offset, data, size); + m_size += size; } void @@ -176,12 +171,12 @@ Bytes::resize(size_t size) noexcept } void -Bytes::erase(const uint8_t* pos, const size_t size) noexcept +Bytes::erase(const void* pos, const size_t size) noexcept { if (size == 0) { return; } - const size_t offset = pos - m_data.get(); + const size_t offset = reinterpret_cast(pos) - m_data.get(); if (offset + size < m_size) { std::memmove(m_data.get() + offset, m_data.get() + offset + size, @@ -190,10 +185,4 @@ Bytes::erase(const uint8_t* pos, const size_t size) noexcept m_size -= size; } -void -Bytes::erase(const uint8_t* first, const uint8_t* last) noexcept -{ - erase(first, last - first); -} - } // namespace util diff --git a/src/ccache/util/bytes.hpp b/src/ccache/util/bytes.hpp index 2780de07..ba71cad0 100644 --- a/src/ccache/util/bytes.hpp +++ b/src/ccache/util/bytes.hpp @@ -87,17 +87,12 @@ public: void push_back(uint8_t value) noexcept; - void insert(const uint8_t* pos, - const uint8_t* first, - const uint8_t* last) noexcept; - void - insert(const uint8_t* pos, const uint8_t* data, const size_t size) noexcept; - void insert(const uint8_t* pos, const char* first, const char* last) noexcept; - void insert(const uint8_t* pos, const char* data, size_t size) noexcept; - void insert(const uint8_t* pos, nonstd::span data) noexcept; - - void erase(const uint8_t* pos, const size_t size) noexcept; - void erase(const uint8_t* first, const uint8_t* last) noexcept; + void insert(const void* pos, const void* first, const void* last) noexcept; + void insert(const void* pos, const void* data, size_t size) noexcept; + void insert(const void* pos, nonstd::span data) noexcept; + + void erase(const void* pos, size_t size) noexcept; + void erase(const void* first, const void* last) noexcept; private: std::unique_ptr m_data; @@ -241,31 +236,26 @@ Bytes::clear() noexcept } inline void -Bytes::insert(const uint8_t* pos, - const uint8_t* data, - const size_t size) noexcept -{ - return insert(pos, data, data + size); -} - -inline void -Bytes::insert(const uint8_t* pos, const char* first, const char* last) noexcept +Bytes::insert(const void* pos, const void* first, const void* last) noexcept { return insert(pos, - reinterpret_cast(first), - reinterpret_cast(last)); + first, + reinterpret_cast(last) + - reinterpret_cast(first)); } inline void -Bytes::insert(const uint8_t* pos, const char* data, size_t size) noexcept +Bytes::insert(const void* pos, nonstd::span data) noexcept { - return insert(pos, data, data + size); + return insert(pos, &*data.begin(), data.size()); } inline void -Bytes::insert(const uint8_t* pos, nonstd::span data) noexcept +Bytes::erase(const void* first, const void* last) noexcept { - return insert(pos, &*data.begin(), &*data.end()); + erase(first, + reinterpret_cast(last) + - reinterpret_cast(first)); } } // namespace util