Index: mojo/public/bindings/bindings_internal.cc |
diff --git a/mojo/public/bindings/bindings_internal.cc b/mojo/public/bindings/bindings_internal.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7b80c2dbadec814c8c4a642ad14f032a3dad3208 |
--- /dev/null |
+++ b/mojo/public/bindings/bindings_internal.cc |
@@ -0,0 +1,49 @@ |
+// 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/bindings_internal.h" |
+ |
+namespace mojo { |
+namespace internal { |
+ |
+void EncodePointer(void* address, uint64_t* offset) { |
+ if (!address) { |
+ *offset = 0; |
+ return; |
+ } |
+ uint8_t* p_obj = reinterpret_cast<uint8_t*>(address); |
+ uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset); |
+ assert(p_obj > p_slot); |
+ *offset = p_obj - p_slot; |
+} |
+ |
+uint8_t* DecodePointerRaw(uint64_t* offset) { |
+ 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); |
+ /* TODO: Enforce alignment |
+ if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0) |
+ return false; |
+ */ |
+ return data >= message.data_start && data < message.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; |
+} |
+ |
+} // namespace internal |
+} // namespace mojo |