| OLD | NEW |
| (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/edk/system/memory.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "build/build_config.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 namespace system { | |
| 14 namespace internal { | |
| 15 | |
| 16 template <size_t alignment> | |
| 17 bool IsAligned(const void* pointer) { | |
| 18 return reinterpret_cast<uintptr_t>(pointer) % alignment == 0; | |
| 19 } | |
| 20 | |
| 21 // MSVS (2010, 2013) sometimes (on the stack) aligns, e.g., |int64_t|s (for | |
| 22 // which |__alignof(int64_t)| is 8) to 4-byte boundaries. http://goo.gl/Y2n56T | |
| 23 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | |
| 24 template <> | |
| 25 bool IsAligned<8>(const void* pointer) { | |
| 26 return reinterpret_cast<uintptr_t>(pointer) % 4 == 0; | |
| 27 } | |
| 28 #endif | |
| 29 | |
| 30 template <size_t size, size_t alignment> | |
| 31 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer) { | |
| 32 CHECK(pointer && IsAligned<alignment>(pointer)); | |
| 33 } | |
| 34 | |
| 35 // Explicitly instantiate the sizes we need. Add instantiations as needed. | |
| 36 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<1, 1>(const void*); | |
| 37 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<4, 4>(const void*); | |
| 38 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 4>(const void*); | |
| 39 template void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer<8, 8>(const void*); | |
| 40 | |
| 41 template <size_t size, size_t alignment> | |
| 42 void MOJO_SYSTEM_IMPL_EXPORT | |
| 43 CheckUserPointerWithCount(const void* pointer, size_t count) { | |
| 44 CHECK_LE(count, std::numeric_limits<size_t>::max() / size); | |
| 45 CHECK(count == 0 || (pointer && IsAligned<alignment>(pointer))); | |
| 46 } | |
| 47 | |
| 48 // Explicitly instantiate the sizes we need. Add instantiations as needed. | |
| 49 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 50 CheckUserPointerWithCount<1, 1>(const void*, size_t); | |
| 51 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 52 CheckUserPointerWithCount<4, 4>(const void*, size_t); | |
| 53 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 54 CheckUserPointerWithCount<8, 4>(const void*, size_t); | |
| 55 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 56 CheckUserPointerWithCount<8, 8>(const void*, size_t); | |
| 57 | |
| 58 template <size_t alignment> | |
| 59 void CheckUserPointerWithSize(const void* pointer, size_t size) { | |
| 60 // TODO(vtl): If running in kernel mode, do a full verification. For now, just | |
| 61 // check that it's non-null and aligned. (A faster user mode implementation is | |
| 62 // also possible if this check is skipped.) | |
| 63 CHECK(size == 0 || (!!pointer && internal::IsAligned<alignment>(pointer))); | |
| 64 } | |
| 65 | |
| 66 // Explicitly instantiate the sizes we need. Add instantiations as needed. | |
| 67 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 68 CheckUserPointerWithSize<1>(const void*, size_t); | |
| 69 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 70 CheckUserPointerWithSize<4>(const void*, size_t); | |
| 71 // Whereas the other |Check...()| functions are usually used with integral typs | |
| 72 // or arrays of integral types, this one is used with Options structs for which | |
| 73 // alignment has been explicitly been specified (using |MOJO_ALIGNAS()|), which | |
| 74 // MSVS *does* respect. | |
| 75 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | |
| 76 template <> | |
| 77 void MOJO_SYSTEM_IMPL_EXPORT | |
| 78 CheckUserPointerWithSize<8>(const void* pointer, size_t size) { | |
| 79 CHECK(size == 0 || | |
| 80 (!!pointer && reinterpret_cast<uintptr_t>(pointer) % 8 == 0)); | |
| 81 } | |
| 82 #else | |
| 83 template void MOJO_SYSTEM_IMPL_EXPORT | |
| 84 CheckUserPointerWithSize<8>(const void*, size_t); | |
| 85 #endif | |
| 86 | |
| 87 } // namespace internal | |
| 88 } // namespace system | |
| 89 } // namespace mojo | |
| OLD | NEW |