xref: /aosp_15_r20/external/pigweed/pw_malloc/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_malloc:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=========
4*61c4878aSAndroid Build Coastguard Workerpw_malloc
5*61c4878aSAndroid Build Coastguard Worker=========
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_malloc
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerThis module defines an interface for replacing the standard libc dynamic memory
10*61c4878aSAndroid Build Coastguard Workeroperations.
11*61c4878aSAndroid Build Coastguard Worker
12*61c4878aSAndroid Build Coastguard WorkerThis facade doesn't implement any heap structure or dynamic memory methods. It
13*61c4878aSAndroid Build Coastguard Workeronly requires that backends implements ``pw::malloc::GetSystemAllocator`` and
14*61c4878aSAndroid Build Coastguard Workeroptionally ``pw::malloc::InitSystemAllocator``. These functions are called
15*61c4878aSAndroid Build Coastguard Workerbefore static initialization, and are responsible for initializing global data
16*61c4878aSAndroid Build Coastguard Workerstructures required by the ``malloc`` implementation.
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard WorkerThe intent of this module is to provide an interface for user-provided dynamic
19*61c4878aSAndroid Build Coastguard Workermemory operations that is compatible with different implementations.
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker-----
22*61c4878aSAndroid Build Coastguard WorkerSetup
23*61c4878aSAndroid Build Coastguard Worker-----
24*61c4878aSAndroid Build Coastguard WorkerThis module requires the following setup:
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard Worker1. Choose a ``pw_malloc`` backend, or write one yourself.
27*61c4878aSAndroid Build Coastguard Worker2. Select a backend in your build system. If using GN build, Specify the
28*61c4878aSAndroid Build Coastguard Worker   ``pw_malloc_BACKEND`` GN build arg to point to the library that provides a
29*61c4878aSAndroid Build Coastguard Worker   ``pw_malloc`` backend. If using the Bazel build, add the constraint value for
30*61c4878aSAndroid Build Coastguard Worker   the backend library that provides a ``pw_malloc`` backend.
31*61c4878aSAndroid Build Coastguard Worker3. Add a dependency on the ``pw_malloc`` facade in your targets ``executable``
32*61c4878aSAndroid Build Coastguard Worker   build template, e.g.:
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker.. code-block::
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker   template("platform_executable") {
37*61c4878aSAndroid Build Coastguard Worker      target("executable", target_name) {
38*61c4878aSAndroid Build Coastguard Worker         deps = []
39*61c4878aSAndroid Build Coastguard Worker         config = []
40*61c4878aSAndroid Build Coastguard Worker         forward_variables_from(invoker, "*")
41*61c4878aSAndroid Build Coastguard Worker         if (pw_malloc_BACKEND != "") {
42*61c4878aSAndroid Build Coastguard Worker            deps += [ dir_pw_malloc ]
43*61c4878aSAndroid Build Coastguard Worker         }
44*61c4878aSAndroid Build Coastguard Worker      }
45*61c4878aSAndroid Build Coastguard Worker   }
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard WorkerCompile-time configuration
48*61c4878aSAndroid Build Coastguard Worker==========================
49*61c4878aSAndroid Build Coastguard WorkerThis module has configuration options that globally affect the behavior of
50*61c4878aSAndroid Build Coastguard Worker``pw_malloc`` via compile-time configuration of this module.
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_MALLOC_LOCK_TYPE
53*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_MALLOC_METRICS_TYPE
54*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_MALLOC_BLOCK_OFFSET_TYPE
55*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_MALLOC_MIN_BUCKET_SIZE
56*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_MALLOC_NUM_BUCKETS
57*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD
58*61c4878aSAndroid Build Coastguard Worker
59*61c4878aSAndroid Build Coastguard WorkerSee the
60*61c4878aSAndroid Build Coastguard Worker:ref:`module documentation <module-structure-compile-time-configuration>` for
61*61c4878aSAndroid Build Coastguard Workermore details.
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard WorkerHeap initialization
64*61c4878aSAndroid Build Coastguard Worker===================
65*61c4878aSAndroid Build Coastguard WorkerYou can provide a region of memory to use as heap as either a byte span or a
66*61c4878aSAndroid Build Coastguard Workerpair of addresses to ``pw::malloc::InitSystemAllocator``.
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker-----
69*61c4878aSAndroid Build Coastguard WorkerUsage
70*61c4878aSAndroid Build Coastguard Worker-----
71*61c4878aSAndroid Build Coastguard WorkerOnce the heap is initialized, you may simply use ``malloc`` and ``free`` as you
72*61c4878aSAndroid Build Coastguard Workerwould normally, as well as standard library functions like ``strdup`` and
73*61c4878aSAndroid Build Coastguard Workerbuilt-in routines like ``new`` that use those functions.
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard WorkerIf you configured a ``PW_MALLOC_METRICS_TYPE``, you can retrieve metrics using
76*61c4878aSAndroid Build Coastguard Worker``pw::malloc::GetSystemMetrics()``.
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard Worker.. toctree::
79*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
80*61c4878aSAndroid Build Coastguard Worker   :hidden:
81*61c4878aSAndroid Build Coastguard Worker
82*61c4878aSAndroid Build Coastguard Worker   Backends <backends>
83