Memory Pool Allocation

This functionality provides fixed-size memory allocation for message payloads using free-list pools with multi-pool fallback.

Static View

The following software units are involved in memory pool allocation:

  • Memory Pool Management Unit: Manages multiple pools with different block sizes

    (see Memory Pool Management for its class diagram)

  • Message Management Unit: Requests memory for message payloads

' *******************************************************************************
' Copyright (c) 2026 BMW AG
'
' This program and the accompanying materials are made available under the
' terms of the Apache License Version 2.0 which is available at
' https://www.apache.org/licenses/LICENSE-2.0
'
' SPDX-License-Identifier: Apache-2.0
' *******************************************************************************

@startuml

package "Memory Pool Allocation" as memory_pool_allocation_flow {
  component "Data Structures" as memory_pool_allocation_data_structures {
    component "Message Management" as memory_pool_allocation_message_management
    component "Memory Pool Management" as memory_pool_allocation_memory_pool_management
  }

  interface "Allocator Requests" as memory_pool_allocation_allocator_requests

  memory_pool_allocation_message_management -( memory_pool_allocation_allocator_requests
  memory_pool_allocation_memory_pool_management )-d- memory_pool_allocation_allocator_requests
}

@enduml

Memory pool allocation component view

Notes:

  • Aggregator routes requests to the appropriate pool based on size.

  • Falls back to larger pools if smaller ones are exhausted.

  • Each pool uses a free-list with pNext_chunk pointer chaining for fast allocation.

  • Each free chunk contains a pointer to the next free chunk (or nullptr if last).

  • Allocation and deallocation are constant-time, O(1).

Dynamic View

The dynamic view illustrates memory allocation with pool selection and fallback.

Allocate

' *******************************************************************************
' Copyright (c) 2026 BMW AG
'
' This program and the accompanying materials are made available under the
' terms of the Apache License Version 2.0 which is available at
' https://www.apache.org/licenses/LICENSE-2.0
'
' SPDX-License-Identifier: Apache-2.0
' *******************************************************************************

@startuml

hide footbox

participant "Message Management" as message_management_unit
participant "Memory Pool Management" as memory_pool_management_unit

activate message_management_unit
activate memory_pool_management_unit

message_management_unit -> memory_pool_management_unit : allocate(requestedSize=48)
memory_pool_management_unit -> memory_pool_management_unit : determine best pool (size <= 64)
memory_pool_management_unit -> memory_pool_management_unit : allocate() from Pool<64>

alt Pool<64> has free block
    memory_pool_management_unit -> memory_pool_management_unit : get pNext_chunk pointer
    memory_pool_management_unit -> memory_pool_management_unit : update pNext_chunk from freed chunk's next pointer
    memory_pool_management_unit -> memory_pool_management_unit : mark chunk as allocated
    memory_pool_management_unit --> message_management_unit : pointer
else Pool<64> is full
    memory_pool_management_unit -> memory_pool_management_unit : allocate() from Pool<256> [fallback]
    alt Pool<256> has free block
        memory_pool_management_unit -> memory_pool_management_unit : get pNext_chunk pointer
        memory_pool_management_unit -> memory_pool_management_unit : update pNext_chunk from freed chunk's next pointer
        memory_pool_management_unit -> memory_pool_management_unit : mark chunk as allocated
        memory_pool_management_unit --> message_management_unit : pointer
    else all pools are full
        memory_pool_management_unit --> message_management_unit : nullptr
    end
end

message_management_unit -> message_management_unit : wrap pointer result as HRESULT

note over message_management_unit : MessagePayloadBuilder reports HRESULT::Ok or CannotAllocatePayload.

deactivate message_management_unit
deactivate memory_pool_management_unit

@enduml

Memory pool allocation sequence

Deallocate

' *******************************************************************************
' Copyright (c) 2026 BMW AG
'
' This program and the accompanying materials are made available under the
' terms of the Apache License Version 2.0 which is available at
' https://www.apache.org/licenses/LICENSE-2.0
'
' SPDX-License-Identifier: Apache-2.0
' *******************************************************************************

@startuml

hide footbox

participant "Message Management" as message_management_unit
participant "Memory Pool Management" as memory_pool_management_unit

activate message_management_unit
activate memory_pool_management_unit

message_management_unit -> memory_pool_management_unit : deallocate(pointer)
memory_pool_management_unit -> memory_pool_management_unit : determine pool from pointer address
memory_pool_management_unit -> memory_pool_management_unit : deallocate(pointer) in Pool<64>
memory_pool_management_unit -> memory_pool_management_unit : write previous pNext_chunk to freed chunk
memory_pool_management_unit -> memory_pool_management_unit : update pNext_chunk to point to freed chunk
memory_pool_management_unit -> memory_pool_management_unit : mark chunk as free
memory_pool_management_unit --> message_management_unit : HRESULT::Ok

note over memory_pool_management_unit : Chunk freed and added to the free-list.

deactivate message_management_unit
deactivate memory_pool_management_unit

@enduml

Memory pool deallocation sequence