2014-07-02 06:26:35 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GMPSharedMemManager.h"
|
|
|
|
#include "GMPMessageUtils.h"
|
|
|
|
#include "mozilla/ipc/SharedMemory.h"
|
2014-07-11 07:35:28 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2014-07-02 06:26:35 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
|
|
|
|
|
|
|
// Really one set of pools on each side of the plugin API.
|
|
|
|
|
|
|
|
// YUV buffers go from Encoder parent to child; pool there, and then return
|
|
|
|
// with Decoded() frames to the Decoder parent and goes into the parent pool.
|
|
|
|
// Compressed (encoded) data goes from the Decoder parent to the child;
|
|
|
|
// pool there, and then return with Encoded() frames and goes into the parent
|
|
|
|
// pool.
|
|
|
|
bool
|
2014-07-28 19:44:20 +04:00
|
|
|
GMPSharedMemManager::MgrAllocShmem(GMPSharedMem::GMPMemoryClasses aClass, size_t aSize,
|
2014-07-02 06:26:35 +04:00
|
|
|
ipc::Shmem::SharedMemory::SharedMemoryType aType,
|
|
|
|
ipc::Shmem* aMem)
|
|
|
|
{
|
2014-07-28 19:44:20 +04:00
|
|
|
mData->CheckThread();
|
2014-07-02 06:26:35 +04:00
|
|
|
|
|
|
|
// first look to see if we have a free buffer large enough
|
2014-07-11 07:35:28 +04:00
|
|
|
for (uint32_t i = 0; i < GetGmpFreelist(aClass).Length(); i++) {
|
|
|
|
MOZ_ASSERT(GetGmpFreelist(aClass)[i].IsWritable());
|
|
|
|
if (aSize <= GetGmpFreelist(aClass)[i].Size<uint8_t>()) {
|
|
|
|
*aMem = GetGmpFreelist(aClass)[i];
|
|
|
|
GetGmpFreelist(aClass).RemoveElementAt(i);
|
2014-07-02 06:26:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find a buffer free with enough space; allocate one
|
|
|
|
size_t pagesize = ipc::SharedMemory::SystemPageSize();
|
|
|
|
aSize = (aSize + (pagesize-1)) & ~(pagesize-1); // round up to page size
|
|
|
|
bool retval = Alloc(aSize, aType, aMem);
|
2014-10-02 04:39:04 +04:00
|
|
|
// The allocator (or NeedsShmem call) should never return less than we ask for...
|
|
|
|
MOZ_ASSERT(aMem->Size<uint8_t>() >= aSize);
|
2014-07-02 06:26:35 +04:00
|
|
|
if (retval) {
|
2014-07-28 19:44:20 +04:00
|
|
|
mData->mGmpAllocated[aClass]++;
|
2014-07-02 06:26:35 +04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-07-28 19:44:20 +04:00
|
|
|
GMPSharedMemManager::MgrDeallocShmem(GMPSharedMem::GMPMemoryClasses aClass, ipc::Shmem& aMem)
|
2014-07-02 06:26:35 +04:00
|
|
|
{
|
2014-07-28 19:44:20 +04:00
|
|
|
mData->CheckThread();
|
2014-07-02 06:26:35 +04:00
|
|
|
|
|
|
|
size_t size = aMem.Size<uint8_t>();
|
|
|
|
size_t total = 0;
|
2014-10-02 04:39:04 +04:00
|
|
|
|
|
|
|
// XXX Bug NNNNNNN Until we put better guards on ipc::shmem, verify we
|
|
|
|
// weren't fed an shmem we already had.
|
|
|
|
for (uint32_t i = 0; i < GetGmpFreelist(aClass).Length(); i++) {
|
|
|
|
if (NS_WARN_IF(aMem == GetGmpFreelist(aClass)[i])) {
|
|
|
|
// Safest to crash in this case; should never happen in normal
|
|
|
|
// operation.
|
|
|
|
MOZ_CRASH("Deallocating Shmem we already have in our cache!");
|
|
|
|
//return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:26:35 +04:00
|
|
|
// XXX This works; there are better pool algorithms. We need to avoid
|
|
|
|
// "falling off a cliff" with too low a number
|
2014-07-11 07:35:28 +04:00
|
|
|
if (GetGmpFreelist(aClass).Length() > 10) {
|
|
|
|
Dealloc(GetGmpFreelist(aClass)[0]);
|
|
|
|
GetGmpFreelist(aClass).RemoveElementAt(0);
|
2014-07-02 06:26:35 +04:00
|
|
|
// The allocation numbers will be fubar on the Child!
|
2014-07-28 19:44:20 +04:00
|
|
|
mData->mGmpAllocated[aClass]--;
|
2014-07-02 06:26:35 +04:00
|
|
|
}
|
2014-07-11 07:35:28 +04:00
|
|
|
for (uint32_t i = 0; i < GetGmpFreelist(aClass).Length(); i++) {
|
|
|
|
MOZ_ASSERT(GetGmpFreelist(aClass)[i].IsWritable());
|
|
|
|
total += GetGmpFreelist(aClass)[i].Size<uint8_t>();
|
|
|
|
if (size < GetGmpFreelist(aClass)[i].Size<uint8_t>()) {
|
|
|
|
GetGmpFreelist(aClass).InsertElementAt(i, aMem);
|
2014-07-02 06:26:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 07:35:28 +04:00
|
|
|
GetGmpFreelist(aClass).AppendElement(aMem);
|
2014-07-02 06:26:35 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-07-28 19:44:20 +04:00
|
|
|
GMPSharedMemManager::NumInUse(GMPSharedMem::GMPMemoryClasses aClass)
|
2014-07-02 06:26:35 +04:00
|
|
|
{
|
2014-07-28 19:44:20 +04:00
|
|
|
return mData->mGmpAllocated[aClass] - GetGmpFreelist(aClass).Length();
|
2014-07-02 06:26:35 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gmp
|
|
|
|
} // namespace mozilla
|