开源鸿蒙 Static Memory

  • 2022-08-09
  • 浏览 (574)

Static Memory

Working Principles

The static memory is a static array. The block size in the static memory pool is set during initialization and cannot be changed after initialization.

The static memory pool consists of a control block LOS_MEMBOX_INFO and several memory blocks LOS_MEMBOX_NODE of the same size. The control block is located at the head of the memory pool and used for memory block management. It contains the memory block size (uwBlkSize), number of memory blocks (uwBlkNum), number of allocated memory blocks (uwBlkCnt), and free list (stFreeList). Memory is allocated and released by block size. Each memory block contains the pointer pstNext that points to the next memory block.

Figure 1 Static memory

Development Guidelines

When to Use

Use static memory allocation to obtain memory blocks of the fixed size. When the memory is no longer required, release the static memory.

Available APIs

The following table describes APIs available for OpenHarmony LiteOS-M static memory management. For more details about the APIs, see the API reference.

Table 1 APIs of the static memory module

Function

API

Description

Initializing the static memory pool

LOS_MemboxInit

Initialize a static memory pool and sets the start address, total size, and size of each memory block based on the input parameters.

Clearing static memory blocks

LOS_MemboxClr

Clears the memory blocks allocated from the static memory pool.

Allocating or releasing static memory

LOS_MemboxAlloc

Allocates a memory block from a specified static memory pool.

LOS_MemboxFree

Releases a memory block allocated from the static memory pool.

Obtaining or printing static memory pool information

LOS_MemboxStatisticsGet

Obtains information about a specified static memory pool, including the total number of memory blocks in the memory pool, number of allocated memory blocks, and size of each memory block.

LOS_ShowBox

Prints information about all nodes in a specified static memory pool (the print level is LOS_INFO_LEVEL). The information includes the start address of the memory pool, memory block size, total number of memory blocks, start address of each idle memory block, and start addresses of all memory blocks.

NOTE
The number of memory blocks in the memory pool after initialization is not equal to the total memory size divided by the memory block size. The reason is the control block of the memory pool and the control header of each memory block have memory overheads. When setting the total memory size, you need to consider these factors.

How to Develop

The typical development process of static memory is as follows:

  1. Plan a memory space as the static memory pool.

  2. Call the LOS_MemboxInit API to initialize the static memory pool.

    During initialization, the memory space specified by the input parameter is divided into multiple blocks (the number of blocks depends on the total static memory size and the block size). Insert all memory blocks to the free list, and place the control header at the beginning of the memory.

  3. Call the LOS_MemboxAlloc API to allocate static memory.

    The system allocates the first free memory block from the free list and returns the start address of this memory block.

  4. Call the LOS_MemboxClr API.

    Clear the memory block corresponding to the address contained in the input parameter.

  5. Call the LOS_MemboxFree API.

    Add the memory block to the free list.

Development Example

This example implements the following:

  1. Initialize a static memory pool.

  2. Allocate a memory block from the static memory pool.

  3. Store a piece of data in a memory block.

  4. Print the data in the memory block.

  5. Clear the data in the memory block.

  6. Release the memory block.

    The sample code is as follows:

#include "los_membox.h"

VOID Example_StaticMem(VOID)
{
    UINT32 *mem = NULL;
    UINT32 blkSize = 10;
    UINT32 boxSize = 100;
    UINT32 boxMem[1000];
    UINT32 ret;

 /* Initialize the memory pool.*/
    ret = LOS_MemboxInit(&boxMem[0], boxSize, blkSize);
    if(ret != LOS_OK) {
        printf("Membox init failed!\n");
        return;
    } else {
        printf("Membox init success!\n");
    }

    /* Request a memory block.*/
    mem = (UINT32 *)LOS_MemboxAlloc(boxMem);
    if (NULL == mem) {
        printf("Mem alloc failed!\n");
        return;
    }
    printf("Mem alloc success!\n");

    /* Assign a value.*/
    *mem = 828;
    printf("*mem = %d\n", *mem);

 /* Clear data in the memory block. */
    LOS_MemboxClr(boxMem, mem);
    printf("Mem clear success \n *mem = %d\n", *mem);

    /* Release the memory.*/
    ret = LOS_MemboxFree(boxMem, mem);
    if (LOS_OK == ret) {
        printf("Mem free success!\n");
    } else {
        printf("Mem free failed!\n");
    }

    return;
}

Verification

The output is as follows:

Membox init success!
Mem alloc success!
*mem = 828
Mem clear success   
*mem = 0
Mem free success!

你可能感兴趣的文章

开源鸿蒙 Kernel

开源鸿蒙 Time Management

开源鸿蒙 Appendix

开源鸿蒙 Kernel Coding Specification

开源鸿蒙 Doubly Linked List

开源鸿蒙 Basic Data Structure

开源鸿蒙 CMSIS Support

开源鸿蒙 POSIX Support

开源鸿蒙 Standard Libraries

开源鸿蒙 Interrupt Management

0  赞