| OLD | NEW |
| 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_EDK_SYSTEM_MEMORY_H_ | 5 #ifndef MOJO_EDK_SYSTEM_MEMORY_H_ |
| 6 #define MOJO_EDK_SYSTEM_MEMORY_H_ | 6 #define MOJO_EDK_SYSTEM_MEMORY_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <string.h> // For |memcpy()|. | 10 #include <string.h> // For |memcpy()|. |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "mojo/edk/system/system_impl_export.h" | 14 #include "mojo/edk/system/system_impl_export.h" |
| 15 #include "mojo/public/c/system/macros.h" | 15 #include "mojo/public/c/system/macros.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 namespace system { | 18 namespace system { |
| 19 | 19 |
| 20 namespace internal { | 20 namespace internal { |
| 21 | 21 |
| 22 // Removes |const| from |T| (available as |remove_const<T>::type|): | 22 // Removes |const| from |T| (available as |remove_const<T>::type|): |
| 23 // TODO(vtl): Remove these once we have the C++11 |remove_const|. | 23 // TODO(vtl): Remove these once we have the C++11 |remove_const|. |
| 24 template <typename T> | 24 template <typename T> |
| 25 struct remove_const { | 25 struct remove_const { |
| 26 typedef T type; | 26 using type = T; |
| 27 }; | 27 }; |
| 28 template <typename T> | 28 template <typename T> |
| 29 struct remove_const<const T> { | 29 struct remove_const<const T> { |
| 30 typedef T type; | 30 using type = T; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 // Yields |(const) char| if |T| is |(const) void|, else |T|: | 33 // Yields |(const) char| if |T| is |(const) void|, else |T|: |
| 34 template <typename T> | 34 template <typename T> |
| 35 struct VoidToChar { | 35 struct VoidToChar { |
| 36 typedef T type; | 36 using type = T; |
| 37 }; | 37 }; |
| 38 template <> | 38 template <> |
| 39 struct VoidToChar<void> { | 39 struct VoidToChar<void> { |
| 40 typedef char type; | 40 using type = char; |
| 41 }; | 41 }; |
| 42 template <> | 42 template <> |
| 43 struct VoidToChar<const void> { | 43 struct VoidToChar<const void> { |
| 44 typedef const char type; | 44 using type = const char; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 // Checks (insofar as appropriate/possible) that |pointer| is a valid pointer to | 47 // Checks (insofar as appropriate/possible) that |pointer| is a valid pointer to |
| 48 // a buffer of the given size and alignment (both in bytes). | 48 // a buffer of the given size and alignment (both in bytes). |
| 49 template <size_t size, size_t alignment> | 49 template <size_t size, size_t alignment> |
| 50 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer); | 50 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointer(const void* pointer); |
| 51 | 51 |
| 52 // Checks (insofar as appropriate/possible) that |pointer| is a valid pointer to | 52 // Checks (insofar as appropriate/possible) that |pointer| is a valid pointer to |
| 53 // a buffer of |count| elements of the given size and alignment (both in bytes). | 53 // a buffer of |count| elements of the given size and alignment (both in bytes). |
| 54 template <size_t size, size_t alignment> | 54 template <size_t size, size_t alignment> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 77 struct NullUserPointer {}; | 77 struct NullUserPointer {}; |
| 78 | 78 |
| 79 // Represents a user pointer to a single |Type| (which must be POD), for Mojo | 79 // Represents a user pointer to a single |Type| (which must be POD), for Mojo |
| 80 // primitive parameters. | 80 // primitive parameters. |
| 81 // | 81 // |
| 82 // Use a const |Type| for in parameters, and non-const |Type|s for out and | 82 // Use a const |Type| for in parameters, and non-const |Type|s for out and |
| 83 // in-out parameters (in which case the |Put()| method is available). | 83 // in-out parameters (in which case the |Put()| method is available). |
| 84 template <typename Type> | 84 template <typename Type> |
| 85 class UserPointer { | 85 class UserPointer { |
| 86 private: | 86 private: |
| 87 typedef typename internal::VoidToChar<Type>::type NonVoidType; | 87 using NonVoidType = typename internal::VoidToChar<Type>::type; |
| 88 | 88 |
| 89 public: | 89 public: |
| 90 // Instead of explicitly using these constructors, you can often use | 90 // Instead of explicitly using these constructors, you can often use |
| 91 // |MakeUserPointer()| (or |NullUserPointer()| for null pointers). (The common | 91 // |MakeUserPointer()| (or |NullUserPointer()| for null pointers). (The common |
| 92 // exception is when you have, e.g., a |char*| and want to get a | 92 // exception is when you have, e.g., a |char*| and want to get a |
| 93 // |UserPointer<void>|.) | 93 // |UserPointer<void>|.) |
| 94 UserPointer() : pointer_(nullptr) {} | 94 UserPointer() : pointer_(nullptr) {} |
| 95 explicit UserPointer(Type* pointer) : pointer_(pointer) {} | 95 explicit UserPointer(Type* pointer) : pointer_(pointer) {} |
| 96 // Allow implicit conversion from the "null user pointer". | 96 // Allow implicit conversion from the "null user pointer". |
| 97 UserPointer(NullUserPointer) : pointer_(nullptr) {} | 97 UserPointer(NullUserPointer) : pointer_(nullptr) {} |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 // MojoResult Core::GetFoos(UserPointer<uint32_t> foos, | 236 // MojoResult Core::GetFoos(UserPointer<uint32_t> foos, |
| 237 // uint32_t num_foos) { | 237 // uint32_t num_foos) { |
| 238 // UserPointer<uint32_t>::Writer foos_writer(foos, num_foos); | 238 // UserPointer<uint32_t>::Writer foos_writer(foos, num_foos); |
| 239 // MojoResult rv = GetFoosImpl(foos.GetPointer(), num_foos); | 239 // MojoResult rv = GetFoosImpl(foos.GetPointer(), num_foos); |
| 240 // foos_writer.Commit(); | 240 // foos_writer.Commit(); |
| 241 // return rv; | 241 // return rv; |
| 242 // } | 242 // } |
| 243 // | 243 // |
| 244 // TODO(vtl): Possibly, since we're not really being safe, we should just not | 244 // TODO(vtl): Possibly, since we're not really being safe, we should just not |
| 245 // copy for Release builds. | 245 // copy for Release builds. |
| 246 typedef UserPointerReader<Type> Reader; | 246 using Reader = UserPointerReader<Type>; |
| 247 typedef UserPointerWriter<Type> Writer; | 247 using Writer = UserPointerWriter<Type>; |
| 248 typedef UserPointerReaderWriter<Type> ReaderWriter; | 248 using ReaderWriter = UserPointerReaderWriter<Type>; |
| 249 | 249 |
| 250 private: | 250 private: |
| 251 friend class UserPointerReader<Type>; | 251 friend class UserPointerReader<Type>; |
| 252 friend class UserPointerReader<const Type>; | 252 friend class UserPointerReader<const Type>; |
| 253 friend class UserPointerWriter<Type>; | 253 friend class UserPointerWriter<Type>; |
| 254 friend class UserPointerReaderWriter<Type>; | 254 friend class UserPointerReaderWriter<Type>; |
| 255 template <class Options> | 255 template <class Options> |
| 256 friend class UserOptionsReader; | 256 friend class UserOptionsReader; |
| 257 | 257 |
| 258 Type* pointer_; | 258 Type* pointer_; |
| 259 // Allow copy and assignment. | 259 // Allow copy and assignment. |
| 260 }; | 260 }; |
| 261 | 261 |
| 262 // Provides a convenient way to make a |UserPointer<Type>|. | 262 // Provides a convenient way to make a |UserPointer<Type>|. |
| 263 template <typename Type> | 263 template <typename Type> |
| 264 inline UserPointer<Type> MakeUserPointer(Type* pointer) { | 264 inline UserPointer<Type> MakeUserPointer(Type* pointer) { |
| 265 return UserPointer<Type>(pointer); | 265 return UserPointer<Type>(pointer); |
| 266 } | 266 } |
| 267 | 267 |
| 268 // Implementation of |UserPointer<Type>::Reader|. | 268 // Implementation of |UserPointer<Type>::Reader|. |
| 269 template <typename Type> | 269 template <typename Type> |
| 270 class UserPointerReader { | 270 class UserPointerReader { |
| 271 private: | 271 private: |
| 272 typedef typename internal::remove_const<Type>::type TypeNoConst; | 272 using TypeNoConst = typename internal::remove_const<Type>::type; |
| 273 | 273 |
| 274 public: | 274 public: |
| 275 // Note: If |count| is zero, |GetPointer()| will always return null. | 275 // Note: If |count| is zero, |GetPointer()| will always return null. |
| 276 UserPointerReader(UserPointer<const Type> user_pointer, size_t count) { | 276 UserPointerReader(UserPointer<const Type> user_pointer, size_t count) { |
| 277 Init(user_pointer.pointer_, count, true); | 277 Init(user_pointer.pointer_, count, true); |
| 278 } | 278 } |
| 279 UserPointerReader(UserPointer<TypeNoConst> user_pointer, size_t count) { | 279 UserPointerReader(UserPointer<TypeNoConst> user_pointer, size_t count) { |
| 280 Init(user_pointer.pointer_, count, true); | 280 Init(user_pointer.pointer_, count, true); |
| 281 } | 281 } |
| 282 | 282 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 size_t count_; | 368 size_t count_; |
| 369 scoped_ptr<Type[]> buffer_; | 369 scoped_ptr<Type[]> buffer_; |
| 370 | 370 |
| 371 DISALLOW_COPY_AND_ASSIGN(UserPointerReaderWriter); | 371 DISALLOW_COPY_AND_ASSIGN(UserPointerReaderWriter); |
| 372 }; | 372 }; |
| 373 | 373 |
| 374 } // namespace system | 374 } // namespace system |
| 375 } // namespace mojo | 375 } // namespace mojo |
| 376 | 376 |
| 377 #endif // MOJO_EDK_SYSTEM_MEMORY_H_ | 377 #endif // MOJO_EDK_SYSTEM_MEMORY_H_ |
| OLD | NEW |