tree: 23fb8c24a42c10a560c13c9bfb62f4ddab6817e3 [path history] [tgz]
  1. aligned_memory.cc
  2. aligned_memory.h
  3. aligned_memory_unittest.cc
  4. discardable_memory.cc
  5. discardable_memory.h
  6. discardable_memory_allocator.cc
  7. discardable_memory_allocator.h
  8. discardable_memory_backing_field_trial_unittest.cc
  9. discardable_memory_internal.h
  10. discardable_shared_memory.cc
  11. discardable_shared_memory.h
  12. discardable_shared_memory_unittest.cc
  13. free_deleter.h
  14. madv_free_discardable_memory_allocator_posix.cc
  15. madv_free_discardable_memory_allocator_posix.h
  16. madv_free_discardable_memory_allocator_posix_unittest.cc
  17. madv_free_discardable_memory_posix.cc
  18. madv_free_discardable_memory_posix.h
  19. madv_free_discardable_memory_posix_unittest.cc
  20. memory_pressure_listener.cc
  21. memory_pressure_listener.h
  22. memory_pressure_listener_unittest.cc
  23. memory_pressure_monitor.cc
  24. memory_pressure_monitor.h
  25. MIRACLE_PTR_OWNERS
  26. nonscannable_memory.cc
  27. nonscannable_memory.h
  28. OWNERS
  29. page_size.h
  30. page_size_nacl.cc
  31. page_size_posix.cc
  32. page_size_win.cc
  33. platform_shared_memory_handle.cc
  34. platform_shared_memory_handle.h
  35. platform_shared_memory_mapper.h
  36. platform_shared_memory_mapper_android.cc
  37. platform_shared_memory_mapper_apple.cc
  38. platform_shared_memory_mapper_fuchsia.cc
  39. platform_shared_memory_mapper_posix.cc
  40. platform_shared_memory_mapper_win.cc
  41. platform_shared_memory_region.cc
  42. platform_shared_memory_region.h
  43. platform_shared_memory_region_android.cc
  44. platform_shared_memory_region_apple.cc
  45. platform_shared_memory_region_fuchsia.cc
  46. platform_shared_memory_region_posix.cc
  47. platform_shared_memory_region_unittest.cc
  48. platform_shared_memory_region_win.cc
  49. post_delayed_memory_reduction_task.cc
  50. post_delayed_memory_reduction_task.h
  51. protected_memory.h
  52. protected_memory_nocompile.nc
  53. protected_memory_unittest.cc
  54. protected_memory_win.cc
  55. ptr_util.h
  56. ptr_util_unittest.cc
  57. raw_ptr.h
  58. raw_ptr.md
  59. raw_ptr_asan_bound_arg_tracker.cc
  60. raw_ptr_asan_bound_arg_tracker.h
  61. raw_ptr_asan_hooks.cc
  62. raw_ptr_asan_hooks.h
  63. raw_ptr_asan_service.cc
  64. raw_ptr_asan_service.h
  65. raw_ptr_asan_unittest.cc
  66. raw_ptr_cast.h
  67. raw_ptr_chromium_unittest.cc
  68. raw_ptr_exclusion.h
  69. raw_ptr_liveness.dot
  70. raw_ptr_liveness.png
  71. raw_ref.h
  72. raw_scoped_refptr_mismatch_checker.h
  73. raw_span.h
  74. raw_span_unittest.cc
  75. read_only_shared_memory_region.cc
  76. read_only_shared_memory_region.h
  77. README.md
  78. ref_counted.cc
  79. ref_counted.h
  80. ref_counted_delete_on_sequence.h
  81. ref_counted_memory.cc
  82. ref_counted_memory.h
  83. ref_counted_memory_unittest.cc
  84. ref_counted_nocompile.nc
  85. ref_counted_unittest.cc
  86. rust_cfg_win_unittest.cc
  87. safe_ref.h
  88. safe_ref_traits.h
  89. safe_ref_unittest.cc
  90. safety_checks.h
  91. safety_checks_unittest.cc
  92. scoped_policy.h
  93. scoped_refptr.h
  94. shared_memory_hooks.h
  95. shared_memory_hooks_unittest.cc
  96. shared_memory_mapper.cc
  97. shared_memory_mapper.h
  98. shared_memory_mapping.cc
  99. shared_memory_mapping.h
  100. shared_memory_mapping_unittest.cc
  101. shared_memory_region_unittest.cc
  102. shared_memory_security_policy.cc
  103. shared_memory_security_policy.h
  104. shared_memory_switch.cc
  105. shared_memory_switch.h
  106. shared_memory_switch_unittest.cc
  107. shared_memory_tracker.cc
  108. shared_memory_tracker.h
  109. singleton.h
  110. singleton_unittest.cc
  111. stack_allocated.h
  112. unsafe_shared_memory_pool.cc
  113. unsafe_shared_memory_pool.h
  114. unsafe_shared_memory_pool_unittest.cc
  115. unsafe_shared_memory_region.cc
  116. unsafe_shared_memory_region.h
  117. values_equivalent.h
  118. values_equivalent_unittest.cc
  119. weak_auto_reset.h
  120. weak_auto_reset_unittest.cc
  121. weak_ptr.cc
  122. weak_ptr.h
  123. weak_ptr_nocompile.nc
  124. weak_ptr_unittest.cc
  125. writable_shared_memory_region.cc
  126. writable_shared_memory_region.h
base/memory/README.md

//base/memory Types

Overview

This directory contains a variety of pointer-like objects (aka smart pointers). This is a brief overview of what they are and how they should be used. Refer to individual header files for details. C++ is not memory safe, so use these types to help guard against potential memory bugs. There are other pointer-like object types implemented elsewhere that may be right for a given use case, such as absl::optional<T> and std::unique_ptr<T>. More on all types in video form here and in a doc here.

raw_ptr<T>

Use for class fields/members that would otherwise be a T*.

This is a weakly refcounted wrapper for a T* (also called a raw pointer). When the object is deleted, the allocator will “poison” the memory that object occupied and keep the memory around so it’s not reused. This reduces the risk and impact of a use-after-free bug.

Depending on the use case, it's possible a smart pointer with additional features would be more appropriate, but if none of those are applicable or necessary, raw_ptr<T> is preferred over a T*.

For more information, see raw_ptr.md; for guidance on usage, see the style guide.

raw_ref<T>

Use for class fields/members that would otherwise be a T&.

This shares much in common with raw_ptr<T>, but asserts that the raw_ref<T> is not nullable.

For more information, see raw_ptr.md; for guidance on usage, see the style guide.

base::WeakPtr<T>

Use when a reference to an object might outlive the object itself.

These are useful for asynchronous work, which is common in Chrome. If an async task references other objects or state, and it's possible for that state to be destroyed before the task runs, those references should be held in a WeakPtr<T>. Each WeakPtr<T> is associated with a WeakPtrFactory<T>. When the associated factory (usually owned by T) is destroyed, all WeakPtr<T> are invalidated (becomes null) rather than becoming use-after-frees. If such references should never outlive the object, consider using SafeRef instead.

base::SafeRef<T>

Use to express that a reference to an object must not outlive the object.

An example is if you have a class member that you want to guarantee outlives the class itself. SafeRef automatically enforces the lifetime assumptions and eliminates the need for validity checks.

If the assumption that the object is valid is broken, then the process terminates safely and generates a crash report. Though not ideal, it's preferable to a potentially undiscovered security bug.

This type is built on top of WeakPtr, so if you want a SafeRef<T>, T needs a WeakPtrFactory as a member. It works like WeakPtr, but doesn‘t allow for a null state. There’s also overlap with raw_ptr, though this was implemented first.

scoped_refptr<T>

Use when you want manually managed strong refcounting. Use carefully!

It’s an owning smart pointer, so it owns a pointer to something allocated in the heap and gives shared ownership of the underlying object, since it can be copied. When all scoped_refptr<T>s pointing to the same object are gone, that object gets destroyed.

This is Chrome's answer to std::shared_ptr<T>. It additionally requires T to inherit from RefCounted or RefCountedThreadSafe, since the ref counting happens in the object itself, unlike shared_ptr<T>.

It's preferred for an object to remain on the same thread, as RefCounted is much cheaper. If there are scoped_refptr<T>s to the same object on different threads, use RefCountedThreadSafe, since accesses to the reference count can race. In this case, without external synchronization, the destructor of scoped_refptr<T>, which decreases the reference count by one, can run on any thread.

Inheriting from RefCountedThreadSafe by itself doesn't make a class T or the underlying object of scoped_refptr<T> thread-safe: It merely ensures that the counter manipulated by scoped_refptr<T> is thread-safe.

If the destructor interacts with other systems it is important to control and know which thread has the last reference to the object, or you can end up with flakiness.