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 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | |
6 | |
7 #include "mojo/public/cpp/environment/logging.h" | |
8 | |
9 namespace mojo { | |
10 namespace internal { | |
11 | |
12 namespace { | |
13 | |
14 const size_t kAlignment = 8; | |
15 | |
16 template <typename T> | |
17 T AlignImpl(T t) { | |
18 return t + (kAlignment - (t % kAlignment)) % kAlignment; | |
19 } | |
20 | |
21 } // namespace | |
22 | |
23 size_t Align(size_t size) { | |
24 return AlignImpl(size); | |
25 } | |
26 | |
27 char* AlignPointer(char* ptr) { | |
28 return reinterpret_cast<char*>(AlignImpl(reinterpret_cast<uintptr_t>(ptr))); | |
29 } | |
30 | |
31 bool IsAligned(const void* ptr) { | |
32 return !(reinterpret_cast<uintptr_t>(ptr) % kAlignment); | |
33 } | |
34 | |
35 void EncodePointer(const void* ptr, uint64_t* offset) { | |
36 if (!ptr) { | |
37 *offset = 0; | |
38 return; | |
39 } | |
40 | |
41 const char* p_obj = reinterpret_cast<const char*>(ptr); | |
42 const char* p_slot = reinterpret_cast<const char*>(offset); | |
43 MOJO_DCHECK(p_obj > p_slot); | |
44 | |
45 *offset = static_cast<uint64_t>(p_obj - p_slot); | |
46 } | |
47 | |
48 const void* DecodePointerRaw(const uint64_t* offset) { | |
49 if (!*offset) | |
50 return nullptr; | |
51 return reinterpret_cast<const char*>(offset) + *offset; | |
52 } | |
53 | |
54 void EncodeHandle(Handle* handle, std::vector<Handle>* handles) { | |
55 if (handle->is_valid()) { | |
56 handles->push_back(*handle); | |
57 handle->set_value(static_cast<MojoHandle>(handles->size() - 1)); | |
58 } else { | |
59 handle->set_value(kEncodedInvalidHandleValue); | |
60 } | |
61 } | |
62 | |
63 void EncodeHandle(Interface_Data* data, std::vector<Handle>* handles) { | |
64 EncodeHandle(&data->handle, handles); | |
65 } | |
66 | |
67 void EncodeHandle(MojoHandle* handle, std::vector<Handle>* handles) { | |
68 EncodeHandle(reinterpret_cast<Handle*>(handle), handles); | |
69 } | |
70 | |
71 void DecodeHandle(Handle* handle, std::vector<Handle>* handles) { | |
72 if (handle->value() == kEncodedInvalidHandleValue) { | |
73 *handle = Handle(); | |
74 return; | |
75 } | |
76 MOJO_DCHECK(handle->value() < handles->size()); | |
77 // Just leave holes in the vector so we don't screw up other indices. | |
78 *handle = FetchAndReset(&handles->at(handle->value())); | |
79 } | |
80 | |
81 void DecodeHandle(Interface_Data* data, std::vector<Handle>* handles) { | |
82 DecodeHandle(&data->handle, handles); | |
83 } | |
84 | |
85 void DecodeHandle(MojoHandle* handle, std::vector<Handle>* handles) { | |
86 DecodeHandle(reinterpret_cast<Handle*>(handle), handles); | |
87 } | |
88 | |
89 } // namespace internal | |
90 } // namespace mojo | |
OLD | NEW |