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/bindings/lib/bindings_serialization.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 #include "mojo/public/bindings/lib/bindings_internal.h" | |
10 | |
11 namespace mojo { | |
12 namespace internal { | |
13 | |
14 size_t Align(size_t size) { | |
15 const size_t kAlignment = 8; | |
16 return size + (kAlignment - (size % kAlignment)) % kAlignment; | |
17 } | |
18 | |
19 void EncodePointer(const void* ptr, uint64_t* offset) { | |
20 if (!ptr) { | |
21 *offset = 0; | |
22 return; | |
23 } | |
24 | |
25 const char* p_obj = reinterpret_cast<const char*>(ptr); | |
26 const char* p_slot = reinterpret_cast<const char*>(offset); | |
27 assert(p_obj > p_slot); | |
28 | |
29 *offset = static_cast<uint64_t>(p_obj - p_slot); | |
30 } | |
31 | |
32 const void* DecodePointerRaw(const uint64_t* offset) { | |
33 if (!*offset) | |
34 return NULL; | |
35 return reinterpret_cast<const char*>(offset) + *offset; | |
36 } | |
37 | |
38 bool ValidatePointer(const void* ptr, const Message& message) { | |
39 const uint8_t* data = static_cast<const uint8_t*>(ptr); | |
40 if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0) | |
41 return false; | |
42 | |
43 const uint8_t* data_start = message.data(); | |
44 const uint8_t* data_end = data_start + message.data_num_bytes(); | |
45 | |
46 return data >= data_start && data < data_end; | |
47 } | |
48 | |
49 void EncodeHandle(Handle* handle, std::vector<Handle>* handles) { | |
50 if (handle->is_valid()) { | |
51 handles->push_back(*handle); | |
52 handle->set_value(static_cast<MojoHandle>(handles->size() - 1)); | |
53 } else { | |
54 // Encode -1 to mean the invalid handle. | |
55 handle->set_value(static_cast<MojoHandle>(-1)); | |
56 } | |
57 } | |
58 | |
59 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles) { | |
60 // Decode -1 to mean the invalid handle. | |
61 if (handle->value() == static_cast<MojoHandle>(-1)) { | |
62 *handle = Handle(); | |
63 return true; | |
64 } | |
65 if (handle->value() >= handles->size()) | |
66 return false; | |
67 // Just leave holes in the vector so we don't screw up other indices. | |
68 *handle = FetchAndReset(&handles->at(handle->value())); | |
69 return true; | |
70 } | |
71 | |
72 } // namespace internal | |
73 } // namespace mojo | |
OLD | NEW |