| 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_SYSTEM_MEMORY_H_ | 5 #ifndef MOJO_SYSTEM_MEMORY_H_ |
| 6 #define MOJO_SYSTEM_MEMORY_H_ | 6 #define MOJO_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()|. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 template <size_t size, size_t alignment> | 39 template <size_t size, size_t alignment> |
| 40 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount(const void* pointer, | 40 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithCount(const void* pointer, |
| 41 size_t count); | 41 size_t count); |
| 42 | 42 |
| 43 // Checks (insofar as appropriate/possible) that |pointer| is a valid pointer to | 43 // Checks (insofar as appropriate/possible) that |pointer| is a valid pointer to |
| 44 // a buffer of the given size and alignment (both in bytes). | 44 // a buffer of the given size and alignment (both in bytes). |
| 45 template <size_t alignment> | 45 template <size_t alignment> |
| 46 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize(const void* pointer, | 46 void MOJO_SYSTEM_IMPL_EXPORT CheckUserPointerWithSize(const void* pointer, |
| 47 size_t size); | 47 size_t size); |
| 48 | 48 |
| 49 // TODO(vtl): Delete all the |Verify...()| things (and replace them with | |
| 50 // |Check...()|. | |
| 51 template <size_t size, size_t alignment> | |
| 52 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerHelper(const void* pointer); | |
| 53 | |
| 54 // Note: This is also used by options_validation.h. | |
| 55 template <size_t size, size_t alignment> | |
| 56 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithCountHelper( | |
| 57 const void* pointer, | |
| 58 size_t count); | |
| 59 | |
| 60 } // namespace internal | 49 } // namespace internal |
| 61 | 50 |
| 62 // Forward declarations so that they can be friended. | 51 // Forward declarations so that they can be friended. |
| 63 template <typename Type> class UserPointerReader; | 52 template <typename Type> class UserPointerReader; |
| 64 template <typename Type> class UserPointerWriter; | 53 template <typename Type> class UserPointerWriter; |
| 65 template <typename Type> class UserPointerReaderWriter; | 54 template <typename Type> class UserPointerReaderWriter; |
| 66 template <class Options> class UserOptionsReader; | 55 template <class Options> class UserOptionsReader; |
| 67 | 56 |
| 68 // Verify (insofar as possible/necessary) that a |T| can be read from the user | |
| 69 // |pointer|. | |
| 70 template <typename T> | |
| 71 bool VerifyUserPointer(const T* pointer) { | |
| 72 return internal::VerifyUserPointerHelper<sizeof(T), MOJO_ALIGNOF(T)>(pointer); | |
| 73 } | |
| 74 | |
| 75 // Verify (insofar as possible/necessary) that |count| |T|s can be read from the | |
| 76 // user |pointer|; |count| may be zero. (This is done carefully since |count * | |
| 77 // sizeof(T)| may overflow a |size_t|.) | |
| 78 template <typename T> | |
| 79 bool VerifyUserPointerWithCount(const T* pointer, size_t count) { | |
| 80 return internal::VerifyUserPointerWithCountHelper<sizeof(T), | |
| 81 MOJO_ALIGNOF(T)>(pointer, | |
| 82 count); | |
| 83 } | |
| 84 | |
| 85 // Verify that |size| bytes (which may be zero) can be read from the user | |
| 86 // |pointer|, and that |pointer| has the specified |alignment| (if |size| is | |
| 87 // nonzero). | |
| 88 template <size_t alignment> | |
| 89 bool MOJO_SYSTEM_IMPL_EXPORT VerifyUserPointerWithSize(const void* pointer, | |
| 90 size_t size); | |
| 91 | |
| 92 // Provides a convenient way to implicitly get null |UserPointer<Type>|s. | 57 // Provides a convenient way to implicitly get null |UserPointer<Type>|s. |
| 93 struct NullUserPointer {}; | 58 struct NullUserPointer {}; |
| 94 | 59 |
| 95 // Represents a user pointer to a single |Type| (which must be POD), for Mojo | 60 // Represents a user pointer to a single |Type| (which must be POD), for Mojo |
| 96 // primitive parameters. | 61 // primitive parameters. |
| 97 // | 62 // |
| 98 // Use a const |Type| for in parameters, and non-const |Type|s for out and | 63 // Use a const |Type| for in parameters, and non-const |Type|s for out and |
| 99 // in-out parameters (in which case the |Put()| method is available). | 64 // in-out parameters (in which case the |Put()| method is available). |
| 100 template <typename Type> | 65 template <typename Type> |
| 101 class UserPointer { | 66 class UserPointer { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 sizeof(NonVoidType), MOJO_ALIGNOF(NonVoidType)>(pointer_, count); | 172 sizeof(NonVoidType), MOJO_ALIGNOF(NonVoidType)>(pointer_, count); |
| 208 memcpy(pointer_, source, count * sizeof(NonVoidType)); | 173 memcpy(pointer_, source, count * sizeof(NonVoidType)); |
| 209 } | 174 } |
| 210 | 175 |
| 211 // Gets a |UserPointer| at offset |i| (in |Type|s) relative to this. | 176 // Gets a |UserPointer| at offset |i| (in |Type|s) relative to this. |
| 212 UserPointer At(size_t i) const { | 177 UserPointer At(size_t i) const { |
| 213 return UserPointer(static_cast<Type*>( | 178 return UserPointer(static_cast<Type*>( |
| 214 static_cast<NonVoidType*>(pointer_) + i)); | 179 static_cast<NonVoidType*>(pointer_) + i)); |
| 215 } | 180 } |
| 216 | 181 |
| 182 // Gets the value of the |UserPointer| as a |uintptr_t|. This should not be |
| 183 // casted back to a pointer (and dereferenced), but may be used as a key for |
| 184 // lookup or passed back to the user. |
| 185 uintptr_t GetPointerValue() const { |
| 186 return reinterpret_cast<uintptr_t>(pointer_); |
| 187 } |
| 188 |
| 217 // These provides safe (read-only/write-only/read-and-write) access to a | 189 // These provides safe (read-only/write-only/read-and-write) access to a |
| 218 // |UserPointer<Type>| (probably pointing to an array) using just an ordinary | 190 // |UserPointer<Type>| (probably pointing to an array) using just an ordinary |
| 219 // pointer (obtained via |GetPointer()|). | 191 // pointer (obtained via |GetPointer()|). |
| 220 // | 192 // |
| 221 // The memory returned by |GetPointer()| may be a copy of the original user | 193 // The memory returned by |GetPointer()| may be a copy of the original user |
| 222 // memory, but should be modified only if the user is intended to eventually | 194 // memory, but should be modified only if the user is intended to eventually |
| 223 // see the change.) If any changes are made, |Commit()| should be called to | 195 // see the change.) If any changes are made, |Commit()| should be called to |
| 224 // guarantee that the changes are written back to user memory (it may be | 196 // guarantee that the changes are written back to user memory (it may be |
| 225 // called multiple times). | 197 // called multiple times). |
| 226 // | 198 // |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 } | 340 } |
| 369 | 341 |
| 370 private: | 342 private: |
| 371 UserPointer<Type> user_pointer_; | 343 UserPointer<Type> user_pointer_; |
| 372 size_t count_; | 344 size_t count_; |
| 373 scoped_ptr<Type[]> buffer_; | 345 scoped_ptr<Type[]> buffer_; |
| 374 | 346 |
| 375 DISALLOW_COPY_AND_ASSIGN(UserPointerReaderWriter); | 347 DISALLOW_COPY_AND_ASSIGN(UserPointerReaderWriter); |
| 376 }; | 348 }; |
| 377 | 349 |
| 378 // Represents a user pointer that will never be dereferenced by the system. The | |
| 379 // pointer value (i.e., the address) may be as a key for lookup, or may be a | |
| 380 // value that is only passed to the user at some point. | |
| 381 template <typename Type> | |
| 382 class UserPointerValue { | |
| 383 public: | |
| 384 UserPointerValue() : pointer_() {} | |
| 385 explicit UserPointerValue(Type* pointer) : pointer_(pointer) {} | |
| 386 ~UserPointerValue() {} | |
| 387 | |
| 388 // Returns the *value* of the pointer, which shouldn't be cast back to a | |
| 389 // pointer and dereferenced. | |
| 390 uintptr_t GetValue() const { | |
| 391 return reinterpret_cast<uintptr_t>(pointer_); | |
| 392 } | |
| 393 | |
| 394 private: | |
| 395 Type* pointer_; | |
| 396 // Allow copy and assignment. | |
| 397 }; | |
| 398 | |
| 399 // Provides a convenient way to make a |UserPointerValue<Type>|. | |
| 400 template <typename Type> | |
| 401 inline UserPointerValue<Type> MakeUserPointerValue(Type* pointer) { | |
| 402 return UserPointerValue<Type>(pointer); | |
| 403 } | |
| 404 | |
| 405 } // namespace system | 350 } // namespace system |
| 406 } // namespace mojo | 351 } // namespace mojo |
| 407 | 352 |
| 408 #endif // MOJO_SYSTEM_MEMORY_H_ | 353 #endif // MOJO_SYSTEM_MEMORY_H_ |
| OLD | NEW |