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 <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Jérôme Duval
2024-01-23 09:49:17 +00:00
committed by Adrien Destugues
parent 88af15cfbd
commit 591a1d179a
2 changed files with 39 additions and 0 deletions
@@ -62,6 +62,8 @@ public:
void SeekCompleted(); void SeekCompleted();
status_t SetBuffer(BPositionIO* buffer); status_t SetBuffer(BPositionIO* buffer);
status_t FlushBefore(off_t position);
BInputAdapter* BuildInputAdapter(); BInputAdapter* BuildInputAdapter();
protected: protected:
+37
View File
@@ -46,6 +46,28 @@ public:
return B_OK; 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) status_t EvaluatePosition(off_t position, off_t totalSize)
{ {
if (position < 0) if (position < 0)
@@ -182,6 +204,11 @@ public:
return ((flags & B_MEDIA_SEEKABLE) == B_MEDIA_SEEKABLE); return ((flags & B_MEDIA_SEEKABLE) == B_MEDIA_SEEKABLE);
} }
const BPositionIO* Buffer() const
{
return fBuffer;
}
private: private:
off_t _PositionToRelative(off_t position) const 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* BInputAdapter*
BAdapterIO::BuildInputAdapter() BAdapterIO::BuildInputAdapter()
{ {