Title: Theory / Important / Chrome / C++ / Pointers

Typ: raw_ptr<T>
Meaning: Similar to T*, but:
* when memory is freed, it gets poisoned, so each use after free fail
* memory is freed when last raw_ptr<T> to this adress is deletede
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md

Instruction: RAW_PTR_EXCLUSION T* var;
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Meaning: Same as T* var; and also note this can't be transformed into a raw_ptr<T>
Language: C++

Question: In which folder raw_ptr<T> can't be used?
Answer: /renderer/ and renderer only code
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Answer2: path that can not depend on //base
Answer3: //ppapi
Answer4: Other explicitly ignored path from tools/clang/rewrite_raw_ptr_fields/manual-paths-to-ignore.txt

Question: When should you use RAW_PTR_EXCLUSION
Answer: In directories that can't use raw_ptr
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Answer2: when raw_ptr wouldn't compile
Answer3: When pointer always points outside of PartitionAlloc (e.g. literals, stack allocated memory, shared memory, mmap'ed memory, V8/Oilpan/Java heaps, TLS, etc.).
Answer4: (Very rare) cases that cause regression on perf bots.
Answer5: (Very rare) cases where raw_ptr<T> can lead to runtime errors. Make sure to look at the “Extra pointer rules” section before resorting to this exclusion.

Question: When can pointer be used instead of raw_ptr<T> without RAW_PTR_EXCLUSION
Answer: function pointer
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Answer2: ObjC pointers
Answer3: const char* and other string types, when only pointing to string literal
Answer4: for values in the stack
Answer5: For pointers that are only used as keys to map
Answer6: In union type

Question: Does raw_ptr have overhead for initialization
Answer: yes
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome

Question: Does raw_ptr have overhead for destruction
Answer: yes
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome

Question: Does raw_ptr have overhead for assignment to the pointer
Answer: yes
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome

Question: Does raw_ptr have overhead for dereferencing
Answer: no
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome

Question: Does raw_ptr have overhead for extracting a pointer
Answer: no
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome

Question: what is the size of raw_ptr<T>
Answer: Same as T*
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome

Question: What causes the overhead of raw_ptr<T>
Answer: null assignment on initialization
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Answer2: on access, checking whether the memory acceeded is protected

Question: Why could raw_ptr<T> break the compilation
Answer: Pointer fields in classes/structs that are used as global or static variables
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Answer2: Pointer fields that require non-null, constexpr initialization
Answer3: Pointer fields in classes/structs that have to be trivially constructible or destructible

Question: What are the potential values of a raw_ptr<T>
Answer: nullptr
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Answer2: a valid pointer
Answer3: the end of a valid allocation
Answer4: last page of the address space, e.g. for sentinels, like reintepret_cast<void*>(-1)

Instruction: p.get()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/raw_ptr.md
Context: Chrome
Returns: the T* to the same address
Variables: raw_ptr<T> p;

Typ: raw_ref<T>
Meaning: Same to T& but fail if accessing a freed reference
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/
Language: C++
Context: Chrome

Typ: base::WeakPtr<T>
Meaning: A T* to use when the reference to the object may outlives the object itself
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/
Language: C++
Context: Chrome

Typ: base::SafeRef<T>
Meaning: A T& that crashes if the referenced object is deallocated while the reference exists
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/
Language: C++
Context: Chrome

Typ: base::scoped_refptr<T>
Meaning: Same as std::shared_ptr<T>, and require T to inherits from RefCounted or RefCountedThreadSafe
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/
Language: C++
Context: Chrome

Question: Constraint on base::WeakPtrFactory<T>
Answer: It should be the last member variable of a class declaration
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Construction: Ensure WeakPtrs are invalidated before its members variable's destructors are executed

Typ: std::WeakPtrFactory<T>
Meaning: An object that can create WeakPtr<T>. When this object is destroyed, all WeakPtr are destroyed too
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome

Instruction: std::WeakPtrFactory<T> ptr{object}
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Returns: A factory for WeakPtr<T> to object

Question: How should base::WeakPtrFactory<T> be used
Answer: Last variable member of class T. Then T creates the WeakPtr and send them to any class needing pointers
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome

Instruction: factory.GetWeakPtr()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Returns: A WeakPtr<T> to object
Variables: base::WeakPtrFactory<T> factory {object}

Instruction: factory.GetMutableWeakPtr()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Returns: A mutable WeakPtr<T> to object
Variables: base::WeakPtrFactory<T> factory {object}

Instruction: factory.GetSafeRef()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Returns: A SafeRef<T> to object
Variables: base::WeakPtrFactory<T> factory {object}

Instruction: factory.InvalidateWeakPtrs()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Effect: Invalidate all WeakPtrs created by this factory
Context: Chrome
Variables: base::WeakPtrFactory<T> factory {object}

Instruction: factory.HasWeakPtrs()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Returns: Whether there still exists any weak pointers generated by this factory
Variables: base::WeakPtrFactory<T> factory {object}

Instruction: object.AsWeakPtr()
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Effect: A weak pointer to object
Context: Chrome
Variables: base::SupportsWeakPtr<T> object;

Typ: SupportsWeakPtr
Meaning: A class that, when extended, allow to get weak pointer to this.
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Assuming: Must only be used in synchrone call. Since the factory is destroyed after the derived class, this could create sublte use-after-destroy issues otherwise

Instruction: AsWeakPtr(object)
Page: https://chromium.googlesource.com/chromium/src/+/HEAD/base/memory/weak_ptr.h
Context: Chrome
Returns: A weak pointer to object
Assuming: object extends, even indirectly, SupportsWeakPtr