From 591a1d179a431e7ae3cc736315760df0186c3140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Sat, 13 Jan 2024 15:52:58 +0100 Subject: [PATCH] AdapterIO: add FlushBefore() to strip the beginning of the MallocIO object by default, AdapterIO is initialized with a BMallocIO object, which will be extended indefinitely. Flushing regularly is necessary to avoid excessive memory usage. Tested in StreamRadio. Change-Id: I9f3142c0a2300ad44dc54ccf6932d41c9526320b Reviewed-on: https://review.haiku-os.org/c/haiku/+/7302 Reviewed-by: Adrien Destugues Tested-by: Commit checker robot --- .../private/media/experimental/AdapterIO.h | 2 + src/kits/media/experimental/AdapterIO.cpp | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/headers/private/media/experimental/AdapterIO.h b/headers/private/media/experimental/AdapterIO.h index 81910065e3..7abaec5737 100644 --- a/headers/private/media/experimental/AdapterIO.h +++ b/headers/private/media/experimental/AdapterIO.h @@ -62,6 +62,8 @@ public: void SeekCompleted(); status_t SetBuffer(BPositionIO* buffer); + status_t FlushBefore(off_t position); + BInputAdapter* BuildInputAdapter(); protected: diff --git a/src/kits/media/experimental/AdapterIO.cpp b/src/kits/media/experimental/AdapterIO.cpp index 642a622bd5..0bc8107386 100644 --- a/src/kits/media/experimental/AdapterIO.cpp +++ b/src/kits/media/experimental/AdapterIO.cpp @@ -46,6 +46,28 @@ public: return B_OK; } + status_t FlushBefore(off_t position, BPositionIO* buffer, const void* oldBuffer, + size_t oldLength) + { + AutoWriteLocker _(fLock); + off_t relative = _PositionToRelative(position); + if (relative < 0) + return B_OK; + if (relative > (off_t)oldLength) + return B_BAD_VALUE; + status_t status = buffer->WriteAt(0, (void*)((addr_t)oldBuffer + relative), + oldLength - relative); + if (status < B_OK) + return status; + status = buffer->Seek(fBuffer->Position() - relative, SEEK_SET); + if (status < B_OK) + return status; + fBackPosition -= relative; + fStartOffset += relative; + SetBuffer(buffer); + return B_OK; + } + status_t EvaluatePosition(off_t position, off_t totalSize) { if (position < 0) @@ -182,6 +204,11 @@ public: return ((flags & B_MEDIA_SEEKABLE) == B_MEDIA_SEEKABLE); } + const BPositionIO* Buffer() const + { + return fBuffer; + } + private: off_t _PositionToRelative(off_t position) const @@ -382,6 +409,16 @@ BAdapterIO::SetBuffer(BPositionIO* buffer) } +status_t +BAdapterIO::FlushBefore(off_t position) +{ + BMallocIO* buffer = new BMallocIO(); + BMallocIO* oldBuffer = (BMallocIO*)fBuffer->Buffer(); + fBuffer->FlushBefore(position, buffer, oldBuffer->Buffer(), oldBuffer->BufferLength()); + return B_OK; +} + + BInputAdapter* BAdapterIO::BuildInputAdapter() {