1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_multibuf: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker=========== 4*61c4878aSAndroid Build Coastguard Workerpw_multibuf 5*61c4878aSAndroid Build Coastguard Worker=========== 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_multibuf 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard WorkerSending or receiving messages via RPC, transfer, or sockets often requires a 10*61c4878aSAndroid Build Coastguard Workerseries of intermediate buffers, each requiring their own copy of the data. 11*61c4878aSAndroid Build Coastguard Worker``pw_multibuf`` allows data to be written *once*, eliminating the memory, CPU 12*61c4878aSAndroid Build Coastguard Workerand latency overhead of copying. 13*61c4878aSAndroid Build Coastguard Worker 14*61c4878aSAndroid Build Coastguard Worker----------------- 15*61c4878aSAndroid Build Coastguard WorkerHow does it work? 16*61c4878aSAndroid Build Coastguard Worker----------------- 17*61c4878aSAndroid Build Coastguard Worker``pw_multibuf`` uses several techniques to minimize copying of data: 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker- **Header and Footer Reservation**: Lower-level components can reserve space 20*61c4878aSAndroid Build Coastguard Worker within a buffer for headers and/or footers. This allows headers and footers 21*61c4878aSAndroid Build Coastguard Worker to be added to user-provided data without moving users' data. 22*61c4878aSAndroid Build Coastguard Worker- **Native Scatter/Gather and Fragmentation Support**: Buffers can refer to 23*61c4878aSAndroid Build Coastguard Worker multiple separate chunks of memory. Messages can be built up from 24*61c4878aSAndroid Build Coastguard Worker discontiguous allocations, and users' data can be fragmented across multiple 25*61c4878aSAndroid Build Coastguard Worker packets. 26*61c4878aSAndroid Build Coastguard Worker- **Divisible Memory Regions**: Incoming buffers can be divided without a copy, 27*61c4878aSAndroid Build Coastguard Worker allowing incoming data to be freely demultiplexed. 28*61c4878aSAndroid Build Coastguard Worker 29*61c4878aSAndroid Build Coastguard Worker------------------------------- 30*61c4878aSAndroid Build Coastguard WorkerWhat kinds of data is this for? 31*61c4878aSAndroid Build Coastguard Worker------------------------------- 32*61c4878aSAndroid Build Coastguard Worker``pw_multibuf`` is best used in code that wants to read, write, or pass along 33*61c4878aSAndroid Build Coastguard Workerdata which are one of the following: 34*61c4878aSAndroid Build Coastguard Worker 35*61c4878aSAndroid Build Coastguard Worker- **Large**: ``pw_multibuf`` is designed to allow breaking up data into 36*61c4878aSAndroid Build Coastguard Worker multiple chunks. It also supports asynchronous allocation for when there may 37*61c4878aSAndroid Build Coastguard Worker not be sufficient space for incoming data. 38*61c4878aSAndroid Build Coastguard Worker- **Communications-Oriented**: Data which is being received or sent across 39*61c4878aSAndroid Build Coastguard Worker sockets, various packets, or shared-memory protocols can benefit from the 40*61c4878aSAndroid Build Coastguard Worker fragmentation, multiplexing, and header/footer-reservation properties of 41*61c4878aSAndroid Build Coastguard Worker ``pw_multibuf``. 42*61c4878aSAndroid Build Coastguard Worker- **Copy-Averse**: ``pw_multibuf`` is structured to allow users to pass around 43*61c4878aSAndroid Build Coastguard Worker and mutate buffers without copying or moving data in-memory. This can be 44*61c4878aSAndroid Build Coastguard Worker especially useful when working in systems that are latency-sensitive, 45*61c4878aSAndroid Build Coastguard Worker need to pass large amounts of data, or when memory usage is constrained. 46*61c4878aSAndroid Build Coastguard Worker 47*61c4878aSAndroid Build Coastguard Worker------------- 48*61c4878aSAndroid Build Coastguard WorkerAPI Reference 49*61c4878aSAndroid Build Coastguard Worker------------- 50*61c4878aSAndroid Build Coastguard WorkerMost users of ``pw_multibuf`` will start by allocating a ``MultiBuf`` using 51*61c4878aSAndroid Build Coastguard Workera ``MultiBufAllocator`` class, such as the ``SimpleAllocator``. 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker``MultiBuf`` s consist of a number of ``Chunk`` s of contiguous memory regions. 54*61c4878aSAndroid Build Coastguard Worker``Chunk`` s can be grown or shrunk which allows ``MultiBuf`` s to be grown or 55*61c4878aSAndroid Build Coastguard Workershrunk. This allows, for example, lower layers to reserve part of a 56*61c4878aSAndroid Build Coastguard Worker``MultiBuf`` for a header or footer (see ``Chunk`` for more details). 57*61c4878aSAndroid Build Coastguard Worker 58*61c4878aSAndroid Build Coastguard Worker``MultiBuf`` exposes an ``std::byte`` iterator interface as well as a ``Chunk`` 59*61c4878aSAndroid Build Coastguard Workeriterator available through the ``Chunks()`` method. It allows extracting a 60*61c4878aSAndroid Build Coastguard Worker``Chunk`` as an RAII-style ``OwnedChunk`` which manages its own lifetime. 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::Chunk 63*61c4878aSAndroid Build Coastguard Worker :members: 64*61c4878aSAndroid Build Coastguard Worker 65*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::OwnedChunk 66*61c4878aSAndroid Build Coastguard Worker :members: 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::MultiBuf 69*61c4878aSAndroid Build Coastguard Worker :members: 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker.. doxygenfunction:: pw::multibuf::FromSpan 72*61c4878aSAndroid Build Coastguard Worker 73*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::MultiBufChunks 74*61c4878aSAndroid Build Coastguard Worker :members: 75*61c4878aSAndroid Build Coastguard Worker 76*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::MultiBufAllocator 77*61c4878aSAndroid Build Coastguard Worker :members: 78*61c4878aSAndroid Build Coastguard Worker 79*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::SimpleAllocator 80*61c4878aSAndroid Build Coastguard Worker :members: 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::Stream 83*61c4878aSAndroid Build Coastguard Worker :members: 84*61c4878aSAndroid Build Coastguard Worker 85*61c4878aSAndroid Build Coastguard WorkerTest-only features 86*61c4878aSAndroid Build Coastguard Worker================== 87*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::test::SimpleAllocatorForTest 88*61c4878aSAndroid Build Coastguard Worker :members: 89*61c4878aSAndroid Build Coastguard Worker 90*61c4878aSAndroid Build Coastguard Worker--------------------------- 91*61c4878aSAndroid Build Coastguard WorkerAllocator Implementors' API 92*61c4878aSAndroid Build Coastguard Worker--------------------------- 93*61c4878aSAndroid Build Coastguard WorkerSome users will need to directly implement the ``MultiBufAllocator`` interface 94*61c4878aSAndroid Build Coastguard Workerin order to provide allocation out of a particular region, provide particular 95*61c4878aSAndroid Build Coastguard Workerallocation policy, fix Chunks to some size (such as MTU size - header for 96*61c4878aSAndroid Build Coastguard Workersocket implementations), or specify other custom behavior. 97*61c4878aSAndroid Build Coastguard Worker 98*61c4878aSAndroid Build Coastguard WorkerThese users will also need to understand and implement the following APIs: 99*61c4878aSAndroid Build Coastguard Worker 100*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::ChunkRegionTracker 101*61c4878aSAndroid Build Coastguard Worker :members: 102*61c4878aSAndroid Build Coastguard Worker 103*61c4878aSAndroid Build Coastguard WorkerA simple implementation of a ``ChunkRegionTracker`` is provided, called 104*61c4878aSAndroid Build Coastguard Worker``HeaderChunkRegionTracker``. It stores its ``Chunk`` and region metadata in a 105*61c4878aSAndroid Build Coastguard Worker``Allocator`` allocation alongside the data. The allocation process is 106*61c4878aSAndroid Build Coastguard Workersynchronous, making this class suitable for testing. The allocated region or 107*61c4878aSAndroid Build Coastguard Worker``Chunk`` must not outlive the provided allocator. 108*61c4878aSAndroid Build Coastguard Worker 109*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::HeaderChunkRegionTracker 110*61c4878aSAndroid Build Coastguard Worker :members: 111*61c4878aSAndroid Build Coastguard Worker 112*61c4878aSAndroid Build Coastguard WorkerAnother ``ChunkRegionTracker`` specialization is the lightweight 113*61c4878aSAndroid Build Coastguard Worker``SingleChunkRegionTracker``, which does not rely on ``Allocator`` and uses the 114*61c4878aSAndroid Build Coastguard Workerprovided memory view to create a single chunk. This is useful when a single 115*61c4878aSAndroid Build Coastguard Worker``Chunk`` is sufficient at no extra overhead. However, the user needs to own 116*61c4878aSAndroid Build Coastguard Workerthe provided memory and know when a new ``Chunk`` can be requested. 117*61c4878aSAndroid Build Coastguard Worker 118*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::multibuf::SingleChunkRegionTracker 119*61c4878aSAndroid Build Coastguard Worker :members: 120