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 <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
This commit is contained in:
Jérôme Duval 2024-01-13 15:52:58 +01:00 committed by Adrien Destugues
parent 88af15cfbd
commit 591a1d179a
2 changed files with 39 additions and 0 deletions

View File

@ -62,6 +62,8 @@ public:
void SeekCompleted();
status_t SetBuffer(BPositionIO* buffer);
status_t FlushBefore(off_t position);
BInputAdapter* BuildInputAdapter();
protected:

View File

@ -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()
{