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

📄 block.c

📁 一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上
💻 C
字号:
/*************************************************************************
* Copyright  2002 National ASIC Center, All right Reserved
*
* FILE NAME: block.c
* PROGRAMMER: DSA
* Date of Creation: 2002/07/24
* DESCRIPTION: The operation about Block.
*	       		Include the init, get, free,
*	       		belong to file system manager.
*
* NOTE:
*
* FUNCTIONS LIST:
* -------------------------------------------------------------------------
* init_area_array()
* get_block()
* free_block()
* find_block()
*
*-------------------------------------------------------------------------
* MODIFICATION HISTORY
*
* 2002/07/24 by DSA Creation of this file
* 2002/07/29 by DSA test this file
* 2002/09/04 by DSA Modify this file in function free_block.
******************************************************************************/
//#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <math.h>
//#include <time.h>

#include <filesys\fs.h>
#include "block.h"
#include <filesys\list.h>

#define NUM 12
#define PERCENT 4 /* 4/8 */
AREA_S area[NUM];/* this number can be knowed */
#if 0
/* #define FLASH_START_ADDRESS 0x */

DEV_DSP_S device_dispcription[] = {
	/* devid, 	rbufsize,  	rbuf,		start,	read, 	write*/
	{FLASH_DEVID,  0,		NULL, 		 0,	drv_read, drv_write},
	{ICCARD_DEVID, 256,	  	(unsigned char *)iccard_rbuf, 0,	drv_read, drv_write},
	{0,				0,		NULL,		0,		NULL,	NULL}
};

const AREA_DSP_S area_dispcription[] = {
	/* devid, 	start,	size */
	{FLASH_DEVID,  (unsigned int)area_1,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_2,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_3,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_4,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_5,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_6,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_7,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_8,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_9,	FLASH_AREA_SIZE},
	{FLASH_DEVID,  (unsigned int)area_10,	FLASH_AREA_SIZE},
	{ICCARD_DEVID, (unsigned int)area_11,	ICCARD_AREA_SIZE},
	{ICCARD_DEVID, (unsigned int)area_12,	ICCARD_AREA_SIZE},
	{0,				0,						0}
};

/*just for test*/
//const PARTITION partition[] = {
//	/* start,	num */
//	{0,	2},
//	{2,	3},
//	{5,	2},
//	{7,	3},
//	{10,2}
//};
#endif

int init_area_array()
{
	register AREA_S *q;
	register AREA_DSP_S const *t;
	unsigned char *p;
	unsigned char m;
	unsigned int bitmapsize;
	unsigned int count,n;

#define READ(a) device_dispcription[(a)->devid].read

	t = area_dispcription;
	q = area;
	while ( t->size )
	{
		/* calculate bytes of bitmap */
		bitmapsize = (unsigned int)((t->size/BLOCKSIZE)>>3);
		
		/* malloc bitmap */
		if ((p=(unsigned char *)FS_MALLOC(bitmapsize))==NULL)
		{
//			PRINT_ERROR("malloc memory is failed!\n");
			for (q--;q!=area;q--)
				FS_FREE((unsigned int *)q->blkbitmap);
			return FS_ERROR;
		}
		q->blkbitmap = (unsigned int *)p;
 		
		/* read bitmap from device */
		(READ(t))(p, t->start, bitmapsize);
		
		/* calculate free block num of area */
		for ( count=0,n=0; n<bitmapsize; n++,p++)
			for ( m=0x01; m&0xFF; m<<=1 )
				if ( *p&m ) count++;
				
		INIT_LIST_HEAD(&q->buf);
		q->blkfreenum = count;
		q->phyinfo = *t;
		q->max_nblknum = bitmapsize*PERCENT;
		q->nblknum = 0;
		
		t++; q++;
	}
	return FS_OK;
}

//int get_block(PARTITION const* partition )
int get_block(int partitionid )
{
	register AREA_S *q;
	register AREA_DSP_S const *t;
	unsigned int m,n;
	unsigned char *p;
	unsigned int bitmapsize,blkid;
	PARTITION *partition = &Partition[partitionid];
	
	q = &area[ partition->start ];
	for ( m=0; m<partition->num; m++,q++ )
		if( q->blkfreenum != 0 )
			break;

	if ( m==partition->num )
	{
//		PRINT_MSG("the free block can not be searched!\n");
		return FS_ERROR;
	}
	
	t = &area_dispcription[ partition->start + m ];
	/* calculate bytes of bitmap */
	bitmapsize = (unsigned int)((t->size/BLOCKSIZE)>>3);
	
	blkid = t->start;
	p = (unsigned char *)q->blkbitmap;
	for ( m = 0; m < bitmapsize; m++,p++ )
	{
		if ( *p != 0x0 )
		{
			blkid += m<<3;
			for ( n=0x80; n&0xFF; n>>=1)
			{
				if ( *p&n )
				{
					*p &= ~n;			/*from 1 to 0*/
					q->blkfreenum--;
					return t->start + (blkid - t->start) * BLOCKSIZE;
				}else
					blkid++;
			}
		}
	}
	return FS_ERROR;
}


int free_block( unsigned int blockID, unsigned int *areaaddr, unsigned int **data )
{
	unsigned int j,m;
	unsigned char *p;
	register AREA_DSP_S const *t;
	register AREA_S *q;

	t = area_dispcription;
	q = area;

	while( t->size )
	{
		if( t->start <= blockID && blockID < t->start+t->size )
		{
			*areaaddr = t->start;
			*data = q->blkbitmap;
//			j = (blockID - t->start)/BLOCKSIZE+1;//02-9-4 15:26
			j = (blockID - t->start)/BLOCKSIZE;
			m = j>>3;
//			j = (unsigned char)(1<<(8-(j%8)));//02-9-4 15:26
			j = (unsigned char)(1<<(7-(j%8)));
			p = (unsigned char *)q->blkbitmap + m;
			
			if( *p&j )
				return FS_OK;
			*p|=j;					/*from 0 to 1*/
			q->blkfreenum++;
			return FS_OK;
		}
		t++; q++;
	}
	return FS_ERROR;
}


AREA_S* find_area( unsigned int blockID )
{
	register AREA_DSP_S const *t;
	register AREA_S *q;

	t = area_dispcription;
	q = area;
	while( t->size ){
		if( t->start <= blockID && blockID < t->start+t->size )
			return q;
		t++;
		q++;
	}
	return NULL;
}


int modify_block_status( unsigned char *phybuf, unsigned int blockID, unsigned int status )
{
	register AREA_DSP_S const *t;
	register AREA_S *q;
	unsigned int n,bitmapsize;
	unsigned char j;

	t = area_dispcription;
	q = area;
	while( t->size ){
		bitmapsize = (unsigned int)((t->size/BLOCKSIZE)>>3);
		if( t->start <= blockID && blockID < t->start+t->size ){
			n = ( blockID - t->start ) / BLOCKSIZE;
			phybuf += (n / 8);
			if( status == 1){
				j = (unsigned char)(1<<(7-(n%8)));
				*phybuf|=j;
			}
			else{
				j = (unsigned char)(0x80>>(n%8));
				*phybuf&=~j;
			}
			return FS_OK; 
		}
		t++;
		q++;
	}
	return FS_ERROR;
}
/*just for test*/
//main( )
//{
//	int init_ok,i,j,m;
//	PARTITION*  part;
//	unsigned int blockid[500];
//	unsigned int bitmapsize;
//	unsigned long off;
//	unsigned int state1,state2,state3;
//	time_t timer1,timer2,timer3;

//	part = &partition;
	
	/*init */
//	for(i = 0; i < NUM; i++)
//	{
//		if( i < 10 )
//			bitmapsize = FLASH_AREA_SIZE / BLOCK_SIZE;
//		else
//			bitmapsize = ICCARD_AREA_SIZE / BLOCK_SIZE;
//		switch(i){
//			case 0:
//			case 1:
//			case 2:
//			case 3:
//			case 4:
//			case 5:
//			case 6:
//			case 9:
//			case 10:
//			case 11:
//				memset((unsigned char *)area_dispcription[i].start,0x00,1);
//				memset((unsigned char *)(area_dispcription[i].start+1),0xff,bitmapsize/8-1);
//			case 7:
//			case 8:
//				memset((unsigned char *)(area_dispcription[i].start),0x00,bitmapsize/8);
//			default:
//				break;
//		}
//	}
//	init_ok = init_area_array();
//	if( init_ok == ERROR )printf(" init failed!\n");
	/*get block*/
/*	timer1 = time( NULL );
	printf(" timer1 is %ld \n", timer1 );*/
//	get_start();

/*	for(m = 0; m < 30; m++){*/
//		for(j = 0; j < 30000; j++){
//			for(i = 0; i < 100; i++){
				//blockid[i] = get_block(&partition[i]);
//				blockid[i] = get_block( part );
//				blockid[i] = get_block( &part[4] );
			//		printf(" blockid[%i] is %i \n", i, blockid[i] );
//			}
//			for(i = 0; i < 100; i++){
//				free_block( blockid[0] + BLOCK_SIZE * i);
//			}
//		}
/*	}*/

//	get_end();
//	off = get_off();
/*	timer2 = time( NULL );
	printf(" timer2 is %ld \n", timer2 );

	for(m = 0; m < 30; m++){
		for(j = 0; j < 30000; j++){
			for(i = 0; i < 100; i++){
				free_block( blockid[0] + BLOCK_SIZE * i);
			}
		}	
	}
	
	state1 = free_block( blockid[0] );
	state2 = free_block( blockid[2] + BLOCK_SIZE );
	state3 = free_block( blockid[2] + BLOCK_SIZE * 512 );//this blockid is invalid !
	if( state1 == OK ) printf("free block is OK!\n");
	if( state2 == OK ) printf("free block is OK!\n");
	if( state3 == OK ) printf("free block is OK!\n");

	timer3 = time( NULL );
	printf(" timer3 is %ld \n", timer3 );
	printf(" timer2 - timer1 is %ld \n", timer2 - timer1 );
	printf(" timer3 - timer2 is %ld \n", timer3 - timer1 );
}*/

⌨️ 快捷键说明

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