xref: /aosp_15_r20/external/pigweed/pw_allocator/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_allocator:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker============
4*61c4878aSAndroid Build Coastguard Workerpw_allocator
5*61c4878aSAndroid Build Coastguard Worker============
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_allocator
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker- **Flexible**: Simple interface makes it easy to inject specific behaviors.
10*61c4878aSAndroid Build Coastguard Worker- **Safe**: Can detect memory corruption, e.g overflows and use-after-free.
11*61c4878aSAndroid Build Coastguard Worker- **Measurable**: Pick what allocations you want to track and measure.
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/basic.cc
14*61c4878aSAndroid Build Coastguard Worker   :language: cpp
15*61c4878aSAndroid Build Coastguard Worker   :linenos:
16*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-basic-allocate]
17*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-basic-allocate]
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker   // Any of these allocators can be passed to the routine above.
22*61c4878aSAndroid Build Coastguard Worker   WithBuffer<LastFitBlockAllocator<uint32_t>, 0x1000> block_allocator;
23*61c4878aSAndroid Build Coastguard Worker   LibCAllocator libc_allocator;
24*61c4878aSAndroid Build Coastguard Worker   TrackingAllocator tracker(libc_allocator);
25*61c4878aSAndroid Build Coastguard Worker   SynchronizedAllocator synced(*block_allocator);
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard WorkerDynamically allocate without giving up control! Pigweed's allocators let you
29*61c4878aSAndroid Build Coastguard Workereasily combine allocator features for your needs, without extra code size or
30*61c4878aSAndroid Build Coastguard Workerperformance penalties for those you don't. Complex projects in particular can
31*61c4878aSAndroid Build Coastguard Workerbenefit from dynamic allocation through simplified code, improved memory usage,
32*61c4878aSAndroid Build Coastguard Workerincreased shared memory, and reduced large reservations.
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker**Want to allocate objects from specific memory like SRAM or PSRAM?**
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard WorkerUse `dependency injection`_! Write your code to take
37*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_allocator-api-allocator` parameters, and you can quickly and
38*61c4878aSAndroid Build Coastguard Workereasily change where memory comes from or what additional features are provided
39*61c4878aSAndroid Build Coastguard Workersimply by changing what allocator is passed:
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/linker_sections.cc
42*61c4878aSAndroid Build Coastguard Worker   :language: cpp
43*61c4878aSAndroid Build Coastguard Worker   :linenos:
44*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-linker_sections-injection]
45*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-linker_sections-injection]
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard WorkerNow you can easily allocate objects in the example above using SRAM, PSRAM, or
48*61c4878aSAndroid Build Coastguard Workerboth:
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/linker_sections.cc
51*61c4878aSAndroid Build Coastguard Worker   :language: cpp
52*61c4878aSAndroid Build Coastguard Worker   :linenos:
53*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-linker_sections-placement]
54*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-linker_sections-placement]
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker**Worried about forgetting to deallocate?**
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard WorkerUse a smart pointer!
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/basic.cc
61*61c4878aSAndroid Build Coastguard Worker   :language: cpp
62*61c4878aSAndroid Build Coastguard Worker   :linenos:
63*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-basic-make_unique]
64*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-basic-make_unique]
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker**Want to know how much memory has been allocated?**
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard WorkerPick the metrics you're interested in and track them with a
69*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_allocator-api-tracking_allocator`:
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/metrics.cc
72*61c4878aSAndroid Build Coastguard Worker   :language: cpp
73*61c4878aSAndroid Build Coastguard Worker   :linenos:
74*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-metrics-custom_metrics1]
75*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-metrics-custom_metrics1]
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/metrics.cc
78*61c4878aSAndroid Build Coastguard Worker   :language: cpp
79*61c4878aSAndroid Build Coastguard Worker   :linenos:
80*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-metrics-custom_metrics2]
81*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-metrics-custom_metrics2]
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Worker**Need to share the allocator with another thread or an interrupt handler?**
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard WorkerUse a :ref:`module-pw_allocator-api-synchronized_allocator` with the lock of
86*61c4878aSAndroid Build Coastguard Workeryour choice:
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/spin_lock.cc
89*61c4878aSAndroid Build Coastguard Worker   :language: cpp
90*61c4878aSAndroid Build Coastguard Worker   :linenos:
91*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_allocator-examples-spin_lock]
92*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_allocator-examples-spin_lock]
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker.. tip:: Check out the :ref:`module-pw_allocator-guides` for even more code
95*61c4878aSAndroid Build Coastguard Worker   samples!
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker--------------------
98*61c4878aSAndroid Build Coastguard WorkerIs it right for you?
99*61c4878aSAndroid Build Coastguard Worker--------------------
100*61c4878aSAndroid Build Coastguard Worker.. rst-class:: key-text
101*61c4878aSAndroid Build Coastguard Worker
102*61c4878aSAndroid Build Coastguard WorkerDoes your project need to use memory...
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard Worker- Without knowing exactly how much ahead of time?
105*61c4878aSAndroid Build Coastguard Worker- From different backing storage, e.g. SRAM vs. PSRAM?
106*61c4878aSAndroid Build Coastguard Worker- Across different tasks using a shared pool?
107*61c4878aSAndroid Build Coastguard Worker- In multiple places, and you need to know how much each place is using?
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard WorkerIf you answered "yes" to any of these questions, ``pw_allocator`` may be able to
110*61c4878aSAndroid Build Coastguard Workerhelp! This module is designed to faciliate dynamic allocation for embedded
111*61c4878aSAndroid Build Coastguard Workerprojects that are sufficiently complex to make static allocation infeasible.
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard WorkerSmaller projects may be able to enumerate their objects and preallocate any
114*61c4878aSAndroid Build Coastguard Workerstorage they may need on device when running, and may be subject to extremely
115*61c4878aSAndroid Build Coastguard Workertight memory constraints. In these cases, ``pw_allocator`` may add more costs in
116*61c4878aSAndroid Build Coastguard Workerterms of code size, overhead, and performance than it provides benefits in terms
117*61c4878aSAndroid Build Coastguard Workerof code simplicity and memory sharing.
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard WorkerAt the other extreme, larger projects may be built on platforms with rich
120*61c4878aSAndroid Build Coastguard Workeroperating system capabilities. On these platforms, the system and language
121*61c4878aSAndroid Build Coastguard Workerruntime may already provide dynamic allocation and memory may be less
122*61c4878aSAndroid Build Coastguard Workerconstrained. In these cases, ``pw_allocator`` may not provide the same
123*61c4878aSAndroid Build Coastguard Workercapabilities as the platform.
124*61c4878aSAndroid Build Coastguard Worker
125*61c4878aSAndroid Build Coastguard WorkerBetween these two is a range of complex projects on RTOSes and other platforms.
126*61c4878aSAndroid Build Coastguard WorkerThese projects may benefit from using the
127*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_allocator-api-allocator` interface and its implementations to
128*61c4878aSAndroid Build Coastguard Workermanage memory.
129*61c4878aSAndroid Build Coastguard Worker
130*61c4878aSAndroid Build Coastguard Worker.. toctree::
131*61c4878aSAndroid Build Coastguard Worker   :hidden:
132*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
133*61c4878aSAndroid Build Coastguard Worker
134*61c4878aSAndroid Build Coastguard Worker   guide
135*61c4878aSAndroid Build Coastguard Worker   api
136*61c4878aSAndroid Build Coastguard Worker   design
137*61c4878aSAndroid Build Coastguard Worker   code_size
138*61c4878aSAndroid Build Coastguard Worker
139*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`rocket` Guides
142*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_allocator-guides
143*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
144*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-primary
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker      Integrate pw_allocator into your project and learn common use cases
147*61c4878aSAndroid Build Coastguard Worker
148*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` API reference
149*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_allocator-api
150*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
151*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard Worker      Detailed description of the pw_allocator's API
154*61c4878aSAndroid Build Coastguard Worker
155*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
156*61c4878aSAndroid Build Coastguard Worker
157*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` Design & roadmap
158*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_allocator-design
159*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
160*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard Worker      Learn why pw_allocator is designed the way it is, and upcoming plans
163*61c4878aSAndroid Build Coastguard Worker
164*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` Code size analysis
165*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_allocator-size-reports
166*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
167*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
168*61c4878aSAndroid Build Coastguard Worker
169*61c4878aSAndroid Build Coastguard Worker      Understand pw_allocator's code footprint and savings potential
170*61c4878aSAndroid Build Coastguard Worker
171*61c4878aSAndroid Build Coastguard Worker.. _dependency injection: https://en.wikipedia.org/wiki/Dependency_injection
172