Bug 1796811 - Update in-tree zlib to version 1.2.13. r=aosmond
[gecko.git] / ipc / glue / BigBuffer.cpp
blobab6647d8e5c841ad20ba676cf83c700c8a184715
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::AllocBuffer(size_t aSize) -> Storage {
36 if (aSize <= kShmemThreshold) {
37 return AsVariant(UniqueFreePtr<uint8_t[]>{
38 reinterpret_cast<uint8_t*>(moz_xmalloc(aSize))});
41 RefPtr<SharedMemory> shmem = new SharedMemoryBasic();
42 size_t capacity = SharedMemory::PageAlignedSize(aSize);
43 if (!shmem->Create(capacity) || !shmem->Map(capacity)) {
44 NS_ABORT_OOM(capacity);
46 return AsVariant(shmem);
49 } // namespace mozilla::ipc
51 void IPC::ParamTraits<mozilla::ipc::BigBuffer>::Write(MessageWriter* aWriter,
52 paramType&& aParam) {
53 using namespace mozilla::ipc;
54 size_t size = std::exchange(aParam.mSize, 0);
55 auto data = std::exchange(aParam.mData, BigBuffer::NoData());
57 WriteParam(aWriter, size);
58 bool isShmem = data.is<1>();
59 WriteParam(aWriter, isShmem);
61 if (isShmem) {
62 if (!data.as<1>()->WriteHandle(aWriter)) {
63 aWriter->FatalError("Failed to write data shmem");
65 } else {
66 aWriter->WriteBytes(data.as<0>().get(), size);
70 bool IPC::ParamTraits<mozilla::ipc::BigBuffer>::Read(MessageReader* aReader,
71 paramType* aResult) {
72 using namespace mozilla::ipc;
73 size_t size = 0;
74 bool isShmem = false;
75 if (!ReadParam(aReader, &size) || !ReadParam(aReader, &isShmem)) {
76 aReader->FatalError("Failed to read data size and format");
77 return false;
80 if (isShmem) {
81 RefPtr<SharedMemory> shmem = new SharedMemoryBasic();
82 size_t capacity = SharedMemory::PageAlignedSize(size);
83 if (!shmem->ReadHandle(aReader) || !shmem->Map(capacity)) {
84 aReader->FatalError("Failed to read data shmem");
85 return false;
87 *aResult = BigBuffer(BigBuffer::Adopt{}, shmem, size);
88 return true;
91 mozilla::UniqueFreePtr<uint8_t[]> buf{
92 reinterpret_cast<uint8_t*>(malloc(size))};
93 if (!buf) {
94 aReader->FatalError("Failed to allocate data buffer");
95 return false;
97 if (!aReader->ReadBytesInto(buf.get(), size)) {
98 aReader->FatalError("Failed to read data");
99 return false;
101 *aResult = BigBuffer(BigBuffer::Adopt{}, buf.release(), size);
102 return true;