• Home
Name Date Size #Lines LOC

..--

README.mdD06-Sep-20244.3 KiB9372

aligned_memory.ccD06-Sep-20241.7 KiB5333

aligned_memory.hD06-Sep-20242.2 KiB8647

aligned_memory_unittest.ccD06-Sep-20242.7 KiB9163

discardable_memory.ccD06-Sep-20244.4 KiB12886

discardable_memory.hD06-Sep-20243.4 KiB8628

discardable_memory_allocator.ccD06-Sep-20241.4 KiB5533

discardable_memory_allocator.hD06-Sep-20242.7 KiB7228

discardable_memory_backing_field_trial_unittest.ccD06-Sep-20243.7 KiB9271

discardable_memory_internal.hD06-Sep-20241.9 KiB5426

discardable_shared_memory.ccD06-Sep-202421.1 KiB591394

discardable_shared_memory.hD06-Sep-20248.2 KiB20175

discardable_shared_memory_unittest.ccD06-Sep-202415.6 KiB485301

free_deleter.hD06-Sep-2024661 2611

madv_free_discardable_memory_allocator_posix.ccD06-Sep-20242.5 KiB7355

madv_free_discardable_memory_allocator_posix.hD06-Sep-20241.7 KiB5536

madv_free_discardable_memory_allocator_posix_unittest.ccD06-Sep-20244 KiB10879

madv_free_discardable_memory_posix.ccD06-Sep-202411.5 KiB352259

madv_free_discardable_memory_posix.hD06-Sep-20244.9 KiB13455

madv_free_discardable_memory_posix_unittest.ccD06-Sep-20244.8 KiB14296

memory_pressure_listener.ccD06-Sep-20245.6 KiB165122

memory_pressure_listener.hD06-Sep-20244.4 KiB11843

memory_pressure_listener_unittest.ccD06-Sep-20243 KiB8260

memory_pressure_monitor.ccD06-Sep-2024675 3420

memory_pressure_monitor.hD06-Sep-20241.6 KiB4822

nonscannable_memory.ccD06-Sep-20241.2 KiB5037

nonscannable_memory.hD06-Sep-20241.5 KiB4117

page_size.hD06-Sep-2024629 238

page_size_nacl.ccD06-Sep-2024338 178

page_size_posix.ccD06-Sep-2024613 2514

page_size_win.ccD06-Sep-2024382 167

platform_shared_memory_handle.ccD06-Sep-2024757 2714

platform_shared_memory_handle.hD06-Sep-20242.1 KiB7250

platform_shared_memory_mapper.hD06-Sep-2024983 2915

platform_shared_memory_mapper_android.ccD06-Sep-20241.2 KiB4024

platform_shared_memory_mapper_apple.ccD06-Sep-20241.5 KiB4634

platform_shared_memory_mapper_fuchsia.ccD06-Sep-20241.2 KiB4129

platform_shared_memory_mapper_posix.ccD06-Sep-2024992 3724

platform_shared_memory_mapper_win.ccD06-Sep-20241.7 KiB5741

platform_shared_memory_region.ccD06-Sep-20243.2 KiB10572

platform_shared_memory_region.hD06-Sep-20249.7 KiB244114

platform_shared_memory_region_android.ccD06-Sep-20245.1 KiB187132

platform_shared_memory_region_apple.ccD06-Sep-20246 KiB200150

platform_shared_memory_region_fuchsia.ccD06-Sep-20245.1 KiB177128

platform_shared_memory_region_posix.ccD06-Sep-20248.8 KiB309231

platform_shared_memory_region_unittest.ccD06-Sep-202416.7 KiB440345

platform_shared_memory_region_win.ccD06-Sep-20248.3 KiB271191

ptr_util.hD06-Sep-2024613 2310

ptr_util_unittest.ccD06-Sep-2024871 4125

raw_ptr.hD06-Sep-2024528 144

raw_ptr.mdD06-Sep-202429.9 KiB755609

raw_ptr_asan_bound_arg_tracker.ccD06-Sep-20241.9 KiB7147

raw_ptr_asan_bound_arg_tracker.hD06-Sep-20244.4 KiB12466

raw_ptr_asan_hooks.ccD06-Sep-20243.9 KiB12987

raw_ptr_asan_hooks.hD06-Sep-2024564 2310

raw_ptr_asan_service.ccD06-Sep-202414.4 KiB367294

raw_ptr_asan_service.hD06-Sep-20242.8 KiB10272

raw_ptr_asan_unittest.ccD06-Sep-202416.6 KiB454292

raw_ptr_cast.hD06-Sep-2024548 144

raw_ptr_chromium_unittest.ccD06-Sep-2024410 133

raw_ptr_exclusion.hD06-Sep-2024568 144

raw_ptr_liveness.dotD06-Sep-20241.7 KiB4738

raw_ptr_liveness.pngD06-Sep-202437.5 KiB

raw_ref.hD06-Sep-2024528 144

raw_scoped_refptr_mismatch_checker.hD06-Sep-20241.8 KiB5022

raw_span.hD06-Sep-20241.1 KiB3415

raw_span_unittest.ccD06-Sep-20241.5 KiB5033

read_only_shared_memory_region.ccD06-Sep-20243 KiB10576

read_only_shared_memory_region.hD06-Sep-20246 KiB15163

ref_counted.ccD06-Sep-20243.1 KiB10359

ref_counted.hD06-Sep-202414.3 KiB475284

ref_counted_delete_on_sequence.hD06-Sep-20242.7 KiB9153

ref_counted_memory.ccD06-Sep-20243.4 KiB12381

ref_counted_memory.hD06-Sep-20246.2 KiB200112

ref_counted_memory_unittest.ccD06-Sep-20243.1 KiB11579

ref_counted_nocompile.ncD06-Sep-20243.2 KiB11081

ref_counted_unittest.ccD06-Sep-202423.6 KiB774562

rust_cfg_win_unittest.ccD06-Sep-20241 KiB3219

safe_ref.hD06-Sep-20246 KiB17492

safe_ref_traits.hD06-Sep-2024560 2313

safe_ref_unittest.ccD06-Sep-20249.9 KiB304258

safety_checks.hD06-Sep-20249.6 KiB242169

safety_checks_unittest.ccD06-Sep-20245.4 KiB169109

scoped_policy.hD06-Sep-2024649 2611

scoped_refptr.hD06-Sep-202412.5 KiB422245

shared_memory_hooks.hD06-Sep-20241.4 KiB4630

shared_memory_hooks_unittest.ccD06-Sep-20242.4 KiB8665

shared_memory_mapper.ccD06-Sep-2024428 188

shared_memory_mapper.hD06-Sep-20241.6 KiB4520

shared_memory_mapping.ccD06-Sep-20243.5 KiB9975

shared_memory_mapping.hD06-Sep-20249 KiB250157

shared_memory_mapping_unittest.ccD06-Sep-20245.9 KiB182125

shared_memory_region_unittest.ccD06-Sep-202410.1 KiB273213

shared_memory_security_policy.ccD06-Sep-20243.1 KiB9454

shared_memory_security_policy.hD06-Sep-20241.4 KiB4523

shared_memory_tracker.ccD06-Sep-20244.8 KiB136107

shared_memory_tracker.hD06-Sep-20242.7 KiB8449

singleton.hD06-Sep-202410.3 KiB28590

singleton_unittest.ccD06-Sep-20247.7 KiB303232

stack_allocated.hD06-Sep-20242.3 KiB6021

unsafe_shared_memory_pool.ccD06-Sep-20243.2 KiB10882

unsafe_shared_memory_pool.hD06-Sep-20242.9 KiB8648

unsafe_shared_memory_pool_unittest.ccD06-Sep-20241.5 KiB5135

unsafe_shared_memory_region.ccD06-Sep-20242.3 KiB8559

unsafe_shared_memory_region.hD06-Sep-20245.3 KiB13450

values_equivalent.hD06-Sep-20242.3 KiB7030

values_equivalent_unittest.ccD06-Sep-20243.8 KiB11792

weak_auto_reset.hD06-Sep-20242.3 KiB7237

weak_auto_reset_unittest.ccD06-Sep-20249.2 KiB296197

weak_ptr.ccD06-Sep-20243.2 KiB11982

weak_ptr.hD06-Sep-202416.5 KiB485235

weak_ptr_nocompile.ncD06-Sep-20245 KiB116101

weak_ptr_unittest.ccD06-Sep-202427.1 KiB908639

writable_shared_memory_region.ccD06-Sep-20242.9 KiB10070

writable_shared_memory_region.hD06-Sep-20245.9 KiB14557

README.md

1# //base/memory Types
2
3## Overview
4This directory contains a variety of pointer-like objects (aka smart pointers).
5This is a brief overview of what they are and how they should be used. Refer to
6individual header files for details. C++ is not memory safe, so use these types
7to help guard against potential memory bugs.
8There are other pointer-like object types implemented elsewhere that may be
9right for a given use case, such as `absl::optional<T>` and
10`std::unique_ptr<T>`. More on all types in video form
11[here](https://youtu.be/MpwbWSEDfjM?t=582s) and in a doc
12[here](https://docs.google.com/document/d/1VRevv8JhlP4I8fIlvf87IrW2IRjE0PbkSfIcI6-UbJo/edit?usp=sharing).
13
14## `raw_ptr<T>`
15Use for class fields/members that would otherwise be a `T*`.
16
17This is a weakly refcounted wrapper for a `T*` (also called a raw
18pointer). When the object is deleted, the allocator will "poison" the memory
19that object occupied and keep the memory around so it’s not reused. This reduces
20the risk and impact of a use-after-free bug.
21
22Depending on the use case, it's possible a smart pointer with additional
23features would be more appropriate, but if none of those are applicable or
24necessary, `raw_ptr<T>` is preferred over a `T*`.
25
26For more information, see [`raw_ptr.md`](./raw_ptr.md); for guidance on
27usage, see
28[the style guide](../../styleguide/c++/c++.md#non_owning-pointers-in-class-fields).
29
30## `raw_ref<T>`
31Use for class fields/members that would otherwise be a `T&`.
32
33This shares much in common with `raw_ptr<T>`, but asserts that the
34`raw_ref<T>` is not nullable.
35
36For more information, see [`raw_ptr.md`](./raw_ptr.md); for guidance on
37usage, see
38[the style guide](../../styleguide/c++/c++.md#non_owning-pointers-in-class-fields).
39
40## `base::WeakPtr<T>`
41Use when a reference to an object might outlive the object itself.
42
43These are useful for asynchronous work, which is common in Chrome. If an async
44task references other objects or state, and it's possible for that state to be
45destroyed before the task runs, those references should be held in a
46`WeakPtr<T>`. Each `WeakPtr<T>` is associated with a `WeakPtrFactory<T>`. When
47the associated factory (usually owned by T) is destroyed, all `WeakPtr<T>` are
48invalidated (becomes null) rather than becoming use-after-frees. If such
49references should never outlive the object, consider using SafeRef instead.
50
51## `base::SafeRef<T>`
52Use to express that a reference to an object must not outlive the object.
53
54An example is if you have a class member that you want to guarantee outlives the
55class itself. SafeRef automatically enforces the lifetime assumptions and
56eliminates the need for validity checks.
57
58If the assumption that the object is valid is broken, then the process
59terminates safely and generates a crash report. Though not ideal, it's
60preferable to a potentially undiscovered security bug.
61
62This type is built on top of WeakPtr, so if you want a `SafeRef<T>`, T needs a
63WeakPtrFactory as a member. It works like `WeakPtr`, but doesn't allow for a
64null state. There's also overlap with `raw_ptr`, though this was implemented
65first.
66
67## `scoped_refptr<T>`
68Use when you want manually managed strong refcounting. Use carefully!
69
70It’s an owning smart pointer, so it owns a pointer to something allocated in the
71heap and gives shared ownership of the underlying object, since it can be
72copied. When all `scoped_refptr<T>`s pointing to the same object are gone, that
73object gets destroyed.
74
75This is Chrome's answer to `std::shared_ptr<T>`. It additionally requires T to
76inherit from `RefCounted` or `RefCountedThreadSafe`, since the ref counting
77happens in the object itself, unlike `shared_ptr<T>`.
78
79It's preferred for an object to remain on the same thread, as `RefCounted` is
80much cheaper. If there are `scoped_refptr<T>`s to the same object on different
81threads, use `RefCountedThreadSafe`, since accesses to the reference count can
82race. In this case, without external synchronization, the destructor of
83`scoped_refptr<T>`, which decreases the reference count by one, can run on any
84thread.
85
86Inheriting from `RefCountedThreadSafe` by itself doesn't make a class `T` or the
87underlying object of `scoped_refptr<T>` thread-safe: It merely ensures that the
88counter manipulated by `scoped_refptr<T>` is thread-safe.
89
90If the destructor interacts with other systems it is important to
91control and know which thread has the last reference to the object, or you can
92end up with flakiness.
93