| 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // when you'd use the rvalue |*user_pointer|, but be aware that this may be | 122 // when you'd use the rvalue |*user_pointer|, but be aware that this may be |
| 123 // costly -- so if the value will be used multiple times, you should save it. | 123 // costly -- so if the value will be used multiple times, you should save it. |
| 124 // | 124 // |
| 125 // (We want to force a copy here, so return |Type| not |const Type&|.) | 125 // (We want to force a copy here, so return |Type| not |const Type&|.) |
| 126 Type Get() const { | 126 Type Get() const { |
| 127 internal::CheckUserPointerHelper<sizeof(Type), | 127 internal::CheckUserPointerHelper<sizeof(Type), |
| 128 MOJO_ALIGNOF(Type)>(pointer_); | 128 MOJO_ALIGNOF(Type)>(pointer_); |
| 129 return *pointer_; | 129 return *pointer_; |
| 130 } | 130 } |
| 131 | 131 |
| 132 // TODO(vtl): Add a |GetArray()| method (see |PutArray()|). | 132 // Gets an array (of type |Type|, or just a buffer if |Type| is |void| or |
| 133 // |const void|) of |count| elements (or bytes if |Type| is |void| or |const |
| 134 // void|) from the location pointed to by this user pointer. Use this when |
| 135 // you'd do something like |memcpy(destination, user_pointer, count * |
| 136 // sizeof(Type)|. |
| 137 void GetArray(typename internal::remove_const<Type>::type* destination, |
| 138 size_t count) { |
| 139 internal::CheckUserPointerWithCountHelper< |
| 140 sizeof(NonVoidType), MOJO_ALIGNOF(NonVoidType)>(pointer_, count); |
| 141 memcpy(destination, pointer_, count * sizeof(NonVoidType)); |
| 142 } |
| 133 | 143 |
| 134 // Puts a value (of type |Type|, or of type |char| if |Type| is |void|) to the | 144 // Puts a value (of type |Type|, or of type |char| if |Type| is |void|) to the |
| 135 // location pointed to by this user pointer. Use this when you'd use the | 145 // location pointed to by this user pointer. Use this when you'd use the |
| 136 // lvalue |*user_pointer|. Since this may be costly, you should avoid using | 146 // lvalue |*user_pointer|. Since this may be costly, you should avoid using |
| 137 // this (for the same user pointer) more than once. | 147 // this (for the same user pointer) more than once. |
| 138 // | 148 // |
| 139 // Note: This |Put()| method is not valid when |T| is const, e.g., |const | 149 // Note: This |Put()| method is not valid when |T| is const, e.g., |const |
| 140 // uint32_t|, but it's okay to include them so long as this template is only | 150 // uint32_t|, but it's okay to include them so long as this template is only |
| 141 // implicitly instantiated (see 14.7.1 of the C++11 standard) and not | 151 // implicitly instantiated (see 14.7.1 of the C++11 standard) and not |
| 142 // explicitly instantiated. (On implicit instantiation, only the declarations | 152 // explicitly instantiated. (On implicit instantiation, only the declarations |
| 143 // need be valid, not the definitions.) | 153 // need be valid, not the definitions.) |
| 144 // | 154 // |
| 145 // If |Type| is |void|, we "convert" it to |char|, so that it makes sense. | 155 // If |Type| is |void|, we "convert" it to |char|, so that it makes sense. |
| 146 // (Otherwise, we'd need a suitable specialization to exclude |Put()|.) | 156 // (Otherwise, we'd need a suitable specialization to exclude |Put()|.) |
| 147 // | 157 // |
| 148 // In C++11, we could do something like: | 158 // In C++11, we could do something like: |
| 149 // template <typename _Type = Type> | 159 // template <typename _Type = Type> |
| 150 // typename enable_if<!is_const<_Type>::value && | 160 // typename enable_if<!is_const<_Type>::value && |
| 151 // !is_void<_Type>::value>::type Put( | 161 // !is_void<_Type>::value>::type Put( |
| 152 // const _Type& value) { ... } | 162 // const _Type& value) { ... } |
| 153 // (which obviously be correct), but C++03 doesn't allow default function | 163 // (which obviously be correct), but C++03 doesn't allow default function |
| 154 // template arguments. | 164 // template arguments. |
| 155 void Put(const NonVoidType& value) { | 165 void Put(const NonVoidType& value) { |
| 156 internal::CheckUserPointerHelper<sizeof(NonVoidType), | 166 internal::CheckUserPointerHelper<sizeof(NonVoidType), |
| 157 MOJO_ALIGNOF(NonVoidType)>(pointer_); | 167 MOJO_ALIGNOF(NonVoidType)>(pointer_); |
| 158 *pointer_ = value; | 168 *pointer_ = value; |
| 159 } | 169 } |
| 160 | 170 |
| 161 // Puts an array (of type |Type|, or just a buffer if |Type| is |void|) and | 171 // Puts an array (of type |Type|, or just a buffer if |Type| is |void|) with |
| 162 // size |count| (number of elements, or number of bytes if |Type| is |void|) | 172 // |count| elements (or bytes |Type| is |void|) to the location pointed to by |
| 163 // to the location pointed to by this user pointer. Use this when you'd do | 173 // this user pointer. Use this when you'd do something like |
| 164 // something like |memcpy(user_pointer, source, count * sizeof(Type))|. | 174 // |memcpy(user_pointer, source, count * sizeof(Type))|. |
| 165 // | 175 // |
| 166 // Note: The same comments about the validity of |Put()| (except for the part | 176 // Note: The same comments about the validity of |Put()| (except for the part |
| 167 // about |void|) apply here. | 177 // about |void|) apply here. |
| 168 void PutArray(const Type* source, size_t count) { | 178 void PutArray(const Type* source, size_t count) { |
| 169 internal::CheckUserPointerWithCountHelper<sizeof(NonVoidType), | 179 internal::CheckUserPointerWithCountHelper< |
| 170 MOJO_ALIGNOF(NonVoidType)>(source, | 180 sizeof(NonVoidType), MOJO_ALIGNOF(NonVoidType)>(pointer_, count); |
| 171 count); | |
| 172 memcpy(pointer_, source, count * sizeof(NonVoidType)); | 181 memcpy(pointer_, source, count * sizeof(NonVoidType)); |
| 173 } | 182 } |
| 174 | 183 |
| 175 // Gets a |UserPointer| at offset |i| (in |Type|s) relative to this. This | 184 // Gets a |UserPointer| at offset |i| (in |Type|s) relative to this. This |
| 176 // method is not valid if |Type| is |void| (TODO(vtl): Maybe I should make it | 185 // method is not valid if |Type| is |void| (TODO(vtl): Maybe I should make it |
| 177 // valid, with the offset in bytes?). | 186 // valid, with the offset in bytes?). |
| 178 UserPointer At(size_t i) const { | 187 UserPointer At(size_t i) const { |
| 179 return UserPointer(pointer_ + i); | 188 return UserPointer(pointer_ + i); |
| 180 } | 189 } |
| 181 | 190 |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 // Provides a convenient way to make a |UserPointerValue<Type>|. | 357 // Provides a convenient way to make a |UserPointerValue<Type>|. |
| 349 template <typename Type> | 358 template <typename Type> |
| 350 inline UserPointerValue<Type> MakeUserPointerValue(Type* pointer) { | 359 inline UserPointerValue<Type> MakeUserPointerValue(Type* pointer) { |
| 351 return UserPointerValue<Type>(pointer); | 360 return UserPointerValue<Type>(pointer); |
| 352 } | 361 } |
| 353 | 362 |
| 354 } // namespace system | 363 } // namespace system |
| 355 } // namespace mojo | 364 } // namespace mojo |
| 356 | 365 |
| 357 #endif // MOJO_SYSTEM_MEMORY_H_ | 366 #endif // MOJO_SYSTEM_MEMORY_H_ |
| OLD | NEW |