Chromium Code Reviews| Index: mojo/public/bindings/lib/bindings_internal.cc |
| diff --git a/mojo/public/bindings/lib/bindings_internal.cc b/mojo/public/bindings/lib/bindings_internal.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..10289cc09544f02fea2e4bd6d216df7999f2f529 |
| --- /dev/null |
| +++ b/mojo/public/bindings/lib/bindings_internal.cc |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/public/bindings/lib/bindings_internal.h" |
| + |
| +#include <assert.h> |
| + |
| +namespace mojo { |
| +namespace internal { |
| + |
| +void EncodePointer(void* address, uint64_t* offset) { |
|
viettrungluu
2013/10/09 03:44:06
const void* address?
|
| + if (!address) { |
| + *offset = 0; |
| + return; |
| + } |
| + 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
|
| + uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset); |
| + assert(p_obj > p_slot); |
| + *offset = p_obj - p_slot; |
|
viettrungluu
2013/10/09 03:44:06
(And there should be a cast here too, I think.)
|
| +} |
| + |
| +uint8_t* DecodePointerRaw(uint64_t* offset) { |
|
viettrungluu
2013/10/09 03:44:06
Probably this should return void*? (And take a con
|
| + if (!*offset) |
| + return NULL; |
| + return reinterpret_cast<uint8_t*>(offset) + *offset; |
| +} |
| + |
| +bool ValidatePointer(const void* ptr, const Message& message) { |
| + const uint8_t* data = static_cast<const uint8_t*>(ptr); |
| + if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0) |
| + return false; |
| + |
| + const uint8_t* data_start = reinterpret_cast<const uint8_t*>(message.data); |
| + const uint8_t* data_end = data_start + message.data->header.num_bytes; |
| + |
| + return data >= data_start && data < data_end; |
| +} |
| + |
| +void EncodeHandle(Handle* handle, std::vector<Handle>* handles) { |
| + handles->push_back(*handle); |
| + handle->value = static_cast<MojoHandle>(handles->size() - 1); |
| +} |
| + |
| +bool DecodeHandle(Handle* handle, const std::vector<Handle>& handles) { |
| + if (handle->value >= handles.size()) |
| + return false; |
| + *handle = handles[handle->value]; |
| + return true; |
| +} |
| + |
| +// static |
| +void ArrayHelper<Handle>::EncodePointersAndHandles( |
| + ArrayHeader* header, |
| + ElementType* elements, |
| + std::vector<Handle>* handles) { |
| + for (uint32_t i = 0; i < header->num_elements; ++i) |
| + EncodeHandle(&elements[i], handles); |
| +} |
| + |
| +// static |
| +bool ArrayHelper<Handle>::DecodePointersAndHandles( |
| + ArrayHeader* header, |
| + ElementType* elements, |
| + const Message& message) { |
| + for (uint32_t i = 0; i < header->num_elements; ++i) { |
| + if (!DecodeHandle(&elements[i], message.handles)) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace internal |
| +} // namespace mojo |