diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-12-21 07:59:30 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-12-21 07:59:30 +0100 |
commit | 7da0cf21faf31888f8412a61ae4a12abe01a827f (patch) | |
tree | 0a789efd38a2bc5b559cfc7af95f021423d7ba7f /src | |
parent | 0cceefe1774263690dce352cf41c3fff3bf85135 (diff) | |
download | dabmod-7da0cf21faf31888f8412a61ae4a12abe01a827f.tar.gz dabmod-7da0cf21faf31888f8412a61ae4a12abe01a827f.tar.bz2 dabmod-7da0cf21faf31888f8412a61ae4a12abe01a827f.zip |
Make Buffer movable
Diffstat (limited to 'src')
-rw-r--r-- | src/Buffer.cpp | 51 | ||||
-rw-r--r-- | src/Buffer.h | 42 |
2 files changed, 63 insertions, 30 deletions
diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 9df834a..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 @@ -37,18 +37,34 @@ Buffer::Buffer(size_t len, const void *data) PDEBUG("Buffer::Buffer(%zu, %p)\n", len, data); m_len = 0; - m_size = 0; - m_data = NULL; + 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()); m_len = 0; - m_size = 0; - m_data = NULL; + m_capacity = 0; + m_data = nullptr; setData(vec.data(), vec.size()); } @@ -56,7 +72,9 @@ Buffer::Buffer(const std::vector<uint8_t> &vec) Buffer::~Buffer() { PDEBUG("Buffer::~Buffer() len=%zu, data=%p\n", m_len, m_data); - free(m_data); + if (m_data) { + free(m_data); + } } @@ -66,6 +84,19 @@ Buffer &Buffer::operator=(const Buffer ©) 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; +} + Buffer &Buffer::operator=(const std::vector<uint8_t> ©) { setData(copy.data(), copy.size()); @@ -81,7 +112,7 @@ Buffer &Buffer::operator+=(const Buffer ©) void Buffer::setLength(size_t len) { - if (len > m_size) { + if (len > m_capacity) { void *tmp = m_data; /* Align to 32-byte boundary for AVX. */ @@ -91,11 +122,11 @@ void Buffer::setLength(size_t len) std::to_string(ret)); } - if (tmp != NULL) { + if (tmp != nullptr) { memcpy(m_data, tmp, m_len); free(tmp); } - m_size = len; + m_capacity = len; } m_len = len; } @@ -112,7 +143,7 @@ void Buffer::appendData(const void *data, size_t len) { size_t offset = m_len; setLength(m_len + len); - if (data != NULL) { + if (data != nullptr) { memcpy((char*)m_data + offset, data, len); } } diff --git a/src/Buffer.h b/src/Buffer.h index 60a7d50..88bd442 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) 2016 + Copyright (C) 2017 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -41,24 +41,13 @@ * The allocation/freeing of the data is handled internally. */ class Buffer { - protected: - /* Current length of the data in the Buffer */ - size_t m_len; - - /* Allocated size of the Buffer */ - size_t m_size; - - /* Pointer to the data. Memory allocation is entirely - * handled by setLength. - */ - void *m_data; - public: using sptr = std::shared_ptr<Buffer>; - Buffer(const Buffer& copy) = default; - Buffer(const std::vector<uint8_t> &vec); - Buffer(size_t len = 0, const void *data = NULL); + Buffer(size_t len = 0, const void *data = nullptr); + Buffer(const Buffer& copy); + Buffer(Buffer&& other); + Buffer(const std::vector<uint8_t>& vec); ~Buffer(); /* Resize the buffer, reallocate memory if needed */ @@ -68,16 +57,29 @@ class Buffer { * Reallocates memory if needed. */ void setData(const void *data, size_t len); - Buffer &operator=(const Buffer ©); - Buffer &operator=(const std::vector<uint8_t> ©); + Buffer& operator=(const Buffer& copy); + Buffer& operator=(Buffer&& other); + Buffer& operator=(const std::vector<uint8_t>& copy); /* Concatenate the current data with the new data given. * Reallocates memory if needed. */ void appendData(const void *data, size_t len); - Buffer &operator+=(const Buffer ©); + Buffer& operator+=(const Buffer& copy); size_t getLength() const { return m_len; } - void *getData() const { return m_data; } + void* getData() const { return m_data; } + + private: + /* Current length of the data in the Buffer */ + size_t m_len; + + /* Allocated size of the Buffer */ + size_t m_capacity; + + /* Pointer to the data. Memory allocation is entirely + * handled by setLength. */ + void *m_data; + }; |