请高手帮我分析下这段μC/OS-II内存分区建立代码
OS_MEM*OSMemCreate(void*addr,INT32Unblks,INT32Ublksize,INT8U*err){#ifOS_CRITICAL_METH...
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err)
{
#if OS_CRITICAL_METHOD ==3
OS_CPU_SR cpu_sr;
#endif
OS_MEM *pmem;
INT8U *pblk;
Void **plink
INT32U i
#if OS_ARG_CHK_EN>0
If(addr == (void *)0){
*err = OS_MEM_INVALID_ADDR;
return((OS_MEM *)0);
}
If (nblks<2) {
*err = OS_MEM_INVALID_BLKS;
return((OS_MEM *)0);
}
If(blksize < sizeof (void *)){
*err = OS_MEM_INVALID_SIZE;
return((OS_MEM *)0);
}
#endif
OS_ENTER_CRITICAL();
pmem = OSMemFreeList;
If (OSMemFreeList != (OS_MEM *)0){
OSMemFreeList = (OS_MEM *)OSMemFreeList -> OSMemFreeList;
}
OS_EXIT_CRITICAL();
If(pmem == = (OS_MEM *)0)){
*err = OS_MEM_INVALID_PART;
return((OS_MEM *)0);
}
Plink = (void **)addr;
pblk = (INT8U *)addr + blksize;
for (i = 0 ;i<(nblks - 1); i++){
*plink = (void *)pblk;
plink = (void **)pblk;
pblk = pblk + blksize;
}
*plink =(void *)0;
pmem ->OSMemAddr =addr;
pmem ->OSMemFreeList =addr;
pmem ->OSMemNFree =nblks;
pmem ->OSMemNBlks =nblks;
pmem ->OSMemNBlkSize =nblksize;
*err =OS_NO_ERR;
return (pmem);
} 展开
{
#if OS_CRITICAL_METHOD ==3
OS_CPU_SR cpu_sr;
#endif
OS_MEM *pmem;
INT8U *pblk;
Void **plink
INT32U i
#if OS_ARG_CHK_EN>0
If(addr == (void *)0){
*err = OS_MEM_INVALID_ADDR;
return((OS_MEM *)0);
}
If (nblks<2) {
*err = OS_MEM_INVALID_BLKS;
return((OS_MEM *)0);
}
If(blksize < sizeof (void *)){
*err = OS_MEM_INVALID_SIZE;
return((OS_MEM *)0);
}
#endif
OS_ENTER_CRITICAL();
pmem = OSMemFreeList;
If (OSMemFreeList != (OS_MEM *)0){
OSMemFreeList = (OS_MEM *)OSMemFreeList -> OSMemFreeList;
}
OS_EXIT_CRITICAL();
If(pmem == = (OS_MEM *)0)){
*err = OS_MEM_INVALID_PART;
return((OS_MEM *)0);
}
Plink = (void **)addr;
pblk = (INT8U *)addr + blksize;
for (i = 0 ;i<(nblks - 1); i++){
*plink = (void *)pblk;
plink = (void **)pblk;
pblk = pblk + blksize;
}
*plink =(void *)0;
pmem ->OSMemAddr =addr;
pmem ->OSMemFreeList =addr;
pmem ->OSMemNFree =nblks;
pmem ->OSMemNBlks =nblks;
pmem ->OSMemNBlkSize =nblksize;
*err =OS_NO_ERR;
return (pmem);
} 展开
1个回答
展开全部
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err)
{
#if OS_CRITICAL_METHOD ==3 //分配CPU状态存储寄存器
OS_CPU_SR cpu_sr;
#endif
OS_MEM *pmem;
INT8U *pblk;
Void **plink
INT32U i
#if OS_ARG_CHK_EN>0
If(addr == (void *)0){
*err = OS_MEM_INVALID_ADDR;
return((OS_MEM *)0);
}
If (nblks<2) { //至少要有两个存块
*err = OS_MEM_INVALID_BLKS;
return((OS_MEM *)0);
}
If(blksize < sizeof (void *)){ //必须包含至少一个指针空间
*err = OS_MEM_INVALID_SIZE;
return((OS_MEM *)0);
}
#endif
OS_ENTER_CRITICAL();
pmem = OSMemFreeList; //获取下一个空闲内存分区
If (OSMemFreeList != (OS_MEM *)0){ //判断空闲内存分区池是否为空
OSMemFreeList = (OS_MEM *)OSMemFreeList -> OSMemFreeList;
}
OS_EXIT_CRITICAL();
If(pmem == = (OS_MEM *)0)){ //判断如果已有一个内存分区
*err = OS_MEM_INVALID_PART;
return((OS_MEM *)0);
}
Plink = (void **)addr; //所申请内存起始地址转化为指针
pblk = (INT8U *)addr + blksize; //所申请内存的第二个Block起始地址
for (i = 0 ;i<(nblks - 1); i++){ //依次申请nblks个Block地址,链接成单向链表
*plink = (void *)pblk; //给plink指针赋值,使plink指针指向下一个Block地址
plink = (void **)pblk; //更新指针plink,值为下一Block首地址强制转换成的指针
pblk = pblk + blksize; //更新指针pblk,指向下一个Block首地址
}
*plink =(void *)0;
pmem ->OSMemAddr =addr; //存储内存分区的起始位置
pmem ->OSMemFreeList =addr; //初始化指针指向空闲块
pmem ->OSMemNFree =nblks; //存储空闲块
pmem ->OSMemNBlks =nblks; //块个数
pmem ->OSMemNBlkSize =nblksize; //存储每个内存块的块大小
*err =OS_NO_ERR;
return (pmem);
}
{
#if OS_CRITICAL_METHOD ==3 //分配CPU状态存储寄存器
OS_CPU_SR cpu_sr;
#endif
OS_MEM *pmem;
INT8U *pblk;
Void **plink
INT32U i
#if OS_ARG_CHK_EN>0
If(addr == (void *)0){
*err = OS_MEM_INVALID_ADDR;
return((OS_MEM *)0);
}
If (nblks<2) { //至少要有两个存块
*err = OS_MEM_INVALID_BLKS;
return((OS_MEM *)0);
}
If(blksize < sizeof (void *)){ //必须包含至少一个指针空间
*err = OS_MEM_INVALID_SIZE;
return((OS_MEM *)0);
}
#endif
OS_ENTER_CRITICAL();
pmem = OSMemFreeList; //获取下一个空闲内存分区
If (OSMemFreeList != (OS_MEM *)0){ //判断空闲内存分区池是否为空
OSMemFreeList = (OS_MEM *)OSMemFreeList -> OSMemFreeList;
}
OS_EXIT_CRITICAL();
If(pmem == = (OS_MEM *)0)){ //判断如果已有一个内存分区
*err = OS_MEM_INVALID_PART;
return((OS_MEM *)0);
}
Plink = (void **)addr; //所申请内存起始地址转化为指针
pblk = (INT8U *)addr + blksize; //所申请内存的第二个Block起始地址
for (i = 0 ;i<(nblks - 1); i++){ //依次申请nblks个Block地址,链接成单向链表
*plink = (void *)pblk; //给plink指针赋值,使plink指针指向下一个Block地址
plink = (void **)pblk; //更新指针plink,值为下一Block首地址强制转换成的指针
pblk = pblk + blksize; //更新指针pblk,指向下一个Block首地址
}
*plink =(void *)0;
pmem ->OSMemAddr =addr; //存储内存分区的起始位置
pmem ->OSMemFreeList =addr; //初始化指针指向空闲块
pmem ->OSMemNFree =nblks; //存储空闲块
pmem ->OSMemNBlks =nblks; //块个数
pmem ->OSMemNBlkSize =nblksize; //存储每个内存块的块大小
*err =OS_NO_ERR;
return (pmem);
}
东莞市友贸实业有限公司_
2023-11-22 广告
2023-11-22 广告
第五代双倍数据速率DDR5双列直插式内存模块是一种高速、高性能的内存模块,适用于需要高带宽和低延迟的应用场景。与前几代内存模块相比,DDR5双列直插式内存模块具有更高的数据传输速率和更低的功耗,同时提供了更大的内存容量和更高的可靠性。在数据...
点击进入详情页
本回答由东莞市友贸实业有限公司_提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询