Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / ipc / glue / BigBuffer.cpp
blob822ffe20fe7024bd3085e34684517e611bbfea40
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/ipc/BigBuffer.h"
9 #include "mozilla/ipc/SharedMemoryBasic.h"
10 #include "nsDebug.h"
12 namespace mozilla::ipc {
14 BigBuffer::BigBuffer(Adopt, SharedMemory* aSharedMemory, size_t aSize)
15 : mSize(aSize), mData(AsVariant(RefPtr{aSharedMemory})) {
16 MOZ_RELEASE_ASSERT(aSharedMemory && aSharedMemory->memory(),
17 "shared memory must be non-null and mapped");
18 MOZ_RELEASE_ASSERT(mSize <= aSharedMemory->Size(),
19 "shared memory region isn't large enough");
22 BigBuffer::BigBuffer(Adopt, uint8_t* aData, size_t aSize)
23 : mSize(aSize), mData(AsVariant(UniqueFreePtr<uint8_t[]>{aData})) {}
25 uint8_t* BigBuffer::Data() {
26 return mData.is<0>() ? mData.as<0>().get()
27 : reinterpret_cast<uint8_t*>(mData.as<1>()->memory());
29 const uint8_t* BigBuffer::Data() const {
30 return mData.is<0>()
31 ? mData.as<0>().get()
32 : reinterpret_cast<const uint8_t*>(mData.as<1>()->memory());
35 auto BigBuffer::TryAllocBuffer(size_t aSize) -> Maybe<Storage> {
36 if (aSize <= kShmemThreshold) {
37 auto mem = UniqueFreePtr<uint8_t[]>{
38 reinterpret_cast<uint8_t*>(malloc(aSize))}; // Fallible!
39 if (!mem) return {};
40 return Some(AsVariant(std::move(mem)));
43 RefPtr<SharedMemory> shmem = new SharedMemoryBasic();
44 size_t capacity = SharedMemory::PageAlignedSize(aSize);
45 if (!shmem->Create(capacity) || !shmem->Map(capacity)) {
46 return {};
48 return Some(AsVariant(shmem));
51 } // namespace mozilla::ipc
53 void IPC::ParamTraits<mozilla::ipc::BigBuffer>::Write(MessageWriter* aWriter,
54 paramType&& aParam) {
55 using namespace mozilla::ipc;
56 size_t size = std::exchange(aParam.mSize, 0);
57 auto data = std::exchange(aParam.mData, BigBuffer::NoData());
59 WriteParam(aWriter, size);
60 bool isShmem = data.is<1>();
61 WriteParam(aWriter, isShmem);
63 if (isShmem) {
64 if (!data.as<1>()->WriteHandle(aWriter)) {
65 aWriter->FatalError("Failed to write data shmem");
67 } else {
68 aWriter->WriteBytes(data.as<0>().get(), size);
72 bool IPC::ParamTraits<mozilla::ipc::BigBuffer>::Read(MessageReader* aReader,
73 paramType* aResult) {
74 using namespace mozilla::ipc;
75 size_t size = 0;
76 bool isShmem = false;
77 if (!ReadParam(aReader, &size) || !ReadParam(aReader, &isShmem)) {
78 aReader->FatalError("Failed to read data size and format");
79 return false;
82 if (isShmem) {
83 RefPtr<SharedMemory> shmem = new SharedMemoryBasic();
84 size_t capacity = SharedMemory::PageAlignedSize(size);
85 if (!shmem->ReadHandle(aReader) || !shmem->Map(capacity)) {
86 aReader->FatalError("Failed to read data shmem");
87 return false;
89 *aResult = BigBuffer(BigBuffer::Adopt{}, shmem, size);
90 return true;
93 mozilla::UniqueFreePtr<uint8_t[]> buf{
94 reinterpret_cast<uint8_t*>(malloc(size))};
95 if (!buf) {
96 aReader->FatalError("Failed to allocate data buffer");
97 return false;
99 if (!aReader->ReadBytesInto(buf.get(), size)) {
100 aReader->FatalError("Failed to read data");
101 return false;
103 *aResult = BigBuffer(BigBuffer::Adopt{}, buf.release(), size);
104 return true;