Skip to content

Commit

Permalink
FIxed a bug in the SimpleFS_remove triggered by many file in the same…
Browse files Browse the repository at this point in the history
… directory. Re formatted files
  • Loading branch information
Nick1296 committed Jul 15, 2018
1 parent a26ded8 commit a5a763d
Show file tree
Hide file tree
Showing 11 changed files with 2,431 additions and 2,486 deletions.
126 changes: 63 additions & 63 deletions bitmap.c
@@ -1,89 +1,89 @@
#include "bitmap.h"
#include "common.h"

void BitMap_init(BitMap* b, int bitmap_blocks, int disk_blocks,int occupation){
//initializing the bitmap
b->entries=(uint8_t*)b+sizeof(BitMap);
b->num_blocks=bitmap_blocks;
b->num_bits=disk_blocks;
uint8_t *bitmap=b->entries;
// we use a mask to sign the occupied blocks
uint8_t mask;
int j,i,write=occupation,written=0;
for(i=0;i<bitmap_blocks;i++){
mask=0;
for(j=0; j<8 && written<write; j++){
mask=mask>>1;
mask+=128;
written++;
}
bitmap[i]=mask;
}
void BitMap_init(BitMap *b, int bitmap_blocks, int disk_blocks, int occupation) {
//initializing the bitmap
b->entries = (uint8_t *) b + sizeof(BitMap);
b->num_blocks = bitmap_blocks;
b->num_bits = disk_blocks;
uint8_t *bitmap = b->entries;
// we use a mask to sign the occupied blocks
uint8_t mask;
int j, i, write = occupation, written = 0;
for (i = 0; i < bitmap_blocks; i++) {
mask = 0;
for (j = 0; j < 8 && written < write; j++) {
mask = mask >> 1;
mask += 128;
written++;
}
bitmap[i] = mask;
}
}


// converts a block index to an index in the array,
// and a the offset of the bit inside the array
BitMapEntryKey BitMap_blockToIndex(int num){
BitMapEntryKey entry;
//we get the entry of the bit map which contains the info about the block
entry.entry_num=num/8;
// we get the displacement inside the block to locate the bit we need
entry.bit_num=num%8;
return entry;
BitMapEntryKey BitMap_blockToIndex(int num) {
BitMapEntryKey entry;
//we get the entry of the bit map which contains the info about the block
entry.entry_num = num / 8;
// we get the displacement inside the block to locate the bit we need
entry.bit_num = (uint8_t) (num % 8);
return entry;
}

// converts a bit to a linear index
int BitMap_indexToBlock(int entry, uint8_t bit_num){
return entry*8+bit_num;
int BitMap_indexToBlock(int entry, uint8_t bit_num) {
return entry * 8 + bit_num;
}

// returns the index of the first bit having status "status"
// in the bitmap bmap, and starts looking from position start
// it returns -1 if it's not found
int BitMap_get(BitMap* bmap, int start, int status){
int i,j,found=0;
int entry=start/8;
// bit that have been already checked/skipped
int checked=start;
//initial displacement in the entry
int starting_bit;
uint8_t mask;
int BitMap_get(BitMap *bmap, int start, int status) {
int i, j, found = 0;
int entry = start / 8;
// bit that have been already checked/skipped
int checked = start;
//initial displacement in the entry
int starting_bit;
uint8_t mask;

for(i=entry;i<bmap->num_blocks && !found;i++){
starting_bit=checked%8;
mask=128;
mask=mask>>starting_bit;
for(j=starting_bit;j<8 && checked<bmap->num_bits && !found;j++){
found=((mask & bmap->entries[i])?1:0)==status;
if(!found){
checked++;
mask=mask>>1;
}
}
}
if(found){
return checked;
}
return FAILED;
for (i = entry; i < bmap->num_blocks && !found; i++) {
starting_bit = checked % 8;
mask = 128;
mask = mask >> starting_bit;
for (j = starting_bit; j < 8 && checked < bmap->num_bits && !found; j++) {
found = ((mask & bmap->entries[i]) ? 1 : 0) == status;
if (!found) {
checked++;
mask = mask >> 1;
}
}
}
if (found) {
return checked;
}
return FAILED;
}

// sets the bit at index pos in bmap to status
int BitMap_set(BitMap* bmap, int pos, int status){
int entry=(pos)/8, bit=(pos)%8;
uint8_t mask=128;
int BitMap_set(BitMap *bmap, int pos, int status) {
int entry = (pos) / 8, bit = (pos) % 8;
uint8_t mask = 128;

mask=mask>>bit;
bmap->entries[entry]= (status)? bmap->entries[entry] | mask : bmap->entries[entry] & ~mask;
mask = mask >> bit;
bmap->entries[entry] = (status) ? bmap->entries[entry] | mask : bmap->entries[entry] & ~mask;

return status;
return status;
}

// return the status of the bit at index pos in bmap
int BitMap_test(BitMap* bmap, int pos){
int entry=pos/8;
int bit=pos%8;
uint8_t mask=128;
mask=mask>>bit;
return (bmap->entries[entry] & mask)? 1:0;
int BitMap_test(BitMap *bmap, int pos) {
int entry = pos / 8;
int bit = pos % 8;
uint8_t mask = 128;
mask = mask >> bit;
return (bmap->entries[entry] & mask) ? 1 : 0;
}
26 changes: 13 additions & 13 deletions bitmap.h
Expand Up @@ -2,21 +2,21 @@

#include <inttypes.h>

typedef struct _BitMap{
int num_bits;
int num_blocks;
uint8_t* entries;
} BitMap;

typedef struct _BitMapEntryKey{
int entry_num;
uint8_t bit_num;
typedef struct _BitMap {
int num_bits;
int num_blocks;
uint8_t *entries;
} BitMap;

typedef struct _BitMapEntryKey {
int entry_num;
uint8_t bit_num;
} BitMapEntryKey;


// initializes the bitmap given an empty BitMap, the number of bitmap_blocks,
// the number of disk blocks covered and the number of already occupied disk blocks
void BitMap_init(BitMap* b, int bitmap_blocks, int disk_blocks, int occupation);
void BitMap_init(BitMap *b, int bitmap_blocks, int disk_blocks, int occupation);

// converts a block index to an index in the array,
// and a char that indicates the offset of the bit inside the array
Expand All @@ -27,11 +27,11 @@ int BitMap_indexToBlock(int entry, uint8_t bit_num);

// returns the index of the first bit having status "status"
// in the bitmap bmap, and starts looking from position start
int BitMap_get(BitMap* bmap, int start, int status);
int BitMap_get(BitMap *bmap, int start, int status);

// sets the bit at index pos in bmap to status
// return status passed on success
int BitMap_set(BitMap* bmap, int pos, int status);
int BitMap_set(BitMap *bmap, int pos, int status);

// return the status of the bit at index pos in bmap
int BitMap_test(BitMap* bmap, int pos);
int BitMap_test(BitMap *bmap, int pos);
3 changes: 2 additions & 1 deletion common.h
@@ -1,4 +1,5 @@
#pragma once

#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
Expand All @@ -11,7 +12,7 @@
#include <signal.h>


#define CHECK_ERR(cond,msg) if (cond) { \
#define CHECK_ERR(cond, msg) if (cond) { \
perror(msg); \
_exit(EXIT_FAILURE); \
}
Expand Down

0 comments on commit a5a763d

Please sign in to comment.