Name Date Size #Lines LOC

..--

README.mdH A D25-Apr-20254.3 KiB9372

aligned_memory.ccH A D25-Apr-20251.7 KiB5534

aligned_memory.hH A D25-Apr-20252.5 KiB9147

aligned_memory_unittest.ccH A D25-Apr-20252.7 KiB9163

discardable_memory.ccH A D25-Apr-20254.4 KiB12886

discardable_memory.hH A D25-Apr-20253.4 KiB8628

discardable_memory_allocator.ccH A D25-Apr-20251.4 KiB5533

discardable_memory_allocator.hH A D25-Apr-20252.7 KiB7228

discardable_memory_backing_field_trial_unittest.ccH A D25-Apr-20253.7 KiB9271

discardable_memory_internal.hH A D25-Apr-20251.9 KiB5426

discardable_shared_memory.ccH A D25-Apr-202521 KiB592394

discardable_shared_memory.hH A D25-Apr-20258.2 KiB20175

discardable_shared_memory_unittest.ccH A D25-Apr-202515.6 KiB485301

free_deleter.hH A D25-Apr-2025661 2611

madv_free_discardable_memory_allocator_posix.ccH A D25-Apr-20252.5 KiB7355

madv_free_discardable_memory_allocator_posix.hH A D25-Apr-20251.7 KiB5536

madv_free_discardable_memory_allocator_posix_unittest.ccH A D25-Apr-20254 KiB10879

madv_free_discardable_memory_posix.ccH A D25-Apr-202511.5 KiB352259

madv_free_discardable_memory_posix.hH A D25-Apr-20255 KiB13756

madv_free_discardable_memory_posix_unittest.ccH A D25-Apr-20254.8 KiB14296

memory_pressure_listener.ccH A D25-Apr-20255.6 KiB165122

memory_pressure_listener.hH A D25-Apr-20254.4 KiB11843

memory_pressure_listener_unittest.ccH A D25-Apr-20253 KiB8260

memory_pressure_monitor.ccH A D25-Apr-2025675 3420

memory_pressure_monitor.hH A D25-Apr-20251.6 KiB4822

nonscannable_memory.ccH A D25-Apr-20251.2 KiB5037

nonscannable_memory.hH A D25-Apr-20251.5 KiB4117

page_size.hH A D25-Apr-2025629 238

page_size_nacl.ccH A D25-Apr-2025338 178

page_size_posix.ccH A D25-Apr-2025613 2514

page_size_win.ccH A D25-Apr-2025382 167

platform_shared_memory_handle.ccH A D25-Apr-2025757 2714

platform_shared_memory_handle.hH A D25-Apr-20252.1 KiB7250

platform_shared_memory_mapper.hH A D25-Apr-2025979 2915

platform_shared_memory_mapper_android.ccH A D25-Apr-20251.2 KiB4024

platform_shared_memory_mapper_apple.ccH A D25-Apr-20251.7 KiB5035

platform_shared_memory_mapper_fuchsia.ccH A D25-Apr-20251.2 KiB4129

platform_shared_memory_mapper_posix.ccH A D25-Apr-2025985 3724

platform_shared_memory_mapper_win.ccH A D25-Apr-20251.7 KiB5741

platform_shared_memory_region.ccH A D25-Apr-20253.2 KiB10572

platform_shared_memory_region.hH A D25-Apr-20259.7 KiB245114

platform_shared_memory_region_android.ccH A D25-Apr-20255.1 KiB187132

platform_shared_memory_region_apple.ccH A D25-Apr-20256 KiB200150

platform_shared_memory_region_fuchsia.ccH A D25-Apr-20255.1 KiB177128

platform_shared_memory_region_posix.ccH A D25-Apr-20258.8 KiB309231

platform_shared_memory_region_unittest.ccH A D25-Apr-202516.7 KiB441345

platform_shared_memory_region_win.ccH A D25-Apr-20258.2 KiB271191

post_delayed_memory_reduction_task.ccH A D25-Apr-20256.5 KiB183137

post_delayed_memory_reduction_task.hH A D25-Apr-20253.3 KiB9865

protected_memory.hH A D25-Apr-202516.5 KiB425220

protected_memory_nocompile.ncH A D25-Apr-20252.3 KiB5037

protected_memory_unittest.ccH A D25-Apr-20254.8 KiB157116

protected_memory_win.ccH A D25-Apr-20251.5 KiB5436

ptr_util.hH A D25-Apr-2025691 2512

ptr_util_unittest.ccH A D25-Apr-2025871 4125

raw_ptr.hH A D25-Apr-2025526 165

raw_ptr.mdH A D25-Apr-202530.7 KiB768619

raw_ptr_asan_bound_arg_tracker.ccH A D25-Apr-20251.8 KiB7147

raw_ptr_asan_bound_arg_tracker.hH A D25-Apr-20254.3 KiB12466

raw_ptr_asan_hooks.ccH A D25-Apr-20253.9 KiB12987

raw_ptr_asan_hooks.hH A D25-Apr-2025525 2310

raw_ptr_asan_service.ccH A D25-Apr-202514.4 KiB367294

raw_ptr_asan_service.hH A D25-Apr-20252.8 KiB10272

raw_ptr_asan_unittest.ccH A D25-Apr-202516.6 KiB454292

raw_ptr_cast.hH A D25-Apr-2025509 144

raw_ptr_chromium_unittest.ccH A D25-Apr-2025410 133

raw_ptr_exclusion.hH A D25-Apr-2025529 144

raw_ptr_liveness.dotH A D25-Apr-20251.7 KiB4738

raw_ptr_liveness.pngHD25-Apr-202537.5 KiB

raw_ref.hH A D25-Apr-2025526 165

raw_scoped_refptr_mismatch_checker.hH A D25-Apr-20251.8 KiB5022

raw_span.hH A D25-Apr-20251.1 KiB3415

raw_span_unittest.ccH A D25-Apr-20251.5 KiB5033

read_only_shared_memory_region.ccH A D25-Apr-20253 KiB10576

read_only_shared_memory_region.hH A D25-Apr-20256 KiB15163

ref_counted.ccH A D25-Apr-20253.1 KiB10359

ref_counted.hH A D25-Apr-202514.3 KiB475284

ref_counted_delete_on_sequence.hH A D25-Apr-20252.7 KiB9153

ref_counted_memory.ccH A D25-Apr-20253.4 KiB12381

ref_counted_memory.hH A D25-Apr-20256.2 KiB200112

ref_counted_memory_unittest.ccH A D25-Apr-20253.1 KiB11579

ref_counted_nocompile.ncH A D25-Apr-20253.2 KiB11081

ref_counted_unittest.ccH A D25-Apr-202524.3 KiB809589

rust_cfg_win_unittest.ccH A D25-Apr-20251 KiB3219

safe_ref.hH A D25-Apr-20256.2 KiB18298

safe_ref_traits.hH A D25-Apr-2025560 2313

safe_ref_unittest.ccH A D25-Apr-202510 KiB312265

safety_checks.hH A D25-Apr-202512.3 KiB282189

safety_checks_unittest.ccH A D25-Apr-20257.2 KiB220146

scoped_policy.hH A D25-Apr-2025649 2611

scoped_refptr.hH A D25-Apr-202512.2 KiB398223

shared_memory_hooks.hH A D25-Apr-20251.4 KiB4630

shared_memory_hooks_unittest.ccH A D25-Apr-20252.4 KiB8765

shared_memory_mapper.ccH A D25-Apr-2025428 188

shared_memory_mapper.hH A D25-Apr-20251.5 KiB4620

shared_memory_mapping.ccH A D25-Apr-20253.5 KiB9975

shared_memory_mapping.hH A D25-Apr-20259 KiB250157

shared_memory_mapping_unittest.ccH A D25-Apr-20255.9 KiB182125

shared_memory_region_unittest.ccH A D25-Apr-202510.1 KiB273213

shared_memory_security_policy.ccH A D25-Apr-20253 KiB9454

shared_memory_security_policy.hH A D25-Apr-20251.4 KiB4523

shared_memory_switch.ccH A D25-Apr-202514.4 KiB372265

shared_memory_switch.hH A D25-Apr-20253.6 KiB10465

shared_memory_switch_unittest.ccH A D25-Apr-20256.7 KiB175123

shared_memory_tracker.ccH A D25-Apr-20255 KiB141108

shared_memory_tracker.hH A D25-Apr-20252.7 KiB8449

singleton.hH A D25-Apr-202510.3 KiB28590

singleton_unittest.ccH A D25-Apr-20257.7 KiB303232

stack_allocated.hH A D25-Apr-20252.3 KiB6021

unsafe_shared_memory_pool.ccH A D25-Apr-20253.2 KiB10882

unsafe_shared_memory_pool.hH A D25-Apr-20252.9 KiB8648

unsafe_shared_memory_pool_unittest.ccH A D25-Apr-20251.5 KiB5135

unsafe_shared_memory_region.ccH A D25-Apr-20252.3 KiB8559

unsafe_shared_memory_region.hH A D25-Apr-20255.3 KiB13450

values_equivalent.hH A D25-Apr-20252.3 KiB7635

values_equivalent_unittest.ccH A D25-Apr-20253.8 KiB11792

weak_auto_reset.hH A D25-Apr-20252.3 KiB7237

weak_auto_reset_unittest.ccH A D25-Apr-20259.2 KiB296197

weak_ptr.ccH A D25-Apr-20253.2 KiB11982

weak_ptr.hH A D25-Apr-202517.6 KiB519251

weak_ptr_nocompile.ncH A D25-Apr-20255.3 KiB123107

weak_ptr_unittest.ccH A D25-Apr-202528.5 KiB958674

writable_shared_memory_region.ccH A D25-Apr-20252.9 KiB10070

writable_shared_memory_region.hH A D25-Apr-20255.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