]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use std::unique_ptr for data in util::Bytes
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 19 Apr 2023 19:00:35 +0000 (21:00 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 19 Apr 2023 20:00:14 +0000 (22:00 +0200)
src/util/Bytes.cpp
src/util/Bytes.hpp

index f06eeee191d0191541211ca70ba09e587d0b9b92..1cde9739e69850c648bd0d9d8bc436a92a242171 100644 (file)
@@ -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.
 //
 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;
index ce9a6a2b35a35655d0fae439a43ed99c7df1347a..3d328d40c306a4c1e4ac42766969d3528939fa81 100644 (file)
@@ -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