From 8a72cde9ad9ac78b013e2d2ff7657637b4d08f46 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 19 Apr 2023 21:00:35 +0200 Subject: refactor: Use std::unique_ptr for data in util::Bytes --- src/util/Bytes.cpp | 61 +++++++++++++++++++++++++----------------------------- src/util/Bytes.hpp | 34 ++++++++++++++---------------- 2 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/util/Bytes.cpp b/src/util/Bytes.cpp index f06eeee1..1cde9739 100644 --- a/src/util/Bytes.cpp +++ b/src/util/Bytes.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Joel Rosdahl and other contributors +// Copyright (C) 2022-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -23,22 +23,20 @@ namespace util { Bytes::Bytes(const Bytes& other) noexcept - : m_size(other.m_size), + : m_data(std::make_unique(other.m_size)), + m_size(other.m_size), m_capacity(other.m_size) { - delete[] m_data; - m_data = new uint8_t[m_size]; if (m_size > 0) { - std::memcpy(m_data, other.m_data, m_size); + std::memcpy(m_data.get(), other.m_data.get(), m_size); } } Bytes::Bytes(Bytes&& other) noexcept + : m_data(std::move(other.m_data)), + m_size(other.m_size), + m_capacity(other.m_capacity) { - delete[] m_data; - m_data = other.m_data; - m_size = other.m_size; - m_capacity = other.m_capacity; other.m_data = nullptr; other.m_size = 0; other.m_capacity = 0; @@ -50,12 +48,11 @@ Bytes::operator=(const Bytes& other) noexcept if (&other == this) { return *this; } - delete[] m_data; - m_data = new uint8_t[other.m_size]; + m_data = std::make_unique(other.m_size); m_size = other.m_size; m_capacity = other.m_size; if (m_size > 0) { - std::memcpy(m_data, other.m_data, m_size); + std::memcpy(m_data.get(), other.m_data.get(), m_size); } return *this; } @@ -66,11 +63,10 @@ Bytes::operator=(Bytes&& other) noexcept if (&other == this) { return *this; } - delete[] m_data; - m_data = other.m_data; + m_data = std::move(other.m_data); m_size = other.m_size; m_capacity = other.m_capacity; - other.m_data = nullptr; + other.m_data.reset(); other.m_size = 0; other.m_capacity = 0; return *this; @@ -80,12 +76,11 @@ void Bytes::reserve(size_t size) noexcept { if (size > m_capacity) { - uint8_t* data = new uint8_t[size]; + auto data = std::make_unique(size); if (m_size > 0) { - std::memcpy(data, m_data, m_size); + std::memcpy(data.get(), m_data.get(), m_size); } - delete[] m_data; - m_data = data; + m_data = std::move(data); m_capacity = size; } } @@ -99,24 +94,25 @@ Bytes::insert(const uint8_t* pos, if (inserted_size == 0) { return; } - const size_t offset = pos - m_data; + 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); - uint8_t* new_data = new uint8_t[m_capacity]; + auto new_data = std::make_unique(m_capacity); if (offset > 0) { - std::memcpy(new_data, m_data, offset); + std::memcpy(new_data.get(), m_data.get(), offset); } if (m_size > offset) { - std::memcpy( - new_data + offset + inserted_size, m_data + offset, m_size - offset); + std::memcpy(new_data.get() + offset + inserted_size, + m_data.get() + offset, + m_size - offset); } - delete[] m_data; - m_data = new_data; + m_data = std::move(new_data); } else if (m_size > offset) { - std::memmove( - m_data + offset + inserted_size, m_data + offset, m_size - offset); + std::memmove(m_data.get() + offset + inserted_size, + m_data.get() + offset, + m_size - offset); } - std::memcpy(m_data + offset, first, inserted_size); + std::memcpy(m_data.get() + offset, first, inserted_size); m_size += inserted_size; } @@ -124,12 +120,11 @@ void Bytes::resize(size_t size) noexcept { if (size > m_capacity) { - uint8_t* new_data = new uint8_t[size]; + auto new_data = std::make_unique(size); if (m_size > 0) { - std::memcpy(new_data, m_data, m_size); + std::memcpy(new_data.get(), m_data.get(), m_size); } - delete[] m_data; - m_data = new_data; + m_data = std::move(new_data); m_capacity = size; } m_size = size; diff --git a/src/util/Bytes.hpp b/src/util/Bytes.hpp index ce9a6a2b..3d328d40 100644 --- a/src/util/Bytes.hpp +++ b/src/util/Bytes.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Joel Rosdahl and other contributors +// Copyright (C) 2022-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -23,6 +23,7 @@ #include #include #include +#include namespace util { @@ -85,24 +86,24 @@ public: void insert(const uint8_t* pos, const char* data, size_t size) noexcept; private: - uint8_t* m_data = nullptr; + std::unique_ptr m_data; size_t m_size = 0; size_t m_capacity = 0; }; inline Bytes::Bytes(size_t size) noexcept - : m_data(new uint8_t[size]), + : m_data(std::make_unique(size)), m_size(size), m_capacity(size) { } inline Bytes::Bytes(const void* data, size_t size) noexcept - : m_data(new uint8_t[size]), + : m_data(std::make_unique(size)), m_size(size), m_capacity(size) { - std::memcpy(m_data, data, size); + std::memcpy(m_data.get(), data, size); } inline Bytes::Bytes(nonstd::span data) noexcept @@ -115,10 +116,7 @@ inline Bytes::Bytes(std::initializer_list init) noexcept { } -inline Bytes::~Bytes() noexcept -{ - delete[] m_data; -} +inline Bytes::~Bytes() noexcept = default; inline uint8_t Bytes::operator[](size_t pos) const noexcept @@ -137,7 +135,7 @@ Bytes::operator==(const Bytes& other) const noexcept { return this == &other || (m_size == other.m_size - && std::memcmp(m_data, other.m_data, m_size) == 0); + && std::memcmp(m_data.get(), other.m_data.get(), m_size) == 0); } inline bool @@ -149,49 +147,49 @@ Bytes::operator!=(const Bytes& other) const noexcept inline const uint8_t* Bytes::data() const noexcept { - return m_data; + return m_data.get(); } inline uint8_t* Bytes::data() noexcept { - return m_data; + return m_data.get(); } inline uint8_t* Bytes::begin() noexcept { - return m_data; + return m_data.get(); } inline const uint8_t* Bytes::begin() const noexcept { - return m_data; + return m_data.get(); } inline const uint8_t* Bytes::cbegin() const noexcept { - return m_data; + return m_data.get(); } inline uint8_t* Bytes::end() noexcept { - return m_data + m_size; + return m_data.get() + m_size; } inline const uint8_t* Bytes::end() const noexcept { - return m_data + m_size; + return m_data.get() + m_size; } inline const uint8_t* Bytes::cend() const noexcept { - return m_data + m_size; + return m_data.get() + m_size; } inline bool -- cgit v1.2.1