aboutsummaryrefslogtreecommitdiffstats
path: root/src/Buffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Buffer.cpp')
-rw-r--r--src/Buffer.cpp88
1 files changed, 58 insertions, 30 deletions
diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 8631c42..3f6f296 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) 2016
+ Copyright (C) 2017
Matthias P. Braendli, matthias.braendli@mpb.li
http://opendigitalradio.org
@@ -28,48 +28,72 @@
#include "Buffer.h"
#include "PcDebug.h"
+#include <string>
#include <stdlib.h>
#include <string.h>
-#include <malloc.h>
-#if HAVE_DECL__MM_MALLOC
-# include <mm_malloc.h>
-#else
-# define memalign(a, b) malloc(b)
-#endif
-
-
Buffer::Buffer(size_t len, const void *data)
{
PDEBUG("Buffer::Buffer(%zu, %p)\n", len, data);
- this->len = 0;
- this->size = 0;
- this->data = NULL;
+ m_len = 0;
+ m_capacity = 0;
+ m_data = nullptr;
setData(data, len);
}
+Buffer::Buffer(const Buffer& other)
+{
+ setData(other.m_data, other.m_len);
+}
+
+Buffer::Buffer(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;
+}
+
Buffer::Buffer(const std::vector<uint8_t> &vec)
{
PDEBUG("Buffer::Buffer(vector [%zu])\n", vec.size());
- this->len = 0;
- this->size = 0;
- this->data = NULL;
+ m_len = 0;
+ m_capacity = 0;
+ m_data = nullptr;
setData(vec.data(), vec.size());
}
Buffer::~Buffer()
{
- PDEBUG("Buffer::~Buffer() len=%zu, data=%p\n", len, data);
- free(data);
+ PDEBUG("Buffer::~Buffer() len=%zu, data=%p\n", m_len, m_data);
+ if (m_data) {
+ free(m_data);
+ }
}
Buffer &Buffer::operator=(const Buffer &copy)
{
- setData(copy.data, copy.len);
+ setData(copy.m_data, copy.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;
+
return *this;
}
@@ -81,26 +105,30 @@ Buffer &Buffer::operator=(const std::vector<uint8_t> &copy)
Buffer &Buffer::operator+=(const Buffer &copy)
{
- appendData(copy.data, copy.len);
+ appendData(copy.m_data, copy.m_len);
return *this;
}
void Buffer::setLength(size_t len)
{
- if (len > size) {
- void *tmp = data;
+ if (len > m_capacity) {
+ void *tmp = m_data;
/* Align to 32-byte boundary for AVX. */
- data = memalign(32, len);
+ const int ret = posix_memalign(&m_data, 32, len);
+ if (ret != 0) {
+ throw std::runtime_error("memory allocation failed: " +
+ std::to_string(ret));
+ }
- if (tmp != NULL) {
- memcpy(data, tmp, this->len);
+ if (tmp != nullptr) {
+ memcpy(m_data, tmp, m_len);
free(tmp);
}
- size = len;
+ m_capacity = len;
}
- this->len = len;
+ m_len = len;
}
@@ -113,10 +141,10 @@ void Buffer::setData(const void *data, size_t len)
void Buffer::appendData(const void *data, size_t len)
{
- size_t offset = this->len;
- setLength(this->len + len);
- if (data != NULL) {
- memcpy((char*)this->data + offset, data, len);
+ size_t offset = m_len;
+ setLength(m_len + len);
+ if (data != nullptr) {
+ memcpy((char*)m_data + offset, data, len);
}
}