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_internal.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 namespace mojo { | |
10 namespace internal { | |
11 | |
12 void EncodePointer(void* address, uint64_t* offset) { | |
viettrungluu
2013/10/09 03:44:06
const void* address?
| |
13 if (!address) { | |
14 *offset = 0; | |
15 return; | |
16 } | |
17 uint8_t* p_obj = reinterpret_cast<uint8_t*>(address); | |
viettrungluu
2013/10/09 03:44:06
static_cast
Though maybe you should be using uint
| |
18 uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset); | |
19 assert(p_obj > p_slot); | |
20 *offset = p_obj - p_slot; | |
viettrungluu
2013/10/09 03:44:06
(And there should be a cast here too, I think.)
| |
21 } | |
22 | |
23 uint8_t* DecodePointerRaw(uint64_t* offset) { | |
viettrungluu
2013/10/09 03:44:06
Probably this should return void*? (And take a con
| |
24 if (!*offset) | |
25 return NULL; | |
26 return reinterpret_cast<uint8_t*>(offset) + *offset; | |
27 } | |
28 | |
29 bool ValidatePointer(const void* ptr, const Message& message) { | |
30 const uint8_t* data = static_cast<const uint8_t*>(ptr); | |
31 if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0) | |
32 return false; | |
33 | |
34 const uint8_t* data_start = reinterpret_cast<const uint8_t*>(message.data); | |
35 const uint8_t* data_end = data_start + message.data->header.num_bytes; | |
36 | |
37 return data >= data_start && data < data_end; | |
38 } | |
39 | |
40 void EncodeHandle(Handle* handle, std::vector<Handle>* handles) { | |
41 handles->push_back(*handle); | |
42 handle->value = static_cast<MojoHandle>(handles->size() - 1); | |
43 } | |
44 | |
45 bool DecodeHandle(Handle* handle, const std::vector<Handle>& handles) { | |
46 if (handle->value >= handles.size()) | |
47 return false; | |
48 *handle = handles[handle->value]; | |
49 return true; | |
50 } | |
51 | |
52 // static | |
53 void ArrayHelper<Handle>::EncodePointersAndHandles( | |
54 ArrayHeader* header, | |
55 ElementType* elements, | |
56 std::vector<Handle>* handles) { | |
57 for (uint32_t i = 0; i < header->num_elements; ++i) | |
58 EncodeHandle(&elements[i], handles); | |
59 } | |
60 | |
61 // static | |
62 bool ArrayHelper<Handle>::DecodePointersAndHandles( | |
63 ArrayHeader* header, | |
64 ElementType* elements, | |
65 const Message& message) { | |
66 for (uint32_t i = 0; i < header->num_elements; ++i) { | |
67 if (!DecodeHandle(&elements[i], message.handles)) | |
68 return false; | |
69 } | |
70 return true; | |
71 } | |
72 | |
73 } // namespace internal | |
74 } // namespace mojo | |
OLD | NEW |