• Home
Name Date Size #Lines LOC

..--

README.mdD04-Jul-20254.3 KiB9372

aligned_memory.ccD04-Jul-20251.7 KiB5432

aligned_memory.hD04-Jul-20256.1 KiB17175

aligned_memory_unittest.ccD04-Jul-20253.5 KiB11585

asan_interface.hD04-Jul-2025628 199

discardable_memory.ccD04-Jul-20253.7 KiB11576

discardable_memory.hD04-Jul-20253.4 KiB8628

discardable_memory_allocator.ccD04-Jul-20251.4 KiB5533

discardable_memory_allocator.hD04-Jul-20252.7 KiB7228

discardable_memory_backing_field_trial_unittest.ccD04-Jul-20253.7 KiB9271

discardable_memory_internal.hD04-Jul-20252.4 KiB6735

discardable_shared_memory.ccD04-Jul-202519.3 KiB558371

discardable_shared_memory.hD04-Jul-20258.6 KiB21382

discardable_shared_memory_unittest.ccD04-Jul-202515.6 KiB488302

free_deleter.hD04-Jul-2025661 2611

madv_free_discardable_memory_allocator_posix.ccD04-Jul-20252.5 KiB7355

madv_free_discardable_memory_allocator_posix.hD04-Jul-20251.7 KiB5536

madv_free_discardable_memory_allocator_posix_unittest.ccD04-Jul-20254 KiB10879

madv_free_discardable_memory_posix.ccD04-Jul-202511.3 KiB347253

madv_free_discardable_memory_posix.hD04-Jul-20255 KiB13756

madv_free_discardable_memory_posix_unittest.ccD04-Jul-20254.8 KiB14296

memory_pressure_listener.ccD04-Jul-20255.6 KiB165122

memory_pressure_listener.hD04-Jul-20254.4 KiB11843

memory_pressure_listener_unittest.ccD04-Jul-20253 KiB8260

memory_pressure_monitor.ccD04-Jul-2025675 3420

memory_pressure_monitor.hD04-Jul-20251.6 KiB4822

page_size.hD04-Jul-2025629 238

page_size_nacl.ccD04-Jul-2025338 178

page_size_posix.ccD04-Jul-2025613 2514

page_size_win.ccD04-Jul-2025382 167

platform_shared_memory_handle.ccD04-Jul-2025757 2714

platform_shared_memory_handle.hD04-Jul-20252.1 KiB7250

platform_shared_memory_mapper.hD04-Jul-2025979 2915

platform_shared_memory_mapper_android.ccD04-Jul-20251.3 KiB4527

platform_shared_memory_mapper_apple.ccD04-Jul-20251.7 KiB5035

platform_shared_memory_mapper_fuchsia.ccD04-Jul-20251.4 KiB4632

platform_shared_memory_mapper_posix.ccD04-Jul-20251.1 KiB4227

platform_shared_memory_mapper_win.ccD04-Jul-20251.8 KiB6143

platform_shared_memory_region.ccD04-Jul-20253.2 KiB10572

platform_shared_memory_region.hD04-Jul-20259.7 KiB246115

platform_shared_memory_region_android.ccD04-Jul-20255.1 KiB187132

platform_shared_memory_region_apple.ccD04-Jul-20256 KiB200150

platform_shared_memory_region_fuchsia.ccD04-Jul-20255.1 KiB177128

platform_shared_memory_region_posix.ccD04-Jul-20258.8 KiB309231

platform_shared_memory_region_unittest.ccD04-Jul-202517 KiB447347

platform_shared_memory_region_win.ccD04-Jul-20258.2 KiB271191

post_delayed_memory_reduction_task.ccD04-Jul-20256.5 KiB184138

post_delayed_memory_reduction_task.hD04-Jul-20253.3 KiB9865

protected_memory.hD04-Jul-202522.2 KiB534273

protected_memory_nocompile.ncD04-Jul-20251.5 KiB4331

protected_memory_posix.ccD04-Jul-20252.2 KiB7353

protected_memory_unittest.ccD04-Jul-20254.5 KiB148108

protected_memory_win.ccD04-Jul-20251.5 KiB5739

ptr_util.hD04-Jul-2025691 2512

ptr_util_unittest.ccD04-Jul-2025871 4125

raw_ptr.hD04-Jul-2025489 144

raw_ptr.mdD04-Jul-202531.1 KiB770619

raw_ptr_asan_bound_arg_tracker.ccD04-Jul-20251.8 KiB7046

raw_ptr_asan_bound_arg_tracker.hD04-Jul-20254.3 KiB12466

raw_ptr_asan_hooks.ccD04-Jul-20253.9 KiB12987

raw_ptr_asan_hooks.hD04-Jul-2025515 2310

raw_ptr_asan_service.ccD04-Jul-202514.5 KiB371296

raw_ptr_asan_service.hD04-Jul-20252.8 KiB10272

raw_ptr_asan_unittest.ccD04-Jul-202516.6 KiB454292

raw_ptr_cast.hD04-Jul-2025509 144

raw_ptr_chromium_unittest.ccD04-Jul-2025410 133

raw_ptr_exclusion.hD04-Jul-2025529 144

raw_ptr_liveness.dotD04-Jul-20251.7 KiB4738

raw_ptr_liveness.pngD04-Jul-202537.5 KiB

raw_ref.hD04-Jul-2025489 144

raw_scoped_refptr_mismatch_checker.hD04-Jul-20251.8 KiB4921

raw_span.hD04-Jul-20251.1 KiB3516

raw_span_unittest.ccD04-Jul-20252 KiB6642

read_only_shared_memory_region.ccD04-Jul-20253 KiB10576

read_only_shared_memory_region.hD04-Jul-20256 KiB15264

ref_counted.ccD04-Jul-20253.1 KiB10359

ref_counted.hD04-Jul-202514.2 KiB474283

ref_counted_delete_on_sequence.hD04-Jul-20252.7 KiB9153

ref_counted_memory.ccD04-Jul-20252.6 KiB8959

ref_counted_memory.hD04-Jul-20256.8 KiB200100

ref_counted_memory_unittest.ccD04-Jul-20252.6 KiB9765

ref_counted_nocompile.ncD04-Jul-20253.2 KiB11081

ref_counted_unittest.ccD04-Jul-202524.3 KiB809589

rust_cfg_win_unittest.ccD04-Jul-20251 KiB3219

safe_ref.hD04-Jul-20256.2 KiB18298

safe_ref_traits.hD04-Jul-2025560 2313

safe_ref_unittest.ccD04-Jul-202510 KiB312265

safety_checks.hD04-Jul-202513.3 KiB316184

safety_checks_unittest.ccD04-Jul-20257.9 KiB239161

scoped_policy.hD04-Jul-2025649 2611

scoped_refptr.hD04-Jul-202512.4 KiB404228

shared_memory_hooks.hD04-Jul-20251.4 KiB4630

shared_memory_hooks_unittest.ccD04-Jul-20252.4 KiB8765

shared_memory_mapper.ccD04-Jul-2025428 188

shared_memory_mapper.hD04-Jul-20251.5 KiB4620

shared_memory_mapping.ccD04-Jul-20253.6 KiB10478

shared_memory_mapping.hD04-Jul-202511.6 KiB311175

shared_memory_mapping_nocompile.ncD04-Jul-202512 KiB159137

shared_memory_mapping_unittest.ccD04-Jul-20256.8 KiB210143

shared_memory_region_unittest.ccD04-Jul-202510.7 KiB283214

shared_memory_safety_checker.hD04-Jul-20252.5 KiB7140

shared_memory_security_policy.ccD04-Jul-20253 KiB9454

shared_memory_security_policy.hD04-Jul-20251.4 KiB4523

shared_memory_switch.ccD04-Jul-202514.4 KiB372265

shared_memory_switch.hD04-Jul-20253.6 KiB10465

shared_memory_switch_unittest.ccD04-Jul-20256.7 KiB175123

shared_memory_tracker.ccD04-Jul-20255 KiB140107

shared_memory_tracker.hD04-Jul-20252.7 KiB8449

singleton.hD04-Jul-202510.3 KiB28590

singleton_unittest.ccD04-Jul-20257.7 KiB303232

stack_allocated.hD04-Jul-20252.3 KiB6021

structured_shared_memory.hD04-Jul-202510.3 KiB287150

structured_shared_memory_nocompile.ncD04-Jul-20251,016 3322

structured_shared_memory_unittest.ccD04-Jul-20255.8 KiB150110

unsafe_shared_memory_pool.ccD04-Jul-20253.2 KiB10882

unsafe_shared_memory_pool.hD04-Jul-20252.9 KiB8649

unsafe_shared_memory_pool_unittest.ccD04-Jul-20251.5 KiB5135

unsafe_shared_memory_region.ccD04-Jul-20252.3 KiB8559

unsafe_shared_memory_region.hD04-Jul-20255.3 KiB13551

values_equivalent.hD04-Jul-20252.3 KiB7635

values_equivalent_unittest.ccD04-Jul-20253.8 KiB11792

weak_auto_reset.hD04-Jul-20252.3 KiB7237

weak_auto_reset_unittest.ccD04-Jul-20259.2 KiB296197

weak_ptr.ccD04-Jul-20253.2 KiB11982

weak_ptr.hD04-Jul-202514.2 KiB430207

weak_ptr_nocompile.ncD04-Jul-20252.2 KiB6353

weak_ptr_unittest.ccD04-Jul-202527.5 KiB926648

writable_shared_memory_region.ccD04-Jul-20252.9 KiB10070

writable_shared_memory_region.hD04-Jul-20256 KiB14658

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 `std::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