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/memory/scoped_ptr.h" | 12 #include <memory> |
| 13 |
13 #include "mojo/edk/system/system_impl_export.h" | 14 #include "mojo/edk/system/system_impl_export.h" |
14 #include "mojo/public/c/system/macros.h" | 15 #include "mojo/public/c/system/macros.h" |
15 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 namespace system { | 19 namespace system { |
19 | 20 |
20 namespace internal { | 21 namespace internal { |
21 | 22 |
22 // Removes |const| from |T| (available as |remove_const<T>::type|): | 23 // Removes |const| from |T| (available as |remove_const<T>::type|): |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 return; | 299 return; |
299 | 300 |
300 if (check) { | 301 if (check) { |
301 internal::CheckUserPointerWithCount<sizeof(Type), MOJO_ALIGNOF(Type)>( | 302 internal::CheckUserPointerWithCount<sizeof(Type), MOJO_ALIGNOF(Type)>( |
302 user_pointer, count); | 303 user_pointer, count); |
303 } | 304 } |
304 buffer_.reset(new TypeNoConst[count]); | 305 buffer_.reset(new TypeNoConst[count]); |
305 memcpy(buffer_.get(), user_pointer, count * sizeof(Type)); | 306 memcpy(buffer_.get(), user_pointer, count * sizeof(Type)); |
306 } | 307 } |
307 | 308 |
308 scoped_ptr<TypeNoConst[]> buffer_; | 309 std::unique_ptr<TypeNoConst[]> buffer_; |
309 | 310 |
310 MOJO_DISALLOW_COPY_AND_ASSIGN(UserPointerReader); | 311 MOJO_DISALLOW_COPY_AND_ASSIGN(UserPointerReader); |
311 }; | 312 }; |
312 | 313 |
313 // Implementation of |UserPointer<Type>::Writer|. | 314 // Implementation of |UserPointer<Type>::Writer|. |
314 template <typename Type> | 315 template <typename Type> |
315 class UserPointerWriter { | 316 class UserPointerWriter { |
316 public: | 317 public: |
317 // Note: If |count| is zero, |GetPointer()| will always return null. | 318 // Note: If |count| is zero, |GetPointer()| will always return null. |
318 UserPointerWriter(UserPointer<Type> user_pointer, size_t count) | 319 UserPointerWriter(UserPointer<Type> user_pointer, size_t count) |
319 : user_pointer_(user_pointer), count_(count) { | 320 : user_pointer_(user_pointer), count_(count) { |
320 if (count_ > 0) { | 321 if (count_ > 0) { |
321 buffer_.reset(new Type[count_]); | 322 buffer_.reset(new Type[count_]); |
322 memset(buffer_.get(), 0, count_ * sizeof(Type)); | 323 memset(buffer_.get(), 0, count_ * sizeof(Type)); |
323 } | 324 } |
324 } | 325 } |
325 | 326 |
326 Type* GetPointer() const { return buffer_.get(); } | 327 Type* GetPointer() const { return buffer_.get(); } |
327 | 328 |
328 void Commit() { | 329 void Commit() { |
329 internal::CheckUserPointerWithCount<sizeof(Type), MOJO_ALIGNOF(Type)>( | 330 internal::CheckUserPointerWithCount<sizeof(Type), MOJO_ALIGNOF(Type)>( |
330 user_pointer_.pointer_, count_); | 331 user_pointer_.pointer_, count_); |
331 memcpy(user_pointer_.pointer_, buffer_.get(), count_ * sizeof(Type)); | 332 memcpy(user_pointer_.pointer_, buffer_.get(), count_ * sizeof(Type)); |
332 } | 333 } |
333 | 334 |
334 private: | 335 private: |
335 UserPointer<Type> user_pointer_; | 336 UserPointer<Type> user_pointer_; |
336 size_t count_; | 337 size_t count_; |
337 scoped_ptr<Type[]> buffer_; | 338 std::unique_ptr<Type[]> buffer_; |
338 | 339 |
339 MOJO_DISALLOW_COPY_AND_ASSIGN(UserPointerWriter); | 340 MOJO_DISALLOW_COPY_AND_ASSIGN(UserPointerWriter); |
340 }; | 341 }; |
341 | 342 |
342 // Implementation of |UserPointer<Type>::ReaderWriter|. | 343 // Implementation of |UserPointer<Type>::ReaderWriter|. |
343 template <typename Type> | 344 template <typename Type> |
344 class UserPointerReaderWriter { | 345 class UserPointerReaderWriter { |
345 public: | 346 public: |
346 // Note: If |count| is zero, |GetPointer()| will always return null. | 347 // Note: If |count| is zero, |GetPointer()| will always return null. |
347 UserPointerReaderWriter(UserPointer<Type> user_pointer, size_t count) | 348 UserPointerReaderWriter(UserPointer<Type> user_pointer, size_t count) |
(...skipping 11 matching lines...) Expand all Loading... |
359 | 360 |
360 void Commit() { | 361 void Commit() { |
361 internal::CheckUserPointerWithCount<sizeof(Type), MOJO_ALIGNOF(Type)>( | 362 internal::CheckUserPointerWithCount<sizeof(Type), MOJO_ALIGNOF(Type)>( |
362 user_pointer_.pointer_, count_); | 363 user_pointer_.pointer_, count_); |
363 memcpy(user_pointer_.pointer_, buffer_.get(), count_ * sizeof(Type)); | 364 memcpy(user_pointer_.pointer_, buffer_.get(), count_ * sizeof(Type)); |
364 } | 365 } |
365 | 366 |
366 private: | 367 private: |
367 UserPointer<Type> user_pointer_; | 368 UserPointer<Type> user_pointer_; |
368 size_t count_; | 369 size_t count_; |
369 scoped_ptr<Type[]> buffer_; | 370 std::unique_ptr<Type[]> buffer_; |
370 | 371 |
371 MOJO_DISALLOW_COPY_AND_ASSIGN(UserPointerReaderWriter); | 372 MOJO_DISALLOW_COPY_AND_ASSIGN(UserPointerReaderWriter); |
372 }; | 373 }; |
373 | 374 |
374 } // namespace system | 375 } // namespace system |
375 } // namespace mojo | 376 } // namespace mojo |
376 | 377 |
377 #endif // MOJO_EDK_SYSTEM_MEMORY_H_ | 378 #endif // MOJO_EDK_SYSTEM_MEMORY_H_ |
OLD | NEW |