From d567ceb9306f8250a28e0b68cc4ca9d196deef75 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 13 Sep 2014 20:51:22 +0200 Subject: Add some Buffer comments --- src/Buffer.cpp | 17 ++++------------- src/Buffer.h | 53 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 7cbbe57..dff2623 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -73,8 +73,10 @@ void Buffer::setLength(size_t len) /* Align to 32-byte boundary for AVX. */ data = memalign(32, len); - memcpy(data, tmp, this->len); - free(tmp); + if (tmp != NULL) { + memcpy(data, tmp, this->len); + free(tmp); + } size = len; } this->len = len; @@ -97,14 +99,3 @@ void Buffer::appendData(const void *data, size_t len) } } - -size_t Buffer::getLength() -{ - return len; -} - - -void *Buffer::getData() -{ - return data; -} diff --git a/src/Buffer.h b/src/Buffer.h index b1a5d93..4bb157b 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -29,28 +29,47 @@ #include - +/* Buffer is a container for a byte array, that is memcpy'ed + * on assignment and by the copy-constructor. + * + * The allocation/freeing of the data is handled internally. + */ class Buffer { -protected: - size_t len; - size_t size; - void *data; + protected: + /* Current length of the data in the Buffer */ + size_t len; -public: - Buffer(const Buffer& copy); - Buffer(size_t len = 0, const void *data = NULL); - ~Buffer(); + /* Allocated size of the Buffer */ + size_t size; - Buffer &operator=(const Buffer ©); - Buffer &operator+=(const Buffer ©); + /* Pointer to the data. Memory allocation is entirely + * handled by setLength. + */ + void *data; - void setLength(size_t len); - void setData(const void *data, size_t len); - void appendData(const void *data, size_t len); + public: + Buffer(const Buffer& copy); + Buffer(size_t len = 0, const void *data = NULL); + ~Buffer(); - size_t getLength(); - void *getData(); -}; + /* Resize the buffer, reallocate memory if needed */ + void setLength(size_t len); + + /* Replace the data in the Buffer by the new data given. + * Reallocates memory if needed. + */ + void setData(const void *data, size_t len); + Buffer &operator=(const Buffer ©); + /* 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 ©); + + size_t getLength() const { return len; } + void *getData() const { return data; } +}; #endif // BUFFER_H + -- cgit v1.2.3