Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate LimitedMemoryStream buffer on demand and not upfront. #2153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sming/Core/Data/Stream/LimitedMemoryStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

uint16_t LimitedMemoryStream::readMemoryBlock(char* data, int bufSize)
{
if(buffer == nullptr) {
return 0;
}

int written = std::min(bufSize, available());
memcpy(data, buffer + readPos, written);

Expand Down Expand Up @@ -45,6 +49,11 @@ int LimitedMemoryStream::seekFrom(int offset, SeekOrigin origin)

size_t LimitedMemoryStream::write(const uint8_t* data, size_t size)
{
if(buffer == nullptr) {
buffer = new char[capacity];
owned = true;
}

auto len = std::min(capacity - writePos, size);
if(len != 0) {
memcpy(buffer + writePos, data, len);
Expand Down
5 changes: 3 additions & 2 deletions Sming/Core/Data/Stream/LimitedMemoryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class LimitedMemoryStream : public ReadWriteStream
{
}

/** @brief Constructor to allocate internal buffer for use
/** @brief Constructor to set size of internal buffer
* @param length Size of buffer
* @note The actual momory for the buffer will be allocated at the first write operation.
*/
LimitedMemoryStream(size_t length) : LimitedMemoryStream(new uint8_t[length], length, 0, true)
LimitedMemoryStream(size_t length) : LimitedMemoryStream(nullptr, length, 0, false)
{
}

Expand Down