int
calc_primes(int max)
{

	int i;
	int j;
	int cnt = 0;
	int size = (max >> 3) + 1;

	bm_primes = (unsigned char *)malloc(size);
	memset(bm_primes, 0, size);

	BM_SET(0, bm_primes);
	BM_SET(1, bm_primes);

	for (i = 2; i <= max; ++i) {
		if (!BM_IS_SET(i, bm_primes)) {
			for (j = 2; (i*j) <= max; ++j) {
				BM_SET((i*j), bm_primes);
			}
		}
	}

	for (i = 2; i <= max; ++i) {
		if (!BM_IS_SET(i, bm_primes)) {
			// printf("Prime: %d\n", i);
			cnt++;
		}
	}

	bm_max = max;

	return(cnt);
}
Exemplo n.º 2
0
void BM_ASSIGN(unsigned char *map, int value, BM_SIZE_T bit_index)
{
	if ( value )
		BM_SET(map, bit_index);
	else
		BM_CLR(map, bit_index);
}
Exemplo n.º 3
0
static int alloc_block(struct filesys *fs)
{
	int bno;

	if((bno = find_free(fs->sb->bm, fs->sb->num_blocks)) == -1) {
		return -1;
	}
	BM_SET(fs->sb->bm, bno);
	return 0;
}
Exemplo n.º 4
0
static int alloc_inode(struct filesys *fs)
{
	int ino;

	if((ino = find_free(fs->sb->ibm, fs->sb->num_inodes)) == -1) {
		return -1;
	}
	BM_SET(fs->sb->ibm, ino);
	return 0;
}
Exemplo n.º 5
0
int mkfs(struct filesys *fs, dev_t dev)
{
	struct superblock *sb;
	struct block_device *bdev;
	int i, bcount;

	if(!(bdev = blk_open(dev))) {
		return -1;
	}
	fs->bdev = bdev;

	if(!(sb = malloc(BLKSZ))) {
		blk_close(bdev);
		return -1;
	}
	fs->sb = sb;

	/* populate the superblock */
	sb->magic = MAGIC;
	sb->ver = FS_VER;
	sb->blksize = BLKSZ;

	sb->num_blocks = bdev->size;
	sb->num_inodes = sb->num_blocks / 4;

	/* inode bitmap just after the superblock */
	sb->ibm_start = 2;
	sb->ibm_count = (sb->num_inodes + BLKBITS - 1) / BLKBITS;
	/* also allocate and initialize in-memory inode bitmap */
	sb->ibm = malloc(sb->ibm_count * BLKSZ);
	assert(sb->ibm);
	memset(sb->ibm, 0, sb->ibm_count * BLKSZ);

	/* XXX mark inode 0 as used always */
	BM_SET(sb->ibm, 0);

	/* block bitmap just after the inode bitmap */
	sb->bm_start = sb->ibm_start + sb->ibm_count;
	sb->bm_count = (sb->num_blocks + BLKBITS - 1) / BLKBITS;
	/* also allocate and initialize in-memory block bitmap */
	sb->bm = malloc(sb->bm_count * BLKSZ);
	assert(sb->bm);
	memset(sb->bm, 0, sb->bm_count * BLKSZ);

	/* inode table, just after the block bitmap */
	sb->itbl_start = sb->bm_start + sb->bm_count;
	sb->itbl_count = (sb->num_inodes * sizeof(struct inode) + BLKSZ - 1) / BLKSZ;

	/* mark all used blocks as used */
	bcount = sb->itbl_start + sb->itbl_count;
	memset(sb->bm, 0xff, bcount / 8);
	for(i=0; i<bcount % 8; i++) {
		int bit = bcount / 8 + i;
		BM_SET(sb->bm, bit);
	}

	/* create the root directory */
	sb->root = newdir(fs, 0);
	sb->root_ino = sb->root->ino;
	/* and write the inode to disk */
	put_inode(fs, sb->root);

	return 0;
}
Exemplo n.º 6
0
int main(int argc, char *argv[])
{
	int ii;
	unsigned char *map;
	int value;
	int index;
	int result;
	bool ok;

	map = BM_ALLOC(HUGE);
	if ( map == NULL )
	{
		fprintf(stderr, "CANNOT ALLOCATE MEMORY FOR BIT MAP OF %u BITS\n", HUGE);
		goto Error;
	}
	else
	{
		printf("ALLOCATED AN ARRAY OF %u BITS\n", HUGE);
	}

	printf("SETTING ARRAY TO ALL '1'S\n");
	BM_ALL(map, 1, HUGE);
	result = BM_TEST(map, 12)                ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", 12, RESULTS('T'), 0, map[0]);

	printf("SETTING ARRAY TO ALL '0'S\n");
	BM_ALL(map, 0, HUGE);
	result = BM_TEST(map, 12)                ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", 12, RESULTS('F'), 0, map[0]);

	// Assign value to the bitmap map at position index:
	value = 11111;
	index = HUGE - 1;  // the bitmap is indexed like a C array.. 0 to size-1
	printf("ASSIGN %d AT %u\n", value, index);
	BM_ASSIGN(map, value, index);
	result = BM_TEST(map, index)             ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", index, RESULTS('T'), index / 8, map[index / 8]);

	value = 0;
	printf("ASSIGN %d AT %u\n", value, index);
	BM_ASSIGN(map, value, index);
	result = BM_TEST(map, index)             ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", index, RESULTS('F'), index / 8, map[index / 8]);

	printf("EVERY EVEN BIT 0, EVERY ODD BIT 1...\n");
	for ( ii = 0; ii < HUGE; ii += 2 )
	{
		BM_CLR(map, ii);
		BM_SET(map, ii + 1);
	}
	result = BM_TEST(map, 3456)              ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", 3456, RESULTS('F'),   3456 / 8, map[3456 / 8]);
	result = BM_TEST(map, 4567)              ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", 4567, RESULTS('T'),   4567 / 8, map[4567 / 8]);
	result = BM_ANY(map, 1, HUGE)             ; printf("  checking for any '1's: %c/%c  %s\n", RESULTS('T'));
	result = BM_ANY(map, 0, HUGE)             ; printf("  checking for any '0's: %c/%c  %s\n", RESULTS('T'));
	ok = TRUE;
	for ( ii = 0; ii < HUGE; ii += 2 )
	{
		if ( ii & 1 )
		{
			if ( ! BM_TEST(map, ii) )       { printf("  FAIL: scan test: bit %u should be 1  byte:%10u is 0x%02x\n", ii, ii / 8, map[ii / 8]); ok = FALSE; break; }
		}
		else
		{
			if (   BM_TEST(map, ii) )       { printf("  FAIL: scan test: bit %u should be 0  byte:%10u is 0x%02x\n", ii, ii / 8, map[ii / 8]); ok = FALSE; break; }
		}
	}
	if ( ok )                                 printf("  scan test...                OK\n");

	printf("EVERY ODD BIT 0, EVERY EVEN BIT 1...\n");
	for ( ii = 0; ii < HUGE; ii += 2 )
	{
		BM_SET(map, ii);
		BM_CLR(map, ii + 1);
	}
	result = BM_TEST(map, 3456)              ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", 3456, RESULTS('T'),   3456 / 8, map[3456 / 8]);
	result = BM_TEST(map, 4567)              ; printf("     bit %12u is %c/%c  %s  byte:%10u is 0x%02x\n", 4567, RESULTS('F'),   4567 / 8, map[4567 / 8]);
	result = BM_ANY(map, 1, HUGE)             ; printf("  checking for any '1's: %c/%c  %s\n", RESULTS('T'));
	result = BM_ANY(map, 0, HUGE)             ; printf("  checking for any '0's: %c/%c  %s\n", RESULTS('T'));
	ok = TRUE;
	for ( ii = 0; ii < HUGE; ii += 2 )
	{
		if ( ii & 1 )
		{
			if (   BM_TEST(map, ii) )       { printf("  FAIL: scan test: bit %u should be 0  byte:%10u is 0x%02x\n", ii, ii / 8, map[ii / 8]); ok = FALSE; break; }
		}
		else
		{
			if ( ! BM_TEST(map, ii) )       { printf("  FAIL: scan test: bit %u should be 1  byte:%10u is 0x%02x\n", ii, ii / 8, map[ii / 8]); ok = FALSE; break; }
		}
	}
	if ( ok )                                 printf("  scan test...                OK\n");

	printf("SETTING ARRAY TO ALL '1'S\n");
	BM_ALL(map, 1, HUGE);
	result = BM_ANY(map, 1, HUGE)             ; printf("  checking for any '1's: %c/%c  %s\n", RESULTS('T'));
	result = BM_ANY(map, 0, HUGE)             ; printf("  checking for any '0's: %c/%c  %s\n", RESULTS('F'));

	printf("SETTING ARRAY TO ALL '0'S\n");
	BM_ALL(map, 0, HUGE);
	result = BM_ANY(map, 1, HUGE)             ; printf("  checking for any '1's: %c/%c  %s\n", RESULTS('F'));
	result = BM_ANY(map, 0, HUGE)             ; printf("  checking for any '0's: %c/%c  %s\n", RESULTS('T'));

	printf("SETTING BIT %u TO '1'\n", HUGE - 2);
	BM_SET(map, HUGE - 2);
	result = BM_ANY(map, 1, HUGE)             ; printf("  checking for any '1's: %c/%c  %s\n", RESULTS('T'));
	result = BM_ANY(map, 0, HUGE)             ; printf("  checking for any '0's: %c/%c  %s\n", RESULTS('T'));

	printf("SETTING ARRAY TO ALL '1'S\n");
	BM_ALL(map, 1, HUGE);
	printf("SETTING BIT %u TO '0'\n", HUGE - 2);
	BM_CLR(map, HUGE - 2);
	result = BM_ANY(map, 1, HUGE)             ; printf("  checking for any '1's: %c/%c  %s\n", RESULTS('T'));
	result = BM_ANY(map, 0, HUGE)             ; printf("  checking for any '0's: %c/%c  %s\n", RESULTS('T'));

	// Freeing space requested: We can just use free(), or for closing the circle:
	BM_FREE(map);
	return 0;

Error:
	return 1;
}