Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: mojo/public/bindings/lib/bindings_internal.cc

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Stop building src/mojo on iOS Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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(const void* ptr, uint64_t* offset) {
13 if (!ptr) {
14 *offset = 0;
15 return;
16 }
17
18 const char* p_obj = reinterpret_cast<const char*>(ptr);
19 const char* p_slot = reinterpret_cast<const char*>(offset);
20 assert(p_obj > p_slot);
21
22 *offset = static_cast<uint64_t>(p_obj - p_slot);
23 }
24
25 const void* DecodePointerRaw(const uint64_t* offset) {
26 if (!*offset)
27 return NULL;
28 return reinterpret_cast<const char*>(offset) + *offset;
29 }
30
31 bool ValidatePointer(const void* ptr, const Message& message) {
32 const uint8_t* data = static_cast<const uint8_t*>(ptr);
33 if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0)
34 return false;
35
36 const uint8_t* data_start = reinterpret_cast<const uint8_t*>(message.data);
37 const uint8_t* data_end = data_start + message.data->header.num_bytes;
38
39 return data >= data_start && data < data_end;
40 }
41
42 void EncodeHandle(Handle* handle, std::vector<Handle>* handles) {
43 handles->push_back(*handle);
44 handle->value = static_cast<MojoHandle>(handles->size() - 1);
45 }
46
47 bool DecodeHandle(Handle* handle, const std::vector<Handle>& handles) {
48 if (handle->value >= handles.size())
49 return false;
50 *handle = handles[handle->value];
51 return true;
52 }
53
54 // static
55 void ArrayHelper<Handle>::EncodePointersAndHandles(
56 ArrayHeader* header,
57 ElementType* elements,
58 std::vector<Handle>* handles) {
59 for (uint32_t i = 0; i < header->num_elements; ++i)
60 EncodeHandle(&elements[i], handles);
61 }
62
63 // static
64 bool ArrayHelper<Handle>::DecodePointersAndHandles(
65 ArrayHeader* header,
66 ElementType* elements,
67 const Message& message) {
68 for (uint32_t i = 0; i < header->num_elements; ++i) {
69 if (!DecodeHandle(&elements[i], message.handles))
70 return false;
71 }
72 return true;
73 }
74
75 } // namespace internal
76 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698