📄 mem_admin.c
字号:
}
return NULL; //没有找到相应的空表项
}
/*******************************************************************************************************
**函 数:Fun_JointItem
**功 能:判断是否合并相邻的控制块为一个IDLE控制块
**输 入:*DestCB要进行操作的控制块
**输 出:1 已经被合并入相邻的控制块内了
** -1 没有被合并进相邻的块内
**受影响的全局变量:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int32 Fun_JointItem(PMEMCB DestCB)
{
PMEMCB temp_sp1, temp_sp2, temp_sp0;
temp_sp0 = DestCB;
temp_sp1 = (PMEMCB)DestCB->Mem_LB;
temp_sp2 = (PMEMCB)DestCB->Mem_RB;
if((temp_sp1 != NULL) && (temp_sp2 != NULL))
{
if((temp_sp1->Busy_Flag == IDLE_FLAG) && (temp_sp2->Busy_Flag == IDLE_FLAG))
{
//要把三个控制合Joint为一个Control Block
temp_sp1->spacesize += (temp_sp2->spacesize + temp_sp0->spacesize);
temp_sp1->Mem_RB = temp_sp2->Mem_RB;
if(temp_sp2->Mem_RB != NULL)
{
((PMEMCB)(temp_sp2->Mem_RB))->Mem_LB = (void *)temp_sp1;
}
if(IDLE_LIST->FirstItem == (void *)temp_sp2)
{
((PMEMCB)temp_sp2->Next)->Prev = NULL;
IDLE_LIST->FirstItem = temp_sp2->Next;
}
else if(IDLE_LIST->LastItem == (void *)temp_sp2)
{
((PMEMCB)temp_sp2->Prev)->Next = NULL;
IDLE_LIST->LastItem = temp_sp2->Prev;
}
else
{
((PMEMCB)(temp_sp2->Next))->Prev = temp_sp2->Prev;
((PMEMCB)(temp_sp2->Prev))->Next = temp_sp2->Next;
}
return 1;
}
}
if(temp_sp1 != NULL)
{
if(temp_sp1->Busy_Flag == IDLE_FLAG)
{
temp_sp1->spacesize += temp_sp0->spacesize;
temp_sp1->Mem_RB = temp_sp0->Mem_RB;
//if(temp_sp0->Mem_RB != NULL)
//{
if(temp_sp2 != NULL)
{
temp_sp2->Mem_LB = (void *)temp_sp1;
}
//}
return 1;
}
}
if(temp_sp2 != NULL)
{
if(temp_sp2->Busy_Flag == IDLE_FLAG)
{
temp_sp0->spacesize += temp_sp2->spacesize;
temp_sp0->Busy_Flag = IDLE_FLAG;
temp_sp0->Mem_RB = temp_sp2->Mem_RB;
if(temp_sp0->Mem_RB != NULL)
{
((PMEMCB)(temp_sp0->Mem_RB))->Mem_LB = (void *)temp_sp0;
}
temp_sp0->Next = temp_sp2->Next;
temp_sp0->Prev = temp_sp2->Prev;
if(IDLE_LIST->LastItem == (void *)temp_sp2)
{
IDLE_LIST->LastItem = (void *)temp_sp0;
}
else
{
((PMEMCB)(temp_sp2->Next))->Prev = temp_sp0;
}
if(IDLE_LIST->FirstItem == (void *)temp_sp2)
{
IDLE_LIST->FirstItem = (void *)temp_sp0;
}
else
{
((PMEMCB)(temp_sp2->Prev))->Next = temp_sp0;
}
return 1;
}
}
return -1;
}
/*******************************************************************************************************
**函 数:Fun_Free
**功 能:释放一块已经分配出去的内存块,并与相邻的空闲块合并
**输 入:free_sp 要释放的内存地址
**输 出:NULL 不管失败还是成功
**受影响的全局变量:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void *Fun_Free(void *free_sp)
{
PMEMCB temp_sp;
int xu_ret;
if(free_sp == NULL)
{
return NULL;
}
temp_sp = Fun_FindItemInList(BUSY_LIST, free_sp);
if(temp_sp == NULL)
{
//没有找到相应的表项
return NULL;
}
/* 从原先的链表中清除这个表项 */
if(BUSY_LIST->FirstItem == BUSY_LIST->LastItem)
{
if(BUSY_LIST->FirstItem == (void *)temp_sp)
{
//链表中就这个表项
BUSY_LIST->FirstItem = NULL;
BUSY_LIST->LastItem = NULL;
}
}
else
{
if(BUSY_LIST->FirstItem == (void *)temp_sp)
{
//链表中第一个表项
BUSY_LIST->FirstItem = temp_sp->Next;
((PMEMCB)(temp_sp->Next))->Prev = NULL;
}
else if(BUSY_LIST->LastItem == (void *)temp_sp)
{
BUSY_LIST->LastItem = temp_sp->Prev;
((PMEMCB)(temp_sp->Prev))->Next = NULL;
}
else
{
((PMEMCB)(temp_sp->Prev))->Next = temp_sp->Next;
((PMEMCB)(temp_sp->Next))->Prev = temp_sp->Prev;
}
}
/* 找到相应的表项 要判断是否合并相邻的控制块到IDLE LIST中 */
xu_ret = Fun_JointItem(temp_sp);
if(xu_ret == 1)
{
//发生了合并
return NULL;
}
else
{
//没有发生合并
temp_sp->Busy_Flag = IDLE_FLAG; //设为空闲状态
Sub_ListAdd(IDLE_LIST, temp_sp);
return NULL;
}
}
#ifdef XU_SIM
int Fun_RetBusyList(void) //返回忙链表中表项的个数
{
return Fun_RetList(BUSY_LIST);
}
int Fun_RetIdleList(void) //返回闲链表中表项的个数
{
return Fun_RetList(IDLE_LIST);
}
int Fun_RetList(PLISTHEADER DestListHeader) //返回指定链表的个数
{
PMEMCB temp_sp;
int ret;
ret = 0;
temp_sp = DestListHeader->FirstItem;
if(temp_sp == NULL)
{
return 0;
}
if(DestListHeader->FirstItem == DestListHeader->LastItem)
{
return 1;
}
do
{
ret += 1;
temp_sp = (PMEMCB)temp_sp->Next;
}while((void *)temp_sp != DestListHeader->LastItem);
ret += 1;
return ret;
}
int Fun_RetListItemInfo(PLISTHEADER DestListHeader, int i, int *Next, int *Prev, int *Addr, int *Space, int *LB, int *RB, int *SelfAddr, int *BusyFlag)
{
PMEMCB temp_sp;
int j;
j = 0;
temp_sp = (PMEMCB)DestListHeader->FirstItem;
do
{
if(j == (i - 1))
{
*Next = (int)temp_sp->Next;
*Prev = (int)temp_sp->Prev;
*Addr = (int)temp_sp->Addr;
*Space = (int)temp_sp->spacesize;
*LB = (int)temp_sp->Mem_LB;
*RB = (int)temp_sp->Mem_RB;
*SelfAddr = (int)temp_sp;
*BusyFlag = (int)temp_sp->Busy_Flag;
return 1;
}
else
{
temp_sp = (PMEMCB)temp_sp->Next;
j += 1;
}
}while(temp_sp != NULL);
return 0;
}
int Fun_RetBusyListItemInfo(int i, int *Next, int *Prev, int *Addr, int *Space, int *LB, int *RB, int *SelfAddr, int *BusyFlag)
{
return Fun_RetListItemInfo(BUSY_LIST, i, Next, Prev, Addr, Space, LB, RB, SelfAddr, BusyFlag);
}
int Fun_RetIDLEListItemInfo(int i, int *Next, int *Prev, int *Addr, int *Space, int *LB, int *RB, int *SelfAddr, int *BusyFlag)
{
return Fun_RetListItemInfo(IDLE_LIST, i, Next, Prev, Addr, Space, LB, RB, SelfAddr, BusyFlag);
}
int Fun_RetPhyList(void) //返回物理链表中表项的个数
{
PMEMCB temp_sp;
int count;
temp_sp = ROOT_CB;
count = 0;
while(temp_sp != NULL)
{
count += 1;
temp_sp = (PMEMCB)temp_sp->Mem_RB;
}
return count;
}
int Fun_RetPhyItemInfo(int i, int *Next, int *Prev, int *Addr, int *Space, int *LB, int *RB, int *SelfAddr, int *BusyFlag)
{
PMEMCB temp_sp;
int j;
j = 0;
temp_sp = ROOT_CB;
do
{
if(j == (i - 1))
{
*Next = (int)temp_sp->Next;
*Prev = (int)temp_sp->Prev;
*Addr = (int)temp_sp->Addr;
*Space = (int)temp_sp->spacesize;
*LB = (int)temp_sp->Mem_LB;
*RB = (int)temp_sp->Mem_RB;
*SelfAddr = (int)temp_sp;
*BusyFlag = (int)temp_sp->Busy_Flag;
return 1;
}
else
{
temp_sp = (PMEMCB)temp_sp->Mem_RB;
j += 1;
}
}while(temp_sp != NULL);
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -