pw_malloc#
Replacement interface for standard libc dynamic memory operations
Stable C C++
This module defines an interface for replacing the standard libc dynamic memory operations.
This facade doesn’t implement any heap structure or dynamic memory methods. It
only requires that backends implements pw::malloc::GetSystemAllocator and
optionally pw::malloc::InitSystemAllocator. These functions are called
before static initialization, and are responsible for initializing global data
structures required by the malloc implementation.
The intent of this module is to provide an interface for user-provided dynamic memory operations that is compatible with different implementations.
Setup#
This module requires the following setup:
Choose a
pw_mallocbackend, or write one yourself.Select a backend in your build system. If using GN build, Specify the
pw_malloc_BACKENDGN build arg to point to the library that provides apw_mallocbackend. If using the Bazel build, add the constraint value for the backend library that provides apw_mallocbackend.Add a dependency on the
pw_mallocfacade in your targetsexecutablebuild template, e.g.:
template("platform_executable") {
target("executable", target_name) {
deps = []
config = []
forward_variables_from(invoker, "*")
if (pw_malloc_BACKEND != "") {
deps += [ dir_pw_malloc ]
}
}
}
Compile-time configuration#
This module has configuration options that globally affect the behavior of
pw_malloc via compile-time configuration of this module.
See the module documentation for more details.
Heap initialization#
You can provide a region of memory to use as heap as either a byte span or a
pair of addresses to pw::malloc::InitSystemAllocator.
Usage#
Once the heap is initialized, you may simply use malloc and free as you
would normally, as well as standard library functions like strdup and
built-in routines like new that use those functions.
If you configured a PW_MALLOC_METRICS_TYPE, you can retrieve metrics using
pw::malloc::UpdateSystemMetrics().