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"
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 {
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!
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
)) {
48 return Some(AsVariant(shmem
));
51 } // namespace mozilla::ipc
53 void IPC::ParamTraits
<mozilla::ipc::BigBuffer
>::Write(MessageWriter
* aWriter
,
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
);
64 if (!data
.as
<1>()->WriteHandle(aWriter
)) {
65 aWriter
->FatalError("Failed to write data shmem");
68 aWriter
->WriteBytes(data
.as
<0>().get(), size
);
72 bool IPC::ParamTraits
<mozilla::ipc::BigBuffer
>::Read(MessageReader
* aReader
,
74 using namespace mozilla::ipc
;
77 if (!ReadParam(aReader
, &size
) || !ReadParam(aReader
, &isShmem
)) {
78 aReader
->FatalError("Failed to read data size and format");
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");
89 *aResult
= BigBuffer(BigBuffer::Adopt
{}, shmem
, size
);
93 mozilla::UniqueFreePtr
<uint8_t[]> buf
{
94 reinterpret_cast<uint8_t*>(malloc(size
))};
96 aReader
->FatalError("Failed to allocate data buffer");
99 if (!aReader
->ReadBytesInto(buf
.get(), size
)) {
100 aReader
->FatalError("Failed to read data");
103 *aResult
= BigBuffer(BigBuffer::Adopt
{}, buf
.release(), size
);