Before using a memory partition, it must be created. This allows µC/OS-III to know something about the memory partition so that it can manage their allocation and deallocation. Once created, a memory partition is as shown in the figure below. Calling OSMemCreate()
creates a memory partition.
(1) When creating a partition, the application code supplies the address of a memory partition control block (OS_MEM
). Typically, this memory control block is allocated from static memory, however it can also be obtained from the heap by calling malloc()
. The application code should however never deallocate it.
(2) OSMemCreate()
organizes the continuous memory provided into a singly linked list and stores the pointer to the beginning of the list in the OS_MEM
structure.
(3) Each memory block must be large enough to hold a pointer. Given the nature of the linked list, a block needs to be able to point to the next block.
The listing below indicates how to create a memory partition with µC/OS-III.
The listing below shows how to create a memory partition with µC/OS-III, but this time, using malloc()
to allocate storage. Do not deallocate the memory control block or the storage for the partition.