/work/obj-fuzz/dist/include/mozilla/ipc/ByteBuf.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
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. */ |
10 | | |
11 | | #ifndef mozilla_ipc_ByteBuf_h |
12 | | #define mozilla_ipc_ByteBuf_h |
13 | | |
14 | | #include "ipc/IPCMessageUtils.h" |
15 | | |
16 | | namespace mozilla { |
17 | | |
18 | | namespace ipc { |
19 | | |
20 | | class ByteBuf final |
21 | | { |
22 | | friend struct IPC::ParamTraits<mozilla::ipc::ByteBuf>; |
23 | | public: |
24 | | bool |
25 | | Allocate(size_t aLength) |
26 | 0 | { |
27 | 0 | MOZ_ASSERT(mData == nullptr); |
28 | 0 | mData = (uint8_t*)malloc(aLength); |
29 | 0 | if (!mData) { |
30 | 0 | return false; |
31 | 0 | } |
32 | 0 | mLen = aLength; |
33 | 0 | mCapacity = aLength; |
34 | 0 | return true; |
35 | 0 | } |
36 | | |
37 | | ByteBuf() |
38 | | : mData(nullptr) |
39 | | , mLen(0) |
40 | | , mCapacity(0) |
41 | 0 | {} |
42 | | |
43 | | ByteBuf(uint8_t *aData, size_t aLen, size_t aCapacity) |
44 | | : mData(aData) |
45 | | , mLen(aLen) |
46 | | , mCapacity(aCapacity) |
47 | 0 | {} |
48 | | |
49 | | ByteBuf(const ByteBuf& aFrom) = delete; |
50 | | |
51 | | ByteBuf(ByteBuf&& aFrom) |
52 | | : mData(aFrom.mData) |
53 | | , mLen(aFrom.mLen) |
54 | | , mCapacity(aFrom.mCapacity) |
55 | 0 | { |
56 | 0 | aFrom.mData = nullptr; |
57 | 0 | aFrom.mLen = 0; |
58 | 0 | aFrom.mCapacity = 0; |
59 | 0 | } |
60 | | |
61 | | ~ByteBuf() |
62 | 0 | { |
63 | 0 | free(mData); |
64 | 0 | } |
65 | | |
66 | | uint8_t* mData; |
67 | | size_t mLen; |
68 | | size_t mCapacity; |
69 | | }; |
70 | | |
71 | | |
72 | | } // namespace ipc |
73 | | } // namespace mozilla |
74 | | |
75 | | |
76 | | namespace IPC { |
77 | | |
78 | | template<> |
79 | | struct ParamTraits<mozilla::ipc::ByteBuf> |
80 | | { |
81 | | typedef mozilla::ipc::ByteBuf paramType; |
82 | | |
83 | | // this is where we transfer the memory from the ByteBuf to IPDL, avoiding a copy |
84 | | static void Write(Message* aMsg, paramType& aParam) |
85 | 0 | { |
86 | 0 | WriteParam(aMsg, aParam.mLen); |
87 | 0 | // hand over ownership of the buffer to the Message |
88 | 0 | aMsg->WriteBytesZeroCopy(aParam.mData, aParam.mLen, aParam.mCapacity); |
89 | 0 | aParam.mData = nullptr; |
90 | 0 | aParam.mCapacity = 0; |
91 | 0 | aParam.mLen = 0; |
92 | 0 | } |
93 | | |
94 | | static bool Read(const Message* aMsg, PickleIterator* aIter, paramType* aResult) |
95 | 0 | { |
96 | 0 | // We make a copy from the BufferList so that we get a contigous result. |
97 | 0 | // For users the can handle a non-contiguous result using ExtractBuffers |
98 | 0 | // is an option, alternatively if the users don't need to take ownership of |
99 | 0 | // the data they can use the removed FlattenBytes (bug 1297981) |
100 | 0 | size_t length; |
101 | 0 | return ReadParam(aMsg, aIter, &length) |
102 | 0 | && aResult->Allocate(length) |
103 | 0 | && aMsg->ReadBytesInto(aIter, aResult->mData, length); |
104 | 0 | } |
105 | | |
106 | | static void Log(const paramType& aParam, std::wstring* aLog) |
107 | 0 | { |
108 | 0 | aLog->append(L"(byte buf)"); |
109 | 0 | } |
110 | | }; |
111 | | |
112 | | |
113 | | } // namespace IPC |
114 | | |
115 | | |
116 | | #endif // ifndef mozilla_ipc_Shmem_h |