| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 11 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | |
| 12 #include "mojo/public/cpp/system/core.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace internal { | |
| 16 | |
| 17 // Please note that this is a different value than |mojo::kInvalidHandleValue|, | |
| 18 // which is the "decoded" invalid handle. | |
| 19 const MojoHandle kEncodedInvalidHandleValue = static_cast<MojoHandle>(-1); | |
| 20 | |
| 21 size_t Align(size_t size); | |
| 22 char* AlignPointer(char* ptr); | |
| 23 | |
| 24 bool IsAligned(const void* ptr); | |
| 25 | |
| 26 // Pointers are encoded as relative offsets. The offsets are relative to the | |
| 27 // address of where the offset value is stored, such that the pointer may be | |
| 28 // recovered with the expression: | |
| 29 // | |
| 30 // ptr = reinterpret_cast<char*>(offset) + *offset | |
| 31 // | |
| 32 // A null pointer is encoded as an offset value of 0. | |
| 33 // | |
| 34 void EncodePointer(const void* ptr, uint64_t* offset); | |
| 35 // Note: This function doesn't validate the encoded pointer value. | |
| 36 const void* DecodePointerRaw(const uint64_t* offset); | |
| 37 | |
| 38 // Note: This function doesn't validate the encoded pointer value. | |
| 39 template <typename T> | |
| 40 inline void DecodePointer(const uint64_t* offset, T** ptr) { | |
| 41 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset))); | |
| 42 } | |
| 43 | |
| 44 // Handles are encoded as indices into a vector of handles. These functions | |
| 45 // manipulate the value of |handle|, mapping it to and from an index. | |
| 46 | |
| 47 void EncodeHandle(Handle* handle, std::vector<Handle>* handles); | |
| 48 void EncodeHandle(Interface_Data* data, std::vector<Handle>* handles); | |
| 49 void EncodeHandle(MojoHandle* handle, std::vector<Handle>* handles); | |
| 50 // Note: The following three functions don't validate the encoded handle value. | |
| 51 void DecodeHandle(Handle* handle, std::vector<Handle>* handles); | |
| 52 void DecodeHandle(Interface_Data* data, std::vector<Handle>* handles); | |
| 53 void DecodeHandle(MojoHandle* handle, std::vector<Handle>* handles); | |
| 54 | |
| 55 // The following 2 functions are used to encode/decode all objects (structs and | |
| 56 // arrays) in a consistent manner. | |
| 57 | |
| 58 template <typename T> | |
| 59 inline void Encode(T* obj, std::vector<Handle>* handles) { | |
| 60 if (obj->ptr) | |
| 61 obj->ptr->EncodePointersAndHandles(handles); | |
| 62 EncodePointer(obj->ptr, &obj->offset); | |
| 63 } | |
| 64 | |
| 65 // Note: This function doesn't validate the encoded pointer and handle values. | |
| 66 template <typename T> | |
| 67 inline void Decode(T* obj, std::vector<Handle>* handles) { | |
| 68 DecodePointer(&obj->offset, &obj->ptr); | |
| 69 if (obj->ptr) | |
| 70 obj->ptr->DecodePointersAndHandles(handles); | |
| 71 } | |
| 72 | |
| 73 template <typename T> | |
| 74 inline void InterfacePointerToData(InterfacePtr<T> input, | |
| 75 Interface_Data* output) { | |
| 76 InterfacePtrInfo<T> info = input.PassInterface(); | |
| 77 output->handle = info.PassHandle().release(); | |
| 78 output->version = info.version(); | |
| 79 } | |
| 80 | |
| 81 template <typename T> | |
| 82 inline void InterfaceDataToPointer(Interface_Data* input, | |
| 83 InterfacePtr<T>* output) { | |
| 84 output->Bind(InterfacePtrInfo<T>( | |
| 85 MakeScopedHandle(FetchAndReset(&input->handle)), input->version)); | |
| 86 } | |
| 87 | |
| 88 } // namespace internal | |
| 89 } // namespace mojo | |
| 90 | |
| 91 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ | |
| OLD | NEW |