summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2023-04-19 21:00:35 +0200
committerJoel Rosdahl <joel@rosdahl.net>2023-04-19 22:00:14 +0200
commit8a72cde9ad9ac78b013e2d2ff7657637b4d08f46 (patch)
tree4b793b9bfafe8cead592a47cd551b969059b86d2
parentc2b49eb057f071e3c29750a51ddeb805936d3bb3 (diff)
downloadccache-8a72cde9ad9ac78b013e2d2ff7657637b4d08f46.tar.gz
refactor: Use std::unique_ptr for data in util::Bytes
-rw-r--r--src/util/Bytes.cpp61
-rw-r--r--src/util/Bytes.hpp34
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<uint8_t[]>(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<uint8_t[]>(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<uint8_t[]>(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<uint8_t[]>(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<uint8_t[]>(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 <cstdint>
#include <cstring>
#include <initializer_list>
+#include <memory>
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<uint8_t[]> 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<uint8_t[]>(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<uint8_t[]>(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<const uint8_t> data) noexcept
@@ -115,10 +116,7 @@ inline Bytes::Bytes(std::initializer_list<uint8_t> 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