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

Side by Side Diff: mojo/public/cpp/bindings/lib/bindings_internal.h

Issue 2202023005: Mojo C++ bindings: inline pointer encoding/decoding/validating functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@85_inline_data_view
Patch Set: . Created 4 years, 4 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
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/bindings_internal.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <functional> 10 #include <functional>
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 const uint32_t kUnionDataSize = 16; 67 const uint32_t kUnionDataSize = 16;
68 68
69 template <typename T> 69 template <typename T>
70 class Array_Data; 70 class Array_Data;
71 71
72 template <typename K, typename V> 72 template <typename K, typename V>
73 class Map_Data; 73 class Map_Data;
74 74
75 using String_Data = Array_Data<char>; 75 using String_Data = Array_Data<char>;
76 76
77 size_t Align(size_t size); 77 inline size_t Align(size_t size) {
78 char* AlignPointer(char* ptr); 78 return (size + 7) & ~0x7;
79 }
79 80
80 bool IsAligned(const void* ptr); 81 inline bool IsAligned(const void* ptr) {
82 return !(reinterpret_cast<uintptr_t>(ptr) & 0x7);
83 }
81 84
82 // Pointers are encoded as relative offsets. The offsets are relative to the 85 // Pointers are encoded as relative offsets. The offsets are relative to the
83 // address of where the offset value is stored, such that the pointer may be 86 // address of where the offset value is stored, such that the pointer may be
84 // recovered with the expression: 87 // recovered with the expression:
85 // 88 //
86 // ptr = reinterpret_cast<char*>(offset) + *offset 89 // ptr = reinterpret_cast<char*>(offset) + *offset
87 // 90 //
88 // A null pointer is encoded as an offset value of 0. 91 // A null pointer is encoded as an offset value of 0.
89 // 92 //
90 void EncodePointer(const void* ptr, uint64_t* offset); 93 inline void EncodePointer(const void* ptr, uint64_t* offset) {
94 if (!ptr) {
95 *offset = 0;
96 return;
97 }
98
99 const char* p_obj = reinterpret_cast<const char*>(ptr);
100 const char* p_slot = reinterpret_cast<const char*>(offset);
101 DCHECK(p_obj > p_slot);
102
103 *offset = static_cast<uint64_t>(p_obj - p_slot);
104 }
105
91 // Note: This function doesn't validate the encoded pointer value. 106 // Note: This function doesn't validate the encoded pointer value.
92 inline const void* DecodePointer(const uint64_t* offset) { 107 inline const void* DecodePointer(const uint64_t* offset) {
93 if (!*offset) 108 if (!*offset)
94 return nullptr; 109 return nullptr;
95 return reinterpret_cast<const char*>(offset) + *offset; 110 return reinterpret_cast<const char*>(offset) + *offset;
96 } 111 }
97 112
98 #pragma pack(push, 1) 113 #pragma pack(push, 1)
99 114
100 struct StructHeader { 115 struct StructHeader {
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 380
366 template <> 381 template <>
367 struct DataViewTraits<NativeStructDataView> { 382 struct DataViewTraits<NativeStructDataView> {
368 using MojomType = NativeStructPtr; 383 using MojomType = NativeStructPtr;
369 }; 384 };
370 385
371 } // namespace internal 386 } // namespace internal
372 } // namespace mojo 387 } // namespace mojo
373 388
374 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 389 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/bindings_internal.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698