allocate.h

来自「使用C++编写的数据库管理系统; 拥有自定义数据文件格式」· C头文件 代码 · 共 68 行

H
68
字号
Block * super;
Block * systemTableBlock;
Block  * bitMapNode;

unsigned long findEmpty()
{
	bitMapNode=super;
	unsigned long nextBlock;	
	unsigned long last=0;  
	unsigned char l=0;	
	for(int i=256;i<2044;i++)     //seek in superblock
	{
		unsigned char l=(unsigned char)(bitMapNode->read(1,i));
		if(l!=0xff)			
			return i;		
	}
	

	while(true){
		nextBlock=bitMapNode->read(4,2044);
		bitMapNode->load(nextBlock);
		last=last+2044;
		for(int i=0;i<2044;i++)
		{
			l=(unsigned char)(super->read(1,i));
			if(l!=0xff)   
				return i+last;
		}
	}
}

unsigned long allocateBlock()
{
	unsigned long result=0;
	unsigned long fileLength=super->read(4,0);
	
	unsigned char a=128;
	unsigned char bitMap;
	unsigned long bitLocation=findEmpty();
	if((bitLocation-256)*8>fileLength*0.9)
	{
		//increace file
		Block::writeEmpty(fileLength,(int)(fileLength*0.2));	
		super->write((int)(fileLength*1.2),4,0);
		
	}	
	bitMap=(unsigned char)bitMapNode->read(1,bitLocation%2044);  //set bit map
	int j=0;
	while(bitMap&a)
	{
		a=a>>1;
		j++;
	}
	bitMap=bitMap|a;
	bitMapNode->write(bitMap,1,bitLocation%2044);
	return (bitLocation-256)*8+j;
			
}


/*void revokeBlock(unsigned long n)
{
	unsigned long a,b;
	a=n/8+256;
	b=n%8;
	bitMap=(unsigned char)bitMapNode->read(1,bitLocation%2044);

}*/

⌨️ 快捷键说明

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