OS Memory Exported Functions

group OS_MEM_Exported_Functions

Defines

os_mem_alloc(ram_type, size)

Allocate a memory block with required size, using the given RAM type. If required size is not aligned to 8, the actual allocated block size will be automatically aligned to 8.

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 succeeded, and free it.
        os_mem_free(p_mem);
    }
    else
    {
        // Memory allocation failed.
        return -1;
    }

    return 0;
}
Parameters:
  • ram_type -- [in] RAM type for allocation, refer to 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, using the given RAM type. If required size is not aligned to 8, the actual allocated block size will be automatically aligned to 8.

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 succeeded, and free it.
        os_mem_free(p_mem);
    }
    else
    {
        // Memory allocation failed.
        return -1;
    }

    return 0;
}
Parameters:
  • ram_type -- [in] RAM type for allocation, refer to 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.

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 succeeded, 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, refer to RAM_TYPE.

  • size -- [in] Required memory size.

  • alignment -- [in] Memory alignment in 2^N bytes. If alignment is 0, use system default memory alignment (8 Bytes for FreeRTOS). 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.

Functions

void os_mem_free(void *p_block)

Free a memory block that had been allocated.

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 succeeded, 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.

void os_mem_aligned_free(void *p_block)

Free a memory block that had been aligned allocated.

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 succeeded, and free it.
        os_mem_aligned_free(p_mem);
    }
    else
    {
        // Aligned memory allocation failed.
        return -1;
    }

    return 0;
}
Parameters:

p_block -- [in] The address of memory block being freed.

size_t os_mem_peek(RAM_TYPE ram_type)

Peek the total unused memory size in the specified RAM type.

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 buffer on memory size.
    unused_size = os_mem_peek(RAM_TYPE_BUFFER_ON);

    return 0;
}
Parameters:

ram_type -- [in] RAM type for peek, refer to RAM_TYPE.

Returns:

The total unused memory size in bytes.