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