LwMEM instances

LwMEM architecture allows multiple instances, to completely isolate memory management between different memories. This may allow separation of memory management at hardware level with different security feature.

By default, LwMEM has single instance created at library level, called default instance. Default instance does not need any special attention as it is embedded at library core, instead application has to assign memory regions for the instance.

Every instance has:

  • Instance control block

  • Multiple regions assigned to each instance

Note

Control block of default instance is already initialized by library core, hence it does not need any special attention at application layer.

LwMEM internal architecture with control block

LwMEM internal architecture with control block

Picture above shows internal architecture of LwMEM. Control block holds info about first free block for allocation and other private data, such as mutex handle when operating system is in use.

Yellow part of the image shows customized, application-defined, regions, which must be manually assigned to the instance during application start-up.

Known example for assinging regions to LwMEM is shown below. Default instance is used, therefore no special attention needs to be added when assigning regions or allocating memory.

Definition and assignment of regions for default LwMEM instance
 1#include "lwmem/lwmem.h"
 2
 3/*
 4 * \brief           Define regions for memory manager
 5 */
 6static
 7lwmem_region_t regions[] = {
 8    /* Set start address and size of each region */
 9    { (void *)0x10000000, 0x00001000 },
10    { (void *)0xA0000000, 0x00008000 },
11    { (void *)0xC0000000, 0x00008000 },
12    { NULL, 0},
13};
14
15/* Later in the initialization process */
16/* Assign regions for manager */
17lwmem_assignmem(regions);
18/* or */
19lwmem_assignmem_ex(NULL, regions);

When application adds second LwMEM instance, then special functions with _ex must be used. These allow application to specify for which LwMEM instance specific operation is intended.

Tip

Check lwmem_assignmem_ex() description for more information about input parameters.

Definition and assignment of regions for custom LwMEM instance
 1#include "lwmem/lwmem.h"
 2
 3/**
 4 * \brief           Custom LwMEM instance
 5 */
 6static
 7lwmem_t lw_custom;
 8
 9/*
10 * \brief           Define regions for memory manager
11 */
12static
13lwmem_region_t regions[] = {
14    /* Set start address and size of each region */
15    { (void *)0x10000000, 0x00001000 },
16    { (void *)0xA0000000, 0x00008000 },
17    { (void *)0xC0000000, 0x00008000 },
18    { NULL, 0 }
19};
20
21/* Later in the initialization process */
22/* Assign regions for custom instance */
23lwmem_assignmem_ex(&lw_custom, regions);