From 68a6e24ee87ae36edd564e159a73485c0ac71e4c Mon Sep 17 00:00:00 2001
From: Mike <mike@sillyhouse.net>
Date: Sat, 21 Oct 2023 12:28:14 +0100
Subject: [PATCH] Add MemoryDataStream::reset() method (#2677)

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.
---
 Sming/Core/Data/Stream/MemoryDataStream.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/Sming/Core/Data/Stream/MemoryDataStream.h b/Sming/Core/Data/Stream/MemoryDataStream.h
index cef511a62b..eeb9b67139 100644
--- a/Sming/Core/Data/Stream/MemoryDataStream.h
+++ b/Sming/Core/Data/Stream/MemoryDataStream.h
@@ -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;