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/bindings_internal.h" |
| 6 |
| 7 namespace mojo { |
| 8 namespace internal { |
| 9 |
| 10 void EncodePointer(void* address, uint64_t* offset) { |
| 11 if (!address) { |
| 12 *offset = 0; |
| 13 return; |
| 14 } |
| 15 uint8_t* p_obj = reinterpret_cast<uint8_t*>(address); |
| 16 uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset); |
| 17 assert(p_obj > p_slot); |
| 18 *offset = p_obj - p_slot; |
| 19 } |
| 20 |
| 21 uint8_t* DecodePointerRaw(uint64_t* offset) { |
| 22 if (!*offset) |
| 23 return NULL; |
| 24 return reinterpret_cast<uint8_t*>(offset) + *offset; |
| 25 } |
| 26 |
| 27 bool ValidatePointer(const void* ptr, const Message& message) { |
| 28 const uint8_t* data = static_cast<const uint8_t*>(ptr); |
| 29 /* TODO: Enforce alignment |
| 30 if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0) |
| 31 return false; |
| 32 */ |
| 33 return data >= message.data_start && data < message.data_end; |
| 34 } |
| 35 |
| 36 void EncodeHandle(Handle* handle, std::vector<Handle>* handles) { |
| 37 handles->push_back(*handle); |
| 38 handle->value = static_cast<MojoHandle>(handles->size() - 1); |
| 39 } |
| 40 |
| 41 bool DecodeHandle(Handle* handle, const std::vector<Handle>& handles) { |
| 42 if (handle->value >= handles.size()) |
| 43 return false; |
| 44 *handle = handles[handle->value]; |
| 45 return true; |
| 46 } |
| 47 |
| 48 } // namespace internal |
| 49 } // namespace mojo |
OLD | NEW |