• Home
Name Date Size #Lines LOC

..--

README.mdD03-May-20244.1 KiB8567

aligned_memory.ccD03-May-20241.7 KiB5333

aligned_memory.hD03-May-20242.1 KiB8447

aligned_memory_unittest.ccD03-May-20242.5 KiB8558

discardable_memory.ccD03-May-20244.4 KiB12886

discardable_memory.hD03-May-20243.4 KiB8628

discardable_memory_allocator.ccD03-May-20241.4 KiB5533

discardable_memory_allocator.hD03-May-20242.7 KiB7228

discardable_memory_backing_field_trial_unittest.ccD03-May-20243.7 KiB9271

discardable_memory_internal.hD03-May-20241.9 KiB5426

discardable_shared_memory.ccD03-May-202421.1 KiB591394

discardable_shared_memory.hD03-May-20248.2 KiB20175

discardable_shared_memory_unittest.ccD03-May-202415 KiB485301

free_deleter.hD03-May-2024661 2611

madv_free_discardable_memory_allocator_posix.ccD03-May-20242.5 KiB7355

madv_free_discardable_memory_allocator_posix.hD03-May-20241.7 KiB5536

madv_free_discardable_memory_allocator_posix_unittest.ccD03-May-20244 KiB10879

madv_free_discardable_memory_posix.ccD03-May-202411.5 KiB352259

madv_free_discardable_memory_posix.hD03-May-20244.9 KiB13455

madv_free_discardable_memory_posix_unittest.ccD03-May-20244.8 KiB14296

memory_pressure_listener.ccD03-May-20245.6 KiB165122

memory_pressure_listener.hD03-May-20244.4 KiB11843

memory_pressure_listener_unittest.ccD03-May-20243 KiB8260

memory_pressure_monitor.ccD03-May-2024675 3420

memory_pressure_monitor.hD03-May-20241.6 KiB4822

nonscannable_memory.ccD03-May-20244.1 KiB127102

nonscannable_memory.hD03-May-20243.5 KiB11067

page_size.hD03-May-2024629 238

page_size_nacl.ccD03-May-2024338 178

page_size_posix.ccD03-May-2024613 2514

page_size_win.ccD03-May-2024382 167

platform_shared_memory_handle.ccD03-May-2024757 2714

platform_shared_memory_handle.hD03-May-20242.1 KiB7250

platform_shared_memory_mapper.hD03-May-2024983 2915

platform_shared_memory_mapper_android.ccD03-May-20241.2 KiB4024

platform_shared_memory_mapper_fuchsia.ccD03-May-20241.2 KiB4129

platform_shared_memory_mapper_mac.ccD03-May-20241.5 KiB4634

platform_shared_memory_mapper_posix.ccD03-May-2024992 3724

platform_shared_memory_mapper_win.ccD03-May-20241.7 KiB5741

platform_shared_memory_region.ccD03-May-20242.9 KiB9464

platform_shared_memory_region.hD03-May-20249.5 KiB240113

platform_shared_memory_region_android.ccD03-May-20245.1 KiB187132

platform_shared_memory_region_fuchsia.ccD03-May-20245.1 KiB177128

platform_shared_memory_region_mac.ccD03-May-20246.1 KiB203151

platform_shared_memory_region_posix.ccD03-May-20248.8 KiB309231

platform_shared_memory_region_unittest.ccD03-May-202416.7 KiB440345

platform_shared_memory_region_win.ccD03-May-20248.2 KiB271191

ptr_util.hD03-May-2024613 2310

ptr_util_unittest.ccD03-May-2024871 4125

raw_ptr.hD03-May-2024508 144

raw_ptr.mdD03-May-202425 KiB618504

raw_ptr_asan_bound_arg_tracker.ccD03-May-20241.9 KiB7147

raw_ptr_asan_bound_arg_tracker.hD03-May-20244.3 KiB12466

raw_ptr_asan_hooks.ccD03-May-20243.8 KiB12383

raw_ptr_asan_hooks.hD03-May-2024544 2310

raw_ptr_asan_service.ccD03-May-202414.4 KiB367294

raw_ptr_asan_service.hD03-May-20242.8 KiB10272

raw_ptr_asan_unittest.ccD03-May-202416.6 KiB454292

raw_ptr_exclusion.hD03-May-2024548 144

raw_ref.hD03-May-2024508 144

raw_scoped_refptr_mismatch_checker.hD03-May-20242.1 KiB5628

read_only_shared_memory_region.ccD03-May-20243 KiB10576

read_only_shared_memory_region.hD03-May-20246 KiB15163

ref_counted.ccD03-May-20243.1 KiB10359

ref_counted.hD03-May-202414.3 KiB476284

ref_counted_delete_on_sequence.hD03-May-20242.7 KiB9153

ref_counted_memory.ccD03-May-20243.4 KiB12381

ref_counted_memory.hD03-May-20246.2 KiB199110

ref_counted_memory_unittest.ccD03-May-20243.1 KiB11579

ref_counted_unittest.ccD03-May-202423.6 KiB774562

ref_counted_unittest.ncD03-May-20244.1 KiB148109

rust_cfg_win_unittest.ccD03-May-20241 KiB3219

safe_ref.hD03-May-20245.5 KiB16688

safe_ref_unittest.ccD03-May-20249.1 KiB284240

scoped_policy.hD03-May-2024649 2611

scoped_refptr.hD03-May-202412.9 KiB436253

shared_memory_hooks.hD03-May-20241.4 KiB4630

shared_memory_hooks_unittest.ccD03-May-20242.4 KiB8665

shared_memory_mapper.ccD03-May-2024428 188

shared_memory_mapper.hD03-May-20241.6 KiB4520

shared_memory_mapping.ccD03-May-20243.5 KiB9975

shared_memory_mapping.hD03-May-20249 KiB250157

shared_memory_mapping_unittest.ccD03-May-20245.9 KiB182125

shared_memory_region_unittest.ccD03-May-202410.1 KiB273213

shared_memory_security_policy.ccD03-May-20243.1 KiB9454

shared_memory_security_policy.hD03-May-20241.4 KiB4523

shared_memory_tracker.ccD03-May-20244.8 KiB136107

shared_memory_tracker.hD03-May-20242.7 KiB8449

singleton.hD03-May-202410.3 KiB28590

singleton_unittest.ccD03-May-20247.7 KiB303232

stack_allocated.hD03-May-20242.3 KiB5820

unsafe_shared_memory_pool.ccD03-May-20243.2 KiB10882

unsafe_shared_memory_pool.hD03-May-20242.9 KiB8648

unsafe_shared_memory_pool_unittest.ccD03-May-20241.5 KiB5135

unsafe_shared_memory_region.ccD03-May-20242.3 KiB8559

unsafe_shared_memory_region.hD03-May-20245.3 KiB13450

values_equivalent.hD03-May-20242.3 KiB7030

values_equivalent_unittest.ccD03-May-20243.8 KiB11792

weak_auto_reset.hD03-May-20242.3 KiB7237

weak_auto_reset_unittest.ccD03-May-20249.2 KiB296197

weak_ptr.ccD03-May-20242.9 KiB10873

weak_ptr.hD03-May-202416 KiB466222

weak_ptr_unittest.ccD03-May-202427.1 KiB908639

weak_ptr_unittest.ncD03-May-20245.3 KiB158115

writable_shared_memory_region.ccD03-May-20242.9 KiB10070

writable_shared_memory_region.hD03-May-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## `base::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`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>`. It's preferred for an
78object to remain on the same thread, as `RefCounted` is much cheaper. If there
79are `scoped_refptr`s to the same object on different threads, use
80`RefCountedThreadSafe`, since accesses to the reference count can race.
81In this case, without external synchronization, the destructor can run on any
82thread. If the destructor interacts with other systems it is important to
83control and know which thread has the last reference to the object, or you can
84end up with flakiness.
85