| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "mojo/public/cpp/bindings/lib/serialization_util.h" | 5 #include "mojo/public/cpp/bindings/lib/serialization_context.h" |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | 6 |
| 10 #include <limits> | 7 #include <limits> |
| 11 | 8 |
| 12 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| 11 #include "mojo/public/cpp/system/core.h" |
| 13 | 12 |
| 14 namespace mojo { | 13 namespace mojo { |
| 15 namespace internal { | 14 namespace internal { |
| 16 | 15 |
| 17 namespace { | |
| 18 | |
| 19 const size_t kAlignment = 8; | |
| 20 | |
| 21 template <typename T> | |
| 22 T AlignImpl(T t) { | |
| 23 return t + (kAlignment - (t % kAlignment)) % kAlignment; | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 size_t Align(size_t size) { | |
| 29 return AlignImpl(size); | |
| 30 } | |
| 31 | |
| 32 char* AlignPointer(char* ptr) { | |
| 33 return reinterpret_cast<char*>(AlignImpl(reinterpret_cast<uintptr_t>(ptr))); | |
| 34 } | |
| 35 | |
| 36 bool IsAligned(const void* ptr) { | |
| 37 return !(reinterpret_cast<uintptr_t>(ptr) % kAlignment); | |
| 38 } | |
| 39 | |
| 40 void EncodePointer(const void* ptr, uint64_t* offset) { | |
| 41 if (!ptr) { | |
| 42 *offset = 0; | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 const char* p_obj = reinterpret_cast<const char*>(ptr); | |
| 47 const char* p_slot = reinterpret_cast<const char*>(offset); | |
| 48 DCHECK(p_obj > p_slot); | |
| 49 | |
| 50 *offset = static_cast<uint64_t>(p_obj - p_slot); | |
| 51 } | |
| 52 | |
| 53 const void* DecodePointerRaw(const uint64_t* offset) { | |
| 54 if (!*offset) | |
| 55 return nullptr; | |
| 56 return reinterpret_cast<const char*>(offset) + *offset; | |
| 57 } | |
| 58 | |
| 59 SerializedHandleVector::SerializedHandleVector() {} | 16 SerializedHandleVector::SerializedHandleVector() {} |
| 60 | 17 |
| 61 SerializedHandleVector::~SerializedHandleVector() { | 18 SerializedHandleVector::~SerializedHandleVector() { |
| 62 for (auto handle : handles_) { | 19 for (auto handle : handles_) { |
| 63 if (handle.is_valid()) { | 20 if (handle.is_valid()) { |
| 64 MojoResult rv = MojoClose(handle.value()); | 21 MojoResult rv = MojoClose(handle.value()); |
| 65 DCHECK_EQ(rv, MOJO_RESULT_OK); | 22 DCHECK_EQ(rv, MOJO_RESULT_OK); |
| 66 } | 23 } |
| 67 } | 24 } |
| 68 } | 25 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 96 SerializationContext::SerializationContext( | 53 SerializationContext::SerializationContext( |
| 97 scoped_refptr<MultiplexRouter> in_router) | 54 scoped_refptr<MultiplexRouter> in_router) |
| 98 : router(std::move(in_router)) {} | 55 : router(std::move(in_router)) {} |
| 99 | 56 |
| 100 SerializationContext::~SerializationContext() { | 57 SerializationContext::~SerializationContext() { |
| 101 DCHECK(!custom_contexts || custom_contexts->empty()); | 58 DCHECK(!custom_contexts || custom_contexts->empty()); |
| 102 } | 59 } |
| 103 | 60 |
| 104 } // namespace internal | 61 } // namespace internal |
| 105 } // namespace mojo | 62 } // namespace mojo |
| OLD | NEW |