blob: 5dd37bfefcbecccf08264757214dbd2c74fd28cc [file] [log] [blame] [view]
Kalvin Lee5f8555b2022-05-16 21:30:201# Glossary
2
3This page describes some core terminology used in PartitionAlloc.
4A weak attempt is made to present terms "in conceptual order" s.t.
5each term depends mainly upon previously defined ones.
6
7## Top-Level Terms
8
9* **Partition**: A heap that is separated and protected both from other
10 partitions and from non-PartitionAlloc memory. Each partition holds
11 multiple buckets.
Kalvin Lee8338a462022-07-29 02:19:2512
13*** promo
14**NOTE**: In code (and comments), "partition," "root," and even
15"allocator" are all conceptually the same thing.
16***
17
Kalvin Lee5f8555b2022-05-16 21:30:2018* **Bucket**: A collection of regions in a partition that contains
19 similar-sized objects. For example, one bucket may hold objects of
20 size (224, 256], another (256, 320], etc. Bucket size
21 brackets are geometrically spaced,
22 [going up to `kMaxBucketed`][max-bucket-comment].
23* **Normal Bucket**: Any bucket whose size ceiling does not exceed
24 `kMaxBucketed`. This is the common case in PartitionAlloc, and
25 the "normal" modifier is often dropped in casual reference.
26* **Direct Map (Bucket)**: Any allocation whose size exceeds `kMaxBucketed`.
27
28Buckets consist of slot spans, organized as linked lists (see below).
29
30## Pages
31
32* **System Page**: A memory page defined by the CPU/OS. Commonly
33 referred to as a "virtual page" in other contexts. This is typically
34 4KiB, but it can be larger. PartitionAlloc supports up to 64KiB,
35 though this constant isn't always known at compile time (depending
36 on the OS).
37* **Partition Page**: The most common granularity used by
38 PartitionAlloc. Consists of exactly 4 system pages.
39* **Super Page**: A 2MiB region, aligned on a 2MiB boundary. Not to
40 be confused with OS-level terms like "large page" or "huge page",
41 which are also commonly 2MiB. These have to be fully committed /
42 uncommitted in memory, whereas super pages can be partially committed
43 with system page granularity.
Kalvin Lee8338a462022-07-29 02:19:2544* **Extent**: An extent is a run of consecutive super pages (belonging
45 to a single partition). Extents are to super pages what slot spans are
46 to slots (see below).
Kalvin Lee5f8555b2022-05-16 21:30:2047
48## Slots and Spans
49
50* **Slot**: An indivisible allocation unit. Slot sizes are tied to
51 buckets. For example, each allocation that falls into the bucket
52 (224, 256] would be satisfied with a slot of size 256. This
53 applies only to normal buckets, not to direct map.
54* **Slot Span**: A run of same-sized slots that are contiguous in
55 memory. Slot span size is a multiple of partition page size, but it
56 isn't always a multiple of slot size, although we try hard for this
57 to be the case.
58 * **Small Bucket**: Allocations up to 4 partition pages. In these
59 cases, slot spans are always between 1 and 4 partition pages in
60 size. For each slot span size, the slot span is chosen to minimize
61 number of pages used while keeping the rounding waste under a
62 reasonable limit.
63 * For example, for a slot size 96, 64B waste is deemed acceptable
64 when using a single partition page, but for slot size
65 384, the potential waste of 256B wouldn't be, so 3 partition pages
66 are used to achieve 0B waste.
67 * PartitionAlloc may avoid waste by lowering the number of committed
68 system pages compared to the number of reserved pages. For
69 example, for the slot size of 896B we'd use a slot span of 2
70 partition pages of 16KiB, i.e. 8 system pages of 4KiB, but commit
71 only up to 7, thus resulting in perfect packing.
72 * **Single-Slot Span**: Allocations above 4 partition pages (but
73 ≤`kMaxBucketed`). This is because each slot span is guaranteed to
74 hold exactly one slot.
75 * Fun fact: there are sizes ≤4 partition pages that result in a
76 slot span having exactly 1 slot, but nonetheless they're still
77 classified as small buckets. The reason is that single-slot spans
78 are often handled by a different code path, and that distinction
79 is made purely based on slot size, for simplicity and efficiency.
80
81## Other Terms
82
83* **Object**: A chunk of memory returned to the allocating invoker
84 of the size requested. It doesn't have to span the entire slot,
85 nor does it have to begin at the slot start. This term is commonly
86 used as a parameter name in PartitionAlloc code, as opposed to
87 `slot_start`.
88* **Thread Cache**: A [thread-local structure][pa-thread-cache] that
89 holds some not-too-large memory chunks, ready to be allocated. This
90 speeds up in-thread allocation by reducing a lock hold to a
91 thread-local storage lookup, improving cache locality.
92* **GigaCage**: A memory region several gigabytes wide, reserved by
93 PartitionAlloc upon initialization, from which all allocations are
94 taken. The motivation for GigaCage is for code to be able to examine
95 a pointer and to immediately determine whether or not the memory was
96 allocated by PartitionAlloc. This provides support for a number of
97 features, including
98 [StarScan][starscan-readme] and
99 [BackupRefPtr][brp-doc].
100 * Note that GigaCage only exists in builds with 64-bit pointers.
101 * In builds with 32-bit pointers, PartitionAlloc tracks pointers
102 it dispenses with a bitmap. This is often referred to as "fake
103 GigaCage" (or simply "GigaCage") for lack of a better term.
104* **Payload**: The usable area of a super page in which slot spans
105 reside. While generally this means "everything between the first
106 and last guard partition pages in a super page," the presence of
107 other metadata (e.g. StarScan bitmaps) can bump the starting offset
108 forward. While this term is entrenched in the code, the team
109 considers it suboptimal and is actively looking for a replacement.
Kalvin Leedbbd6e42022-08-09 02:35:37110* **Allocation Fast Path**: A path taken during an allocation that is
111 considered fast. Usually means that an allocation request can be
112 immediately satisfied by grabbing a slot from the freelist of the
113 first active slot span in the bucket.
114* **Allocation Slow Path**: Anything which is not fast (see above).
115 Can involve
116 * finding another active slot span in the list,
117 * provisioning more slots in a slot span,
118 * bringing back a free (or decommitted) slot span,
119 * allocating a new slot span, or even
120 * allocating a new super page.
121
122*** aside
123By "slow" we may mean something as simple as extra logic (`if`
124statements etc.), or something as costly as system calls.
125***
Kalvin Lee5f8555b2022-05-16 21:30:20126
127## PartitionAlloc-Everywhere
128
129Originally, PartitionAlloc was used only in Blink (Chromium's rendering engine).
130It was invoked explicitly, by calling PartitionAlloc APIs directly.
131
132PartitionAlloc-Everywhere is the name of the project that brought PartitionAlloc
133to the entire-ish codebase (exclusions apply). This was done by intercepting
134`malloc()`, `free()`, `realloc()`, aforementioned `posix_memalign()`, etc. and
135routing them into PartitionAlloc. The shim located in
136`base/allocator/allocator_shim_default_dispatch_to_partition_alloc.h` is
137responsible for intercepting. For more details, see
138[base/allocator/README.md](../../../base/allocator/README.md).
139
140A special, catch-it-all *Malloc* partition has been created for the intercepted
141`malloc()` et al. This is to isolate from already existing Blink partitions.
142The only exception from that is Blink's *FastMalloc* partition, which was also
143catch-it-all in nature, so it's perfectly fine to merge these together, to
144minimize fragmentation.
145
146As of 2022, PartitionAlloc-Everywhere is supported on
147
148* Windows 32- and 64-bit
149* Linux
150* Android 32- and 64-bit
151* macOS
152* Fuchsia
153
154[max-bucket-comment]: https://source.chromium.org/chromium/chromium/src/+/main:base/allocator/partition_allocator/partition_alloc_constants.h;l=345;drc=667e6b001f438521e1c1a1bc3eabeead7aaa1f37
155[pa-thread-cache]: https://source.chromium.org/chromium/chromium/src/+/main:base/allocator/partition_allocator/thread_cache.h
156[starscan-readme]: https://chromium.googlesource.com/chromium/src/+/main/base/allocator/partition_allocator/starscan/README.md
157[brp-doc]: https://docs.google.com/document/d/1m0c63vXXLyGtIGBi9v6YFANum7-IRC3-dmiYBCWqkMk/preview