Memory Management

group OS_87x3e_Memory

Allocate, free, and peek memory functions.

The Memory Management function group allows to allocate, free, and peek heap memory in the system.

Defines

os_mem_alloc(ram_type, size)

Allocate a memory block with required size.

os_mem.h

Example usage

int test(void)
{
    size_t mem_size = 0x1000;
    void *p_mem = NULL;

    p_mem = os_mem_alloc(RAM_TYPE_DATA_ON, mem_size);
    if (p_mem != NULL)
    {
        // Memory allocation successed, and free it.
        os_mem_free(p_mem);
    }
    else
    {
        // Memory allocation failed.
        return -1;
    }

    return 0;
}

Parameters:
  • ram_type[in] RAM type for allocation.

    • RAM_TYPE_DATA_ON Data ON RAM type.

    • RAM_TYPE_DATA_OFF Data OFF RAM type.

    • RAM_TYPE_BUFFER_ON BUFFER ON RAM type.

    • RAM_TYPE_DTCM0_OFF DTCM0 OFF RAM type.

  • size[in] Required memory size.

Returns:

The address of the allocated memory block. If the address is NULL, the memory allocation failed.

os_mem_zalloc(ram_type, size)

Allocate and clear a memory block with required size.

os_mem.h

Example usage

int test(void)
{
    size_t mem_size = 0x1000;
    void *p_mem = NULL;

    p_mem = os_mem_zalloc(RAM_TYPE_DATA_ON, mem_size);
    if (p_mem != NULL)
    {
        // Memory allocation successed, and free it.
        os_mem_free(p_mem);
    }
    else
    {
        // Memory allocation failed.
        return -1;
    }

    return 0;
}

Parameters:
  • ram_type[in] RAM type for allocation.

    • RAM_TYPE_DATA_ON Data ON RAM type.

    • RAM_TYPE_DATA_OFF Data OFF RAM type.

    • RAM_TYPE_BUFFER_ON BUFFER ON RAM type.

    • RAM_TYPE_DTCM0_OFF DTCM0 OFF RAM type.

  • size[in] Required memory size.

Returns:

The address of the allocated memory block. If the address is NULL, the memory allocation failed.

os_mem_aligned_alloc(ram_type, size, alignment)

Allocate an aligned memory block with required size.

os_mem.h

Example usage

int test(void)
{
    size_t mem_size = 0x1000;
    uint8_t mem_alignment = 16;
    void *p_mem = NULL;

    p_mem = os_mem_aligned_alloc(RAM_TYPE_DATA_ON, mem_size, mem_alignment);
    if (p_mem != NULL)
    {
        // Aligned memory allocation successed, and free it.
        os_mem_aligned_free(p_mem);
    }
    else
    {
        // Aligned memory allocation failed.
        return -1;
    }

    return 0;
}

Parameters:
  • ram_type[in] RAM type for allocation.

    • RAM_TYPE_DATA_ON Data ON RAM type.

    • RAM_TYPE_DATA_OFF Data OFF RAM type.

    • RAM_TYPE_BUFFER_ON BUFFER ON RAM type.

    • RAM_TYPE_DTCM0_OFF DTCM0 OFF RAM type.

  • size[in] Required memory size.

  • alignment[in] memory alignment in 2^N bytes. If alignment is 0, use system default memory alignment. The aligned memory block must use os_mem_aligned_free() API function to free.

Returns:

The address of the allocated memory block. If the address is NULL, the memory allocation failed.

os_mem_alloc2(size)

Free a memory block that had been allocated.

os_mem.h

Example usage

int test(void)
{
    size_t mem_size = 0x1000;
    void *p_mem = NULL;

    p_mem = os_mem_alloc(RAM_TYPE_DATA_ON, mem_size);
    if (p_mem != NULL)
    {
        // Memory allocation successed, and free it.
        os_mem_free(p_mem);
    }
    else
    {
        // Memory allocation failed.
        return -1;
    }

    return 0;
}

Parameters:
  • p_block[in] The address of memory block being freed.

Returns:

None.

mem_peek()

Peek the unused memory size of the specified RAM type.

os_mem.h

Example usage

int test(void)
{
    size_t unused_data_on;
    size_t unused_data_off;

    // Peek unused DATA ON memory size.
    unused_size = os_mem_peek(RAM_TYPE_DATA_ON);

    // Peek unused DATA OFF memory size.
    unused_size = os_mem_peek(RAM_TYPE_DATA_OFF);

    return 0;
}

Parameters:
  • ram_type[in] RAM type for allocation.

    • RAM_TYPE_DATA_ON Data ON RAM type.

    • RAM_TYPE_DATA_OFF Data OFF RAM type.

    • RAM_TYPE_BUFFER_ON BUFFER ON RAM type.

    • RAM_TYPE_DTCM0_OFF DTCM0 OFF RAM type.

Returns:

The unused memory size in btyes.

Variables

void (*os_mem_aligned_free)(void *p_block)

Free an aligned memory block that had been allocated.

os_mem.h

Example usage

int test(void)
{
    size_t mem_size = 0x1000;
    uint8_t mem_alignment = 16;
    void *p_mem = NULL;

    p_mem = os_mem_aligned_alloc(RAM_TYPE_DATA_ON, mem_size, mem_alignment);
    if (p_mem != NULL)
    {
        // Aligned memory allocation successed, and free it.
        os_mem_aligned_free(p_mem);
    }
    else
    {
        // Aligned memory allocation failed.
        return -1;
    }

    return 0;
}

Param p_block:

The address of memory block being freed.

Return:

None.