aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2018-01-22 00:14:32 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2018-01-22 00:14:32 +0100
commitce7524f3d64a9d687c6786b7a3bb0d0fe76b52fa (patch)
tree3e455bcb7574f20d704aeb3ad94e166fb92ecf22
parentf24c77a1ff2dc6bf98802b61fa85030b6c8b75f5 (diff)
downloaddabmod-ce7524f3d64a9d687c6786b7a3bb0d0fe76b52fa.tar.gz
dabmod-ce7524f3d64a9d687c6786b7a3bb0d0fe76b52fa.tar.bz2
dabmod-ce7524f3d64a9d687c6786b7a3bb0d0fe76b52fa.zip
Minor Buffer improvements
-rw-r--r--src/Buffer.cpp35
-rw-r--r--src/Buffer.h21
2 files changed, 29 insertions, 27 deletions
diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 3f6f296..e96b8d6 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -3,7 +3,7 @@
Her Majesty the Queen in Right of Canada (Communications Research
Center Canada)
- Copyright (C) 2017
+ Copyright (C) 2018
Matthias P. Braendli, matthias.braendli@mpb.li
http://opendigitalradio.org
@@ -58,7 +58,7 @@ Buffer::Buffer(Buffer&& other)
other.m_data = nullptr;
}
-Buffer::Buffer(const std::vector<uint8_t> &vec)
+Buffer::Buffer(const std::vector<uint8_t>& vec)
{
PDEBUG("Buffer::Buffer(vector [%zu])\n", vec.size());
@@ -78,34 +78,38 @@ Buffer::~Buffer()
}
-Buffer &Buffer::operator=(const Buffer &copy)
+Buffer& Buffer::operator=(const Buffer& other)
{
- setData(copy.m_data, copy.m_len);
+ if (&other != this) {
+ setData(other.m_data, other.m_len);
+ }
return *this;
}
Buffer& Buffer::operator=(Buffer&& other)
{
- m_len = other.m_len;
- m_capacity = other.m_capacity;
- m_data = other.m_data;
-
- other.m_len = 0;
- other.m_capacity = 0;
- other.m_data = nullptr;
+ if (&other != this) {
+ m_len = other.m_len;
+ m_capacity = other.m_capacity;
+ m_data = other.m_data;
+
+ other.m_len = 0;
+ other.m_capacity = 0;
+ other.m_data = nullptr;
+ }
return *this;
}
-Buffer &Buffer::operator=(const std::vector<uint8_t> &copy)
+Buffer& Buffer::operator=(const std::vector<uint8_t>& buf)
{
- setData(copy.data(), copy.size());
+ setData(buf.data(), buf.size());
return *this;
}
-Buffer &Buffer::operator+=(const Buffer &copy)
+Buffer& Buffer::operator+=(const Buffer& other)
{
- appendData(copy.m_data, copy.m_len);
+ appendData(other.m_data, other.m_len);
return *this;
}
@@ -143,6 +147,7 @@ void Buffer::appendData(const void *data, size_t len)
{
size_t offset = m_len;
setLength(m_len + len);
+
if (data != nullptr) {
memcpy((char*)m_data + offset, data, len);
}
diff --git a/src/Buffer.h b/src/Buffer.h
index 88bd442..e486724 100644
--- a/src/Buffer.h
+++ b/src/Buffer.h
@@ -3,7 +3,7 @@
Her Majesty the Queen in Right of Canada (Communications Research
Center Canada)
- Copyright (C) 2017
+ Copyright (C) 2018
Matthias P. Braendli, matthias.braendli@mpb.li
http://opendigitalradio.org
@@ -35,8 +35,8 @@
#include <vector>
#include <memory>
-/* Buffer is a container for a byte array, that is memcpy'ed
- * on assignment and by the copy-constructor.
+/* Buffer is a container for a byte array, which is memory-aligned
+ * to 32 bytes for SSE performance.
*
* The allocation/freeing of the data is handled internally.
*/
@@ -45,7 +45,7 @@ class Buffer {
using sptr = std::shared_ptr<Buffer>;
Buffer(size_t len = 0, const void *data = nullptr);
- Buffer(const Buffer& copy);
+ Buffer(const Buffer& other);
Buffer(Buffer&& other);
Buffer(const std::vector<uint8_t>& vec);
~Buffer();
@@ -54,18 +54,16 @@ class Buffer {
void setLength(size_t len);
/* Replace the data in the Buffer by the new data given.
- * Reallocates memory if needed.
- */
+ * Reallocates memory if needed. */
void setData(const void *data, size_t len);
- Buffer& operator=(const Buffer& copy);
+ Buffer& operator=(const Buffer& other);
Buffer& operator=(Buffer&& other);
- Buffer& operator=(const std::vector<uint8_t>& copy);
+ Buffer& operator=(const std::vector<uint8_t>& buf);
/* Concatenate the current data with the new data given.
- * Reallocates memory if needed.
- */
+ * Reallocates memory if needed. */
void appendData(const void *data, size_t len);
- Buffer& operator+=(const Buffer& copy);
+ Buffer& operator+=(const Buffer& other);
size_t getLength() const { return m_len; }
void* getData() const { return m_data; }
@@ -80,6 +78,5 @@ class Buffer {
/* Pointer to the data. Memory allocation is entirely
* handled by setLength. */
void *m_data;
-
};