⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 structalloc.c~

📁 参照MINIX3写的操作系统 用GCC+NASM+BOCHS开发
💻 C~
字号:
#include "../include/toyos.h"#include "structAlloc.h"

/*	Struct Alloc Manager(结构体分配管理器)
	此管理器是为了管理内核结构体的动态管理分配
	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	:结构体的类型
	size	:结构体的大小
	start	:用于结构体分配的开始地址
	end		:用于结构体分配的结束地址

*/
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;

	/*实际分配数已超出系统上限*/
	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)						/*此字节已分配满,跳下一字节*/
			continue;
		else
		{									/*此字节尚为分配满,进一步确定位置*/
			for(j=0;j<=7;j++)
			{
				if(i*8+j>=structAllocer.usable)				/*超出了末尾,从头开始*/
				{
					i=0;
					break;
				}
				if(~(((*temp)>>j)&0x1))			/*查找成功*/
				{
					(*temp)=(*temp)|(tmp1<<j);		/*将相应位置位*/
					index=(i*8)+j;
					break;		
				}
			}
			break;

		}		
	}
	if(index== -1)								/*如果此时index0,就从头搜索*/
	{
		first=0;
		goto Allochead;
	}

	/*已index为地址分配新结构体,并初始化*/
	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;
	/*将新结构体链入链表*/
	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	:结构体的类型
	start	:用于结构体分配的开始地址
	end		:用于结构体分配的结束地址

*/
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	:结构体的类型

*/
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;
	}
	/*实际分配数已超出系统上限*/
	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)								/*此字节已分配满,跳下一字节*/
			continue;					
		else
		{											/*此字节尚为分配满,进一步确定位置*/
			for(j=0;j<=7;j++)
			{

				if(i*8+j>=alloc->usable)				/*超出了末尾,从头开始*/
				{
					i=0;
					break;
				}
				if((~(*temp)>>j)&0x1)				/*查找成功*/
				{
					(*temp)=(*temp)|(tmp1<<j);		/*将相应位置位*/
					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	:结构体的类型
	addr	:结构体的首地址

*/
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(现在还未实现)*/
	return addr;
	
}














⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -