fatfs/src/ff.c
static DWORD find_bitmap (	/* 0:Not found, 2..:Cluster block found, 0xFFFFFFFF:Disk error */
	FATFS* fs,	/* Filesystem object */
	DWORD clst,	/* Cluster number to scan from */
	DWORD ncl	/* Number of contiguous clusters to find (1..) */
)
{
	BYTE bm, bv;
	UINT i;
	DWORD val, scl, ctr;


	clst -= 2;	/* The first bit in the bitmap corresponds to cluster #2 */
	if (clst >= fs->n_fatent - 2) clst = 0;
	scl = val = clst; ctr = 0;
	for (;;) {
		if (move_window(fs, fs->bitbase + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF;
		i = val / 8 % SS(fs); bm = 1 << (val % 8);
		do {
			do {
				bv = fs->win[i] & bm; bm <<= 1;		/* Get bit value */
				if (++val >= fs->n_fatent - 2) {	/* Next cluster (with wrap-around) */
					val = 0; bm = 0; i = SS(fs);
				}
				if (bv == 0) {	/* Is it a free cluster? */
					if (++ctr == ncl) return scl + 2;	/* Check if run length is sufficient for required */
				} else {
					scl = val; ctr = 0;		/* Encountered a cluster in-use, restart to scan */
				}
				if (val == clst) return 0;	/* All cluster scanned? */
			} while (bm != 0);
			bm = 1;
		} while (++i < SS(fs));
	}
}
/*--------------------------------------*/
/* Find a contiguous free cluster block */
/*--------------------------------------*/
Find a contiguous free cluster block
Find a contiguous free cluster block
static DWORD find_bitmap (	
	FATFS* fs,	
	DWORD clst,	
	DWORD ncl	
)
{
	BYTE bm, bv;
	UINT i;
	DWORD val, scl, ctr;
	clst -= 2;	
	if (clst >= fs->n_fatent - 2) clst = 0;
	scl = val = clst; ctr = 0;
	for (;;) {
		if (move_window(fs, fs->bitbase + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF;
		i = val / 8 % SS(fs); bm = 1 << (val % 8);
		do {
			do {
				bv = fs->win[i] & bm; bm <<= 1;		
				if (++val >= fs->n_fatent - 2) {	
					val = 0; bm = 0; i = SS(fs);
				}
				if (bv == 0) {	
					if (++ctr == ncl) return scl + 2;	
				} else {
					scl = val; ctr = 0;		
				}
				if (val == clst) return 0;	
			} while (bm != 0);
			bm = 1;
		} while (++i < SS(fs));
	}
}
static DWORD find_bitmap ( FATFS * fs , DWORD clst , DWORD ncl ) { BYTE bm , bv ; UINT i ; DWORD val , scl , ctr ; clst -= 2 ; if ( clst >= fs -> n_fatent - 2 ) clst = 0 ; scl = val = clst ; ctr = 0 ; for ( ; ; ) { if ( move_window ( fs , fs -> bitbase + val / 8 / SS ( fs ) ) != FR_OK ) return 0xFFFFFFFF ; i = val / 8 % SS ( fs ) ; bm = 1 << ( val % 8 ) ; do { do { bv = fs -> win [ i ] & bm ; bm <<= 1 ; if ( ++ val >= fs -> n_fatent - 2 ) { val = 0 ; bm = 0 ; i = SS ( fs ) ; } if ( bv == 0 ) { if ( ++ ctr == ncl ) return scl + 2 ; } else { scl = val ; ctr = 0 ; } if ( val == clst ) return 0 ; } while ( bm != 0 ) ; bm = 1 ; } while ( ++ i < SS ( fs ) ) ; } }
Find a contiguous free cluster block
Find a contiguous free cluster block
/* 0:Not found, 2..:Cluster block found, 0xFFFFFFFF:Disk error */ /* Filesystem object */ /* Cluster number to scan from */ /* Number of contiguous clusters to find (1..) */ /* The first bit in the bitmap corresponds to cluster #2 */ /* Get bit value */ /* Next cluster (with wrap-around) */ /* Is it a free cluster? */ /* Check if run length is sufficient for required */ /* Encountered a cluster in-use, restart to scan */ /* All cluster scanned? */