Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit efa12510 authored by Marco Nelissen's avatar Marco Nelissen
Browse files

Fix bug in MediaBufferGroup new buffer allocation

MediaBufferGroup could allocate buffers of size 0, but this
was masked because we pre-allocated many more buffers. Now
that we allocate fewer buffers upfront, it's more likely
to hit this case.

Bug: 29125703
Test: CTS

Change-Id: Iba666f161be6d7c8bf09e1b48b1fd80750685a57
parent fd1da15b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -186,6 +186,9 @@ public:
        ret = reply.readInt32();
        ALOGV("readMultiple status %d, bufferCount %u, sinceStop %u",
                ret, bufferCount, mBuffersSinceStop);
        if (bufferCount && ret == WOULD_BLOCK) {
            ret = OK;
        }
        return ret;
    }

+7 −2
Original line number Diff line number Diff line
@@ -157,11 +157,15 @@ status_t MediaBufferGroup::acquire_buffer(
    Mutex::Autolock autoLock(mInternal->mLock);
    for (;;) {
        size_t smallest = requestedSize;
        size_t biggest = requestedSize;
        MediaBufferBase *buffer = nullptr;
        auto free = mInternal->mBuffers.end();
        for (auto it = mInternal->mBuffers.begin(); it != mInternal->mBuffers.end(); ++it) {
            if ((*it)->refcount() == 0) {
            const size_t size = (*it)->size();
            if (size > biggest) {
                biggest = size;
            }
            if ((*it)->refcount() == 0) {
                if (size >= requestedSize) {
                    buffer = *it;
                    break;
@@ -176,7 +180,8 @@ status_t MediaBufferGroup::acquire_buffer(
                && (free != mInternal->mBuffers.end()
                    || mInternal->mBuffers.size() < mInternal->mGrowthLimit)) {
            // We alloc before we free so failure leaves group unchanged.
            const size_t allocateSize = requestedSize < SIZE_MAX / 3 * 2 /* NB: ordering */ ?
            const size_t allocateSize = requestedSize == 0 ? biggest :
                    requestedSize < SIZE_MAX / 3 * 2 /* NB: ordering */ ?
                    requestedSize * 3 / 2 : requestedSize;
            buffer = new MediaBuffer(allocateSize);
            if (buffer->data() == nullptr) {