?? structalloc.c~
字號:
#include "../include/toyos.h"#include "structAlloc.h"
/* Struct Alloc Manager(結(jié)構(gòu)體分配管理器)
此管理器是為了管理內(nèi)核結(jié)構(gòu)體的動態(tài)管理分配
2008-2-9*/
/* 初始化StructAllocer
start :用于分配STRUCT_ALLOC 的首地址
size :分配空間的大小*/
PUBLIC void initStructAllocer(t_32 start,t_32 size)
{
/*初始化StructAllocer各字段*/
int i=0;
printk("initStructAllocer\n");
structAllocer.start=start;
structAllocer.size=size;
structAllocer.sid=STRUCT_ALLOC;
structAllocer.structSize=sizeof(struct _structAlloc);
for(i=0;i<MAX_STRUCT/8;i++)
structAllocer.bitmap[i]=0;
structAllocer.first=0;
structAllocer.usable=(structAllocer.size/structAllocer.structSize);
structAllocer.alloc=0;
structAllocer.next=NULL;
}
/*
分配新的Struct Alloc
type :結(jié)構(gòu)體的類型
size :結(jié)構(gòu)體的大小
start :用于結(jié)構(gòu)體分配的開始地址
end :用于結(jié)構(gòu)體分配的結(jié)束地址
*/
PUBLIC t32 mallocStructAlloc(t8 type,t32 structSize,t32 start,t32 size)
{
t32 first=structAllocer.first;
t32 index= -1; /*保存分配的序號*/
t8 *temp;
t8 tmp1=1;
structAlloc *newStruct;
structAlloc *next;
int i=0;
int j=0;
/*實(shí)際分配數(shù)已超出系統(tǒng)上限*/
if(structAllocer.alloc+1>structAllocer.usable)
return -1;
/*搜索分配位圖*/
first=first/8;
Allochead:
for(i=first;i<MAX_STRUCT/8;i++)
{
temp=&structAllocer.bitmap[i];
if(*temp==0xff) /*此字節(jié)已分配滿,跳下一字節(jié)*/
continue;
else
{ /*此字節(jié)尚為分配滿,進(jìn)一步確定位置*/
for(j=0;j<=7;j++)
{
if(i*8+j>=structAllocer.usable) /*超出了末尾,從頭開始*/
{
i=0;
break;
}
if(~(((*temp)>>j)&0x1)) /*查找成功*/
{
(*temp)=(*temp)|(tmp1<<j); /*將相應(yīng)位置位*/
index=(i*8)+j;
break;
}
}
break;
}
}
if(index== -1) /*如果此時index0,就從頭搜索*/
{
first=0;
goto Allochead;
}
/*已index為地址分配新結(jié)構(gòu)體,并初始化*/
newStruct=(structAlloc *)(structAllocer.start+(index*structAllocer.structSize));
newStruct->start=start;
newStruct->size=size;
newStruct->sid=type;
newStruct->structSize=structSize;
newStruct->first=0;
newStruct->alloc=0;
newStruct->usable=(newStruct->size)/(newStruct->structSize);
newStruct->next=NULL;
for(i=0;i<MAX_STRUCT/8;i++)
newStruct->bitmap[i]=0;
/*更新structAllocer*/
structAllocer.first=index;
structAllocer.alloc+=1;
/*將新結(jié)構(gòu)體鏈入鏈表*/
next=structAllocer.next;
if(next==NULL)
structAllocer.next=newStruct;
else
{
while(next->next)
next=next->next;
next->next=newStruct;
}
/*返回index*/
return index;
}
/*
釋放Struct Alloc
type :結(jié)構(gòu)體的類型
start :用于結(jié)構(gòu)體分配的開始地址
end :用于結(jié)構(gòu)體分配的結(jié)束地址
*/
PUBLIC t32 freeStructAlloc(t8 type)
{
structAlloc *free=NULL;
structAlloc *next=structAllocer.next;
structAlloc *prev=NULL;
t32 index=0;
t8 *tmp=NULL;
t8 tmp1=0x1;
while(next)
{
if(next->sid==type)
{
free=next;
if(!prev)
structAllocer.next=next->next;
else
prev->next=next->next;
break;
}
else
{
prev=next;
next=next->next;
}
}
if(!free) /*釋放失敗*/
return -1;
/*釋放位圖*/
index=((t32)free-structAllocer.start)/(structAllocer.structSize);
tmp=&structAllocer.bitmap[index/8];
(*tmp)&=(~(tmp1<<(index%8)));
structAllocer.alloc-=1;
return index;
}
/*
分配新的Struct
type :結(jié)構(gòu)體的類型
*/
PUBLIC t32 mallocStruct(t8 type)
{
structAlloc *alloc=NULL;
structAlloc *next=structAllocer.next;
t32 first=0;
t32 index= -1; /*保存分配的序號*/
t8 *temp=NULL;
t8 tmp1=1;
int i=0,j=0;
while(next)
if(next->sid==type)
break;
else
next=next->next;
alloc=next;
if(!alloc) /*分配失敗*/
{
printk("alloc==NULL");
return -2;
}
/*實(shí)際分配數(shù)已超出系統(tǒng)上限*/
if(alloc->alloc+1>alloc->usable)
return -1;
first=alloc->first;
first=first/8;
beginhead:
for(i=first;i<MAX_STRUCT/8;i++)
{
temp=&alloc->bitmap[i];
if(*temp==0xff) /*此字節(jié)已分配滿,跳下一字節(jié)*/
continue;
else
{ /*此字節(jié)尚為分配滿,進(jìn)一步確定位置*/
for(j=0;j<=7;j++)
{
if(i*8+j>=alloc->usable) /*超出了末尾,從頭開始*/
{
i=0;
break;
}
if((~(*temp)>>j)&0x1) /*查找成功*/
{
(*temp)=(*temp)|(tmp1<<j); /*將相應(yīng)位置位*/
index=(i*8)+j;
break;
}
}
break;
}
}
if(index== -1) //如果此時index0,就從頭搜索
{
first=0;
goto beginhead;
}
/*更新*/
alloc->first=index;
alloc->alloc+=1;
return alloc->start+(index*alloc->structSize);
}
/*
分配新的Struct
type :結(jié)構(gòu)體的類型
addr :結(jié)構(gòu)體的首地址
*/
PUBLIC t32 freeStruct(t8 type,t32 addr)
{
structAlloc *free=NULL;
structAlloc *next=structAllocer.next;
t32 index=0;
t8 *tmp=NULL;
t8 tmp1=0x1;
while(next)
if(next->sid==type)
break;
else
next=next->next;
free=next;
if(!free) /*釋放失敗*/
return -1;
if(addr<free->start||addr>(free->start+free->size))
return -1;
/*釋放位圖*/
index=(addr-free->start)/(free->structSize);
tmp=&free->bitmap[index/8];
(*tmp)&=(~(tmp1<<(index%8)));
free->alloc-=1;
/*如果free->alloc==0 還要釋放structAlloc(現(xiàn)在還未實(shí)現(xiàn))*/
return addr;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -