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 Figure 17-3the figure below. Calling OSMemCreate()
creates a memory partition.F17-3
Panel | ||||
---|---|---|---|---|
| ||||
Panel | ||
---|---|---|
| ||
(1) |
...
When creating a partition, the application code supplies the address of a memory partition control block ( |
...
calling |
...
(2) |
...
organizes the continuous memory provided into a singly linked list and stores the pointer to the beginning of the list in |
...
the |
...
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. |
...
Code Block | ||
---|---|---|
|
...
| |
OS_MEM MyPartition; (1)
CPU_INT08U MyPartitionStorage[12][100]; (2)
void main (void) (3)
{
OS_ERR err;
:
:
OSInit(&err);
:
OSMemCreate((OS_MEM *)&MyPartition, (4)
(CPU_CHAR *)"My Partition", (5)
(void *)&MyPartitionStorage[0][0], (6)
(OS_MEM_QTY ) 12, (7)
(OS_MEM_SIZE)100, (8)
(OS_ERR *)&err); (9)
/* Check 'err' */
:
:
OSStart(&err);
} |
Panel | ||
---|---|---|
| ||
(1) An application needs to allocate storage for a memory partition control block (i.e. |
...
structure). This can be a static allocation as shown here |
...
or |
...
can be used in the code. However, the application code must not deallocate the memory control block. |
...
(2) |
...
The application also needs to allocate storage for the memory that will be split into memory blocks. This can also be a static allocation |
...
or |
...
can be used. The same reasoning applies. Do not deallocate this storage since other tasks may rely on the existence of this storage. |
...
(3) |
...
Memory partition must be created before allocating and deallocating blocks from the partition. One of the best places to create memory partitions is |
...
in |
...
prior to starting the multitasking process. Of course, an application can call a function |
...
from |
...
to do this instead of actually placing the code directly |
...
in |
...
(4) |
...
You pass the address of the memory partition control block |
...
to |
...
the |
...
data structure. Instead, you should always use µC/OS-III’s API. |
...
(5) |
...
You can assign a name to the memory partition. There is no limit to the length of the ASCII string as µC/OS-III saves a pointer to the ASCII string in the partition control block and not the actual characters. |
...
(6) |
...
You then need to pass the base address of the storage area reserved for the memory blocks. |
...
(7) |
...
Here, you specify how many memory blocks are available from this memory partition. Hard coded numbers are used for the sake of the illustration but you should instead |
...
use |
...
constants. |
...
(8) |
...
You need to specify the size of each memory block in the partition. Again, a hard coded value is used for illustration, which is not recommended in real code. |
...
(9) |
...
As with most µC/OS-III services, |
...
returns an error code indicating the outcome of the service. The call is successful if “ |
...
contains |
Listing 17-2 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.L17-2(1) Instead
Code Block | ||
---|---|---|
| ||
OS_MEM *MyPartitionPtr; (1)
void main (void)
{
OS_ERR err;
void *p_storage;
:
OSInit(&err);
:
MyPartitionPtr = (OS_MEM *)malloc(sizeof(OS_MEM)); (2)
if (MyPartitionPtr != (OS_MEM *)0) {
p_storage = malloc(12 * 100); (3)
if (p_storage != (void *)0) {
OSMemCreate((OS_MEM *)MyPartitionPtr, (4)
(CPU_CHAR *)"My Partition",
(void *)p_storage, (5)
(OS_MEM_QTY ) 12, (6)
(OS_MEM_SIZE)100, (6)
(OS_ERR *)&err);
/* Check 'err" */
}
}
:
OSStart(&err);
} |
Panel | ||
---|---|---|
| ||
(1) Instead of allocating static storage for the memory partition control block, you can assign a pointer that receives |
...
the |
...
allocated using |
...
(2) |
...
The application allocates storage for the memory control block. |
...
(3) |
...
We then allocate storage for the memory partition. |
...
(4) |
...
A pointer is passed to the allocated memory control block |
...
to |
...
(5) |
...
The base address of the storage used for the partition is passed |
...
to |
...
(6) |
...
Finally, the number of blocks and the size of each block is passed so that µC/OS-III can create the linked list of 12 blocks of 100 bytes each. Again, hard coded numbers are used, but these would typically be replaced |
...
by |