diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-11-19 22:09:59 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-11-19 22:09:59 +0100 |
commit | c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc (patch) | |
tree | 68230e76666b5ef2a01471fc9d6951a719856662 /ble/ByteBuffer.cpp | |
parent | 52450ed3034b0ba058ea2c9f9baa2d5f78df6a94 (diff) | |
download | ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.tar.gz ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.tar.bz2 ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.tar.xz ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.zip |
ByteBuffer:
o Reducing silliness, no allocations by ByteBuffer.
o Create StaticByteBuffer as a nice one-liner to create a buffer.
LinuxBluetooth: methods that want a buffer needs to pass it in, ByteBuffer is not allocating anymore.
Diffstat (limited to 'ble/ByteBuffer.cpp')
-rw-r--r-- | ble/ByteBuffer.cpp | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp index 1a294f1..40ba06c 100644 --- a/ble/ByteBuffer.cpp +++ b/ble/ByteBuffer.cpp @@ -7,13 +7,8 @@ #include <iostream> #include <ble/ByteBuffer.h> - using namespace std; -ByteBuffer ByteBuffer::alloc(std::size_t size) { - return {new uint8_t[size], size}; -} - ByteBuffer::ByteBuffer(uint8_t* bytes, size_t size) : zero(bytes), end_(&bytes[size]), cursor(bytes) { } @@ -47,7 +42,7 @@ ByteBuffer &ByteBuffer::write(const ByteBuffer &value) { ByteBuffer &ByteBuffer::write(const uint8_t *bytes, size_t len) { assertCanAccessRelative(len); - memcpy(cursor, bytes, len); + std::memcpy(cursor, bytes, len); cursor += len; @@ -174,7 +169,13 @@ double ByteBuffer::readFLOAT() { void ByteBuffer::copy(uint8_t *bytes, size_t length) const { assertCanAccessRelative(length - 1); - memcpy(bytes, cursor, length); + std::memcpy(bytes, cursor, length); +} + +void ByteBuffer::copy(ByteBuffer& other) const { + other.assertCanAccessRelative(getBytesLeft()); + + std::memcpy(other.cursor, cursor, getBytesLeft()); } void ByteBuffer::reset() { @@ -182,7 +183,7 @@ void ByteBuffer::reset() { } ByteBuffer ByteBuffer::view() const { -// DF << "cursor=" << getCursor() << ", size=" << getSize() << ", new size=" << end_ - cursor << ", cursor=" << (uint64_t) cursor << ", zero=" << (uint64_t) zero; +// LOG_DEBUG("cursor=" << getCursor() << ", size=" << getSize() << ", new size=" << end_ - cursor << ", cursor=" << (uint64_t) cursor << ", zero=" << (uint64_t) zero); return {cursor, getBytesLeft()}; } |