内存管理(Fixed-Size Memory Partitions)
链表实现->减少碎片内存导致的浪费
Required Configuration
OS_CFG_MEM_EN must be enabled in os_cfg.h. Refer to µC-OS-III Configuration Manual.
OSMemCreate
Description
Creates and initializes a memory partition. A memory partition contains a user-specified
number of fixed-size memory blocks. An application may obtain one of these memory blocks
and, when completed, release the block back to the same partition where the block originated.
Files
os.h/os_mem.c
Prototype
void OSMemCreate (OS_MEM *p_mem,
CPU_CHAR *p_name,
void *p_addr,
OS_MEM_QTY n_blks,
OS_MEM_SIZE blk_size,
OS_ERR *p_err)
Arguments
p_mem
is a pointer to a memory partition control block that must be allocated in the application.
It is assumed that storage will be allocated for the memory control blocks in the
application. In other words, the user will declare a “global” variable as follows, and pass a
pointer to this variable to OSMemCreate():
OS_MEM MyMemPartition;
p_name
is a pointer to an ASCII string to provide a name to the memory partition. The name can
be displayed by debuggers or µC/Probe.
p_addr
is the address of the start of a memory area used to create fixed-size memory blocks.
Memory partitions may be created using either static arrays or malloc() during startup.
Note that the partition must align on a pointer boundary. Thus, if a pointer is 16-bits wide.
the partition must start on a memory location with an address that ends with 0, 2, 4, 6, 8,
etc. If a pointer is 32-bits wide, the partition must start on a memory location with an
address that ends in 0, 4, 8 or C. The easiest way to ensure this is to create a static array as
follows:
void * MyMemArray[n] [m] (n代表内存块的数量,m代表每一个块所带的内存空间的大小)
You should never deallocate memory blocks that were allocated from the heap to prevent
fragmentation of your heap. It is quite acceptable to allocate memory blocks from the
heap as long as the user does not deallocate them.
n_blks
contains the number of memory blocks available from the specified partition. You need to
specify at least two memory blocks per partition.
blk_size
specifies the size (in bytes) of each memory block within a partition. A memory block
must be large enough to hold at least a pointer. Also, the size of a memory block must be
a multiple of the size of a pointer. If a pointer is 32-bits wide then the block size must be
4, 8, 12, 16, 20, etc. bytes (i.e., a multiple of 4 bytes).
p_err
is a pointer to a variable that holds an error code:
OS_ERR_NONE
If the memory partition is created successfully
OS_ERR_ILLEGAL_CREATE_RUN_TIME
If OS_SAFETY_CRITICAL_IEC61508 is defined: you called this after calling OSStart()
and thus you are no longer allowed to create additional kernel objects.
OS_ERR_MEM_CREATE_ISR
If OS_CFG_CALLED_FROM_ISR_CHK_EN is set to DEF_ENABLED in os_cfg.h: if you called
OSMemCreate() from an ISR.
OS_ERR_MEM_INVALID_BLKS
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if the user does not specify
at least two memory blocks per partition
OS_ERR_MEM_INVALID_P_ADDR
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if specifying an invalid
address (i.e., p_addr is a NULL pointer) or the partition is not properly aligned.
OS_ERR_MEM_INVALID_SIZE
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if the user does not specify a
block size that can contain at least a pointer variable, and if it is not a multiple of a
pointer-size variable.
OSMemGet(申请内存控制块)
Description
Obtains a memory block from a memory partition. It is assumed that the application knows the
size of each memory block obtained. Also, the application must return the memory block
[using OSMemPut()] to the same memory partition when it no longer requires it. OSMemGet() may
be called more than once until all memory blocks are allocated.
Files
os.h/os_mem.c
Prototype
void *OSMemGet (OS_MEM *p_mem,
OS_ERR *p_err)
Arguments
p_mem
is a pointer to the desired memory partition control block.
p_err
is a pointer to a variable that holds an error code:
OS_ERR_NONE
If a memory block is available and returned to the application.
OS_ERR_MEM_INVALID_P_MEM
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if p_mem is a NULL pointer.
OS_ERR_MEM_NO_FREE_BLKS
If the memory partition does not contain additional memory blocks to allocate.
OS_ERR_OBJ_TYPE
If OS_CFG_OBJ_TYPE_CHK_EN is set to DEF_ENABLED in os_cfg.h: if the user did not pass
a pointer to a memory partition.
Returned Value
OSMemGet() returns a pointer to the allocated memory block if one is available. If a memory
block is not available from the memory partition, OSMemGet() returns a NULL pointer. It is up to
the application to “cast” the pointer to the proper data type since OSMemGet() returns a void *.
OSMemPut(归还内存控制块)
Description
Returns a memory block back to a memory partition. It is assumed that the user will return the
memory block to the same memory partition from which it was allocated.
Files
os.h/os_mem.c
Prototype
void OSMemPut (OS_MEM *p_mem,
void *p_blk,
OS_ERR *p_err)
Arguments
p_mem
is a pointer to the memory partition control block.
p_blk
is a pointer to the memory block to be returned to the memory partition.
p_err
is a pointer to a variable that holds an error code:
OS_ERR_NONE
If a memory block is available and returned to the application.
OS_ERR_MEM_FULL
If returning a memory block to an already full memory partition. This would indicate
that the user freed more blocks that were allocated and potentially did not return
some of the memory blocks to the proper memory partition.
OS_ERR_MEM_INVALID_P_BLK
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if the user passed a NULL
pointer for the memory block being returned to the memory partition.
OS_ERR_MEM_INVALID_P_MEM
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if p_mem is a NULL pointer.
OS_ERR_OBJ_TYPE
If OS_CFG_OBJ_TYPE_CHK_EN is set to DEF_ENABLED in os_cfg.h: if the user did not pass
a pointer to a memory partition.