Skip to content

Commit

Permalink
Add MemoryDataStream::reset() method (#2677)
Browse files Browse the repository at this point in the history
The class already has a `clear()` method but this leaves existing memory allocated. The purpose is to allow re-use of an existing class instance efficiently by reducing memory re-allocations.

The new `reset()` method allows the instance to be cleared and also release allocated memory. The new name is consistent with `std::unique_ptr::reset()` usage.

A similar thing can be accomplished by using `std::unique_ptr<MemoryDataStream>` however an in-built reset() method makes for cleaner code.
  • Loading branch information
mikee47 authored Oct 21, 2023
1 parent d85d10c commit 68a6e24
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sming/Core/Data/Stream/MemoryDataStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ class MemoryDataStream : public ReadWriteStream
readPos = 0;
}

/**
* @brief Clear stream and release allocated memory
*/
void reset()
{
clear();
free(buffer);
buffer = nullptr;
capacity = 0;
}

size_t getSize() const
{
return size;
Expand Down

0 comments on commit 68a6e24

Please sign in to comment.