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 3 // found in the LICENSE file. 4 5 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 6 7 namespace mojo { 8 namespace internal { 9 10 namespace { 11 12 const size_t kAlignment = 8; 13 14 template <typename T> 15 T AlignImpl(T t) { 16 return t + (kAlignment - (t % kAlignment)) % kAlignment; 17 } 18 19 } // namespace 20 21 size_t Align(size_t size) { 22 return AlignImpl(size); 23 } 24 25 char* AlignPointer(char* ptr) { 26 return reinterpret_cast<char*>(AlignImpl(reinterpret_cast<uintptr_t>(ptr))); 27 } 28 29 bool IsAligned(const void* ptr) { 30 return !(reinterpret_cast<uintptr_t>(ptr) % kAlignment); 31 } 32 33 void EncodePointer(const void* ptr, uint64_t* offset) { 34 if (!ptr) { 35 *offset = 0; 36 return; 37 } 38 39 const char* p_obj = reinterpret_cast<const char*>(ptr); 40 const char* p_slot = reinterpret_cast<const char*>(offset); 41 DCHECK(p_obj > p_slot); 42 43 *offset = static_cast<uint64_t>(p_obj - p_slot); 44 } 45 46 } // namespace internal 47 } // namespace mojo 48