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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* A type that can be sent without needing to make a copy during
8 * serialization. In addition the receiver can take ownership of the
9 * data to avoid having to make an additional copy. */
11 #ifndef mozilla_ipc_ByteBufUtils_h
12 #define mozilla_ipc_ByteBufUtils_h
14 #include "mozilla/ipc/ByteBuf.h"
15 #include "mozilla/CheckedInt.h"
16 #include "mozilla/mozalloc_oom.h"
17 #include "ipc/IPCMessageUtils.h"
22 struct ParamTraits
<mozilla::ipc::ByteBuf
> {
23 typedef mozilla::ipc::ByteBuf paramType
;
25 // this is where we transfer the memory from the ByteBuf to IPDL, avoiding a
27 static void Write(MessageWriter
* aWriter
, paramType
&& aParam
) {
28 // We need to send the length as a 32-bit value, not a size_t, because on
29 // ARM64 Windows we end up with a 32-bit GMP process sending a ByteBuf to
30 // a 64-bit parent process. WriteBytesZeroCopy takes a uint32_t as an
31 // argument, so it would end up getting truncated anyways. See bug 1757534.
32 mozilla::CheckedInt
<uint32_t> length
= aParam
.mLen
;
33 MOZ_RELEASE_ASSERT(length
.isValid());
34 WriteParam(aWriter
, length
.value());
35 // hand over ownership of the buffer to the Message
36 aWriter
->WriteBytesZeroCopy(aParam
.mData
, length
.value(), aParam
.mCapacity
);
37 aParam
.mData
= nullptr;
42 static bool Read(MessageReader
* aReader
, paramType
* aResult
) {
43 // We make a copy from the BufferList so that we get a contigous result.
45 if (!ReadParam(aReader
, &length
)) return false;
46 if (!aResult
->Allocate(length
)) {
47 mozalloc_handle_oom(length
);
50 return aReader
->ReadBytesInto(aResult
->mData
, length
);
56 #endif // ifndef mozilla_ipc_ByteBufUtils_h