This article and we share the main Ucos in the memory management related content, together to see it, hope to learn ucos to help you.
in embedded devices, persistent calls to malloc () and free () are prone to memory fragmentation, and long runs can eventually result in memory exhaustion. UCOS provides a memory management mechanism that allocates memory space when the system is initialized, organizes all available space into a linked list, requests memory directly from the linked list, and then returns memory directly to the free memory list when the memory is freed. Using this method not only avoids the generation of memory fragments, but also makes it possible to allocate memory space within a constant time.
the structure of memory management in UCOS is os_mem, with the following structure:
typedef struct OS_MEM // Memory control block {
void *osmemaddr; point to the first address of the memory partition
void *osmemfreelist; The table header of the block list for this memory partition
INT32U osmemblksize; the size of each block
INT32U osmemnblks; The number of blocks in the partition
INT32U Osmemnfree; number of free blocks in this partition
} Os_mem;
in the in UCOS, a memory partition is divided into a number of equal-sized chunks of memory that are linked to a linked list, and the table header of the list is stored in osmemfreelist .
For example, a 4 memory block, each memory block 128bit memory partition Memoy as follows:
INT32U Memory[4][4]
UCOS 's idea is that we are going to allocate space into a two-dimensional array, and then each row of the two-dimensional array is a block, and the number of columns in thetwo-dimensional array is both the size of the block.
The first block of Memory is the initial address of memor[0]
The first address of the second block of Memory is memory[1]
The first address of the third block of Memory is memory[2]
The first address of the fourth block of Memory is memory[3]
for Management convenience we store the address of the next block at the beginning of each block , so that all blocks are strung into a single linked list.
the beginning of the first block stores Memory[1]
the beginning of the first block stores memory[2]
the beginning of the first block stores Memory[3]
the beginning of the first block stores NULL
The final results are as follows:
the core code for creating a memory partition in UCOS is as follows ( code taken from osmemcreate):
first address of memory partition
Plink = (void * *) addr;
the address of a second block
PBLK = (int8u *) ((int32u) addr + blksize);
for (i = 0; i < (nblks-1); i++)
{
the beginning of each block holds the first address of the next block .
*plink = (void *) pblk;
Update pointers only
Plink = (void * *) pblk;
PBLK = (int8u *) ((int32u) pblk + blksize);
}
The last block points to NULL
*plink = (void *) 0;
the core of the above code is *pblink = (void*) pblk; each one Block The beginning holds the next Block address, so that all the Block organized into a linked list.
Request a memory block:
and there's memory to allocate .
if (Pmem->osmemnfree > 0)
{
get a block
PBLK = pmem->osmemfreelist;
point The list header to the next block in the block .
pmem->osmemfreelist = * (void * *) pblk;
update the number of memory blocks
pmem->osmemnfree--;
}
the core of the code is pmem->osmemfreelist = * (void * *) pblk; because pblk point to the Block , the Block the first address of the store is the next Block address, so this sentence actually points the table header of the free list to the next Block .
Delete a block of memory:
Place the address of the block header on the first address of the chunk of memory that will be deleted
* (void * *) pblk = pmem->osmemfreelist;
updating a list header
Pmem->osmemfreelist = pblk;
update the number of available memory blocks
pmem->osmemnfree++;
the core of the code is * (void * *) pblk = pmem->osmemfreelist; when you want to delete the Block the table header of the linked list, which is equivalent to putting this Block Linked to the table header of the free list.
Source: CSDN
Memory management of Ucos learning