kernel/memory_management/shared_multi_heap.html · kernel/memory_management/shared_multi_heap.rst 공식 원문 ↗

Zephyr 3.7.0 · Kernel · Memory Management

공유 멀티 힙

Cacheability나 CPU affinity 같은 capability를 opaque attribute로 표현해 적합한 memory region에서 할당합니다.

Official pathkernel/memory_management/shared_multi_heap.html
Source filekernel/memory_management/shared_multi_heap.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Boot 때 pool과 region을 구성하고 application은 물리 배치를 알 필요 없이 capability만 지정합니다.

Input

attribute

Choice

runtime heap

Regions

DT · platform

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 0904A20B5E22A22A5124A700BCB457B24AE34C3908482348B644319CB964B653
.. _memory_management_shared_multi_heap:

Shared Multi Heap
#################

The shared multi-heap memory pool manager uses the multi-heap allocator to
manage a set of reserved memory regions with different capabilities /
attributes (cacheable, non-cacheable, etc...).

All the different regions can be added at run-time to the shared multi-heap
pool providing an opaque "attribute" value (an integer or enum value) that can
be used by drivers or applications to request memory with certain capabilities.

This framework is commonly used as follow:

1. At boot time some platform code initialize the shared multi-heap framework
   using :c:func:`shared_multi_heap_pool_init()` and add the memory regions to
   the pool with :c:func:`shared_multi_heap_add()`, possibly gathering the
   needed information for the regions from the DT.

2. Each memory region encoded in a :c:struct:`shared_multi_heap_region`
   structure.  This structure is also carrying an opaque and user-defined
   integer value that is used to define the region capabilities (for example:
   cacheability, cpu affinity, etc...)

.. code-block:: c

   // Init the shared multi-heap pool
   shared_multi_heap_pool_init()

   // Fill the struct with the data for cacheable memory
   struct shared_multi_heap_region cacheable_r0 = {
        .addr = addr_r0,
        .size = size_r0,
        .attr = SMH_REG_ATTR_CACHEABLE,
   };

   // Add the region to the pool
   shared_multi_heap_add(&cacheable_r0, NULL);

   // Add another cacheable region
   struct shared_multi_heap_region cacheable_r1 = {
        .addr = addr_r1,
        .size = size_r1,
        .attr = SMH_REG_ATTR_CACHEABLE,
   };

   shared_multi_heap_add(&cacheable_r0, NULL);

   // Add a non-cacheable region
   struct shared_multi_heap_region non_cacheable_r2 = {
        .addr = addr_r2,
        .size = size_r2,
        .attr = SMH_REG_ATTR_NON_CACHEABLE,
   };

   shared_multi_heap_add(&non_cacheable_r2, NULL);

3. When a driver or application needs some dynamic memory with a certain
   capability, it can use :c:func:`shared_multi_heap_alloc()` (or the aligned
   version) to request the memory by using the opaque parameter to select the
   correct set of attributes for the needed memory. The framework will take
   care of selecting the correct heap (thus memory region) to carve memory
   from, based on the opaque parameter and the runtime state of the heaps
   (available memory, heap state, etc...)

.. code-block:: c

   // Allocate 4K from cacheable memory
   shared_multi_heap_alloc(SMH_REG_ATTR_CACHEABLE, 0x1000);

   // Allocate 4K from non-cacheable memory
   shared_multi_heap_alloc(SMH_REG_ATTR_NON_CACHEABLE, 0x1000);

Adding new attributes
*********************

The API does not enforce any attributes, but at least it defines the two most
common ones: :c:enumerator:`SMH_REG_ATTR_CACHEABLE` and :c:enumerator:`SMH_REG_ATTR_NON_CACHEABLE`.

.. doxygengroup:: shared_multi_heap
   :project: Zephyr

Part 3

한국어 전문 번역

Shared multi-heap memory pool manager는 multi-heap allocator를 사용해 cacheable, non-cacheable 등 서로 다른 capability와 attribute를 가진 reserved memory region 집합을 관리합니다.

각 region은 runtime에 pool로 추가할 수 있습니다. 이때 integer 또는 enum인 opaque attribute 값을 함께 제공하며 driver나 application은 원하는 memory capability를 이 값으로 요청합니다.

일반적인 사용 흐름

  1. Boot 때 platform code가 shared_multi_heap_pool_init()으로 framework를 초기화하고 shared_multi_heap_add()로 region을 추가합니다. Region 정보는 DT(DeviceTree)에서 수집할 수 있습니다.
  2. 각 memory region을 struct shared_multi_heap_region으로 표현합니다. 이 구조체의 user-defined integer attribute는 cacheability, CPU affinity 같은 capability를 나타냅니다.
// Init the shared multi-heap pool
shared_multi_heap_pool_init()

// Fill the struct with the data for cacheable memory
struct shared_multi_heap_region cacheable_r0 = {
     .addr = addr_r0,
     .size = size_r0,
     .attr = SMH_REG_ATTR_CACHEABLE,
};

// Add the region to the pool
shared_multi_heap_add(&cacheable_r0, NULL);

// Add another cacheable region
struct shared_multi_heap_region cacheable_r1 = {
     .addr = addr_r1,
     .size = size_r1,
     .attr = SMH_REG_ATTR_CACHEABLE,
};

shared_multi_heap_add(&cacheable_r0, NULL);

// Add a non-cacheable region
struct shared_multi_heap_region non_cacheable_r2 = {
     .addr = addr_r2,
     .size = size_r2,
     .attr = SMH_REG_ATTR_NON_CACHEABLE,
};

shared_multi_heap_add(&non_cacheable_r2, NULL);

Driver나 application이 특정 capability의 동적 memory를 원하면 shared_multi_heap_alloc() 또는 aligned 변형에 opaque attribute를 넘깁니다. Framework는 이 값과 각 heap의 가용 memory·runtime state를 함께 보고 allocation할 올바른 heap, 즉 memory region을 선택합니다.

// Allocate 4K from cacheable memory
shared_multi_heap_alloc(SMH_REG_ATTR_CACHEABLE, 0x1000);

// Allocate 4K from non-cacheable memory
shared_multi_heap_alloc(SMH_REG_ATTR_NON_CACHEABLE, 0x1000);

새 attribute 추가

API 자체는 특정 attribute 집합을 강제하지 않습니다. 다만 가장 흔한 SMH_REG_ATTR_CACHEABLE과 SMH_REG_ATTR_NON_CACHEABLE 두 값을 기본으로 정의합니다. Platform은 같은 opaque integer model에 맞춰 필요한 capability를 확장할 수 있습니다.

API 참고: shared_multi_heap

shared_multi_heap group은 pool 초기화, region 등록, attribute 기반 allocation/free와 region descriptor를 제공합니다.

MAX_SHARED_MULTI_HEAP_ATTR

Framework이 표준으로 정의하는 attribute의 최대 수입니다.

enum shared_multi_heap_attr

자주 쓰는 memory region attribute를 정의하는 enum type입니다.

SMH_REG_ATTR_CACHEABLE

CPU cache를 사용할 수 있는 memory region입니다.

SMH_REG_ATTR_NON_CACHEABLE

Non-cacheable memory region입니다.

SMH_REG_ATTR_NUM

Attribute enum의 마지막에 있어야 하는 개수 marker입니다.

int shared_multi_heap_pool_init(void)

Shared multi-heap pool을 초기화합니다. 반드시 가장 먼저 호출하고 각 heap은 뒤에 shared_multi_heap_add()로 추가합니다. Generic multi-heap과 마찬가지로 보통 SoC 또는 board level에서 호출합니다.

반환값: 성공은 0, 이미 초기화됐으면 -EALREADY, 그 밖에는 errno code입니다.

void *shared_multi_heap_alloc(enum shared_multi_heap_attr attr, size_t bytes)

지정 capability/attribute와 byte 크기를 만족하는 block을 shared multi-heap pool에서 할당합니다. Backend는 opaque attr로 올바른 child heap을 선택합니다.

매개변수

  • attr: memory block에 요구하는 capability/attribute
  • bytes: 요청 allocation 크기(byte)

반환값: 유효한 heap pointer이며 공간이 없으면 NULL입니다. 생성 문서는 이를 ptr/err 두 항목으로 표현합니다.

void *shared_multi_heap_aligned_alloc(enum shared_multi_heap_attr attr, size_t align, size_t bytes)

지정 capability와 크기의 block을 power-of-two alignment에 맞춰 할당합니다.

매개변수

  • attr: 요구 capability/attribute
  • align: 반환 pointer의 power-of-two alignment(byte)
  • bytes: 요청 allocation 크기(byte)

반환값: 유효한 heap pointer이며 공간이 없으면 NULL입니다.

void shared_multi_heap_free(void *block)

이전에 shared_multi_heap_alloc() 또는 shared_multi_heap_aligned_alloc()이 반환한 block을 원래 pool에 돌려줍니다.

매개변수

  • block: shared multi-heap에서 할당한 해제 대상 pointer
int shared_multi_heap_add(struct shared_multi_heap_region *region, void *user_data)

Shared multi-heap region 하나를 pool에 추가합니다.

매개변수

  • region: 추가할 memory region pointer
  • user_data: 해당 heap에 연결할 임의 data pointer

반환값: 성공은 0, attribute 범위가 잘못되면 -EINVAL, 더 추가할 heap slot이 없으면 -ENOMEM, 그 밖에는 errno code입니다.

struct shared_multi_heap_region

Pool에 추가할 memory region의 위치·크기·attribute를 담는 구조체이며 에 선언됩니다.

uint32_t attr

Memory heap의 opaque attribute 값입니다.

uintptr_t addr

Memory heap 시작 virtual address입니다.

size_t size

Memory heap 크기(byte)입니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.