Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void bitmap_init(int start, int num, int nbits, int type)
- {
- /* YOUR CODE */
- dprintf("...start bitmap initialization with start=%d, num=%d, nbits=%d\n",start, num, nbits);
- //create bitmap with size of arry of chars neccessary to support num bits
- char *bitmap;
- //get size of bit array in bytes
- //if type then bitmap sector, else inode sector
- int size = -1;
- if(type){
- size = array_size(SECTOR_BITMAP_SIZE * CHARBITS);
- }else{
- size = array_size(INODE_BITMAP_SIZE * CHARBITS);
- }
- //allocate the neccessary bytes for the num size bitmap
- bitmap = (char *)calloc(size, sizeof(char));
- //initialize all bits to 0 (clear all bits)
- for (int i = 0; i < num; i++)
- {
- clear_bit(bitmap,i);
- }
- //for nbits set the bit (bit = 1)
- for (int i = 0; i < nbits; i++)
- {
- set_bit(bitmap,i);
- }
- //set the bits of the sector that thec current bitmap occupies
- for(int i = start; i < start + num; i++) set_bit(bitmap, i);
- //print all bits for testing
- /*
- for (int i = 0; i < num; i++)
- {
- printf("%d: %lu\n",i, test_bit(bitmap,i));
- }
- */
- // check if bitmap will fit in one sector (SECTOR_SIZE > size(bitmap))
- if(SECTOR_SIZE >= size)
- {
- //printf("SECTOR_SIZE >= size -> %d > %lu", SECTOR_SIZE, size);
- if(Disk_Write(start, bitmap)<0)
- {
- dprintf("Error initializing bitmap, func=bitmap_init\n");
- }
- else{
- dprintf("...bitmap wrote to disk successfully using 1 sector to store, func=bitmap_init\n");
- }
- }
- else
- {
- //printf("size > SECTOR_SIZE -> %lu > %d", size, SECTOR_SIZE );
- char buff[SECTOR_SIZE];
- int toXfr = size;//track total bytes to write to disk
- int numBytes = 0;
- int i = -1;
- int numSectorUsed = 0;
- for (i = 0; i<= size; i+=SECTOR_SIZE)
- {
- if(toXfr>SECTOR_SIZE)
- {
- numBytes = SECTOR_SIZE;
- }
- else
- {
- numBytes = toXfr;
- }
- memcpy(buff, &bitmap[i], numBytes);//copy to buff numBytes of bitmap
- if(Disk_Write(start++, buff)<0)
- {
- dprintf("Error initializing bitmap");
- }
- numSectorUsed +=1;
- bzero(buff, SECTOR_SIZE);
- toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
- //for testing
- /*
- for(int y = 0; y < numBytes; y++)
- {
- printf("%d",buff[y]);
- }
- printf("\n");
- printf("Copied %d bytes, %d remaining\n", numBytes, toXfr);
- */
- }
- dprintf("...bitmap written to disk using %d sectors to store on disk, func=bitmap_init\n", numSectorUsed);
- }
- free(bitmap);
- dprintf("... mem freed for bitmap, func=bitmap_init\n");
- }
- // set the first unused bit from a bitmap of 'nbits' bits (flip the
- // first zero appeared in the bitmap to one) and return its location;
- // return -1 if the bitmap is already full (no more zeros)
- static int bitmap_first_unused(int start, int num, int nbits)
- {
- /* YOUR CODE */
- dprintf("... bitmap_first_unused called start=%d, num=%d, nbits=%d\n", start, num, nbits);
- char buff[SECTOR_SIZE];
- //determine number of sectors the bitmap occupies
- //int secRemain = (nbits / CHARBITS)%SECTOR_SIZE;
- //int numSec = (nbits / CHARBITS)/SECTOR_SIZE;
- int numSec = num;
- dprintf("... numsec=%d\n", numSec);
- int found = 0;
- int firstUnused = -1;
- //determine number of bytes needed to represent bitmap with nbits
- int bmpARRLen = array_size(nbits);
- int realBMPLen = nbits;
- dprintf("... real len of bm(in bytes)=%d\n", realBMPLen);
- int currSec = start;
- //prep bitmap array
- char *bitmap;
- //bitmap = (char*)calloc(bmpARRLen, sizeof(char));
- bitmap = (char*)malloc(realBMPLen);
- int index = 0; //track index in bmp where next chunk from buff will be stored
- int numBytes = bmpARRLen; //track remaining bytes to be copied to form complete bitmap
- int bytesToCopy = -1;
- for(int i = 0; i < numSec; i++)
- {
- //read each sector and build full bitmap
- if(Disk_Read(currSec, buff) < 0)
- {
- dprintf("... could not retrieve bitmap from disk in func bitmap_first_unused\n");
- return -1;
- }
- //copy buff to bitmap
- index = SECTOR_SIZE * i;
- if(numBytes<=SECTOR_SIZE)
- {
- bytesToCopy = numBytes;
- }
- else
- {
- bytesToCopy = SECTOR_SIZE;
- numBytes -= SECTOR_SIZE;
- }
- memcpy(&bitmap[index], buff, bytesToCopy);
- bzero(buff, SECTOR_SIZE);
- }
- /*if(nbits*CHARBITS == 1000){
- for(int i=0; i <nbits*CHARBITS; i++){
- dprintf("%d=%d ", i, test_bit(bitmap, i));
- }}*/
- int act_size = (int)malloc_usable_size(bitmap);
- dprintf("... act size of bmp=%d", act_size);
- dprintf("... bits for bmp=%d", nbits*CHARBITS);
- //search for first bit equal to 0
- for(int i =0; i < nbits*CHARBITS; i++)
- {
- //if equal to 0 set bit and return i
- if (test_bit(bitmap, i) == 0)
- {
- //set ith bit to 1
- set_bit(bitmap, i);
- found = 1;
- firstUnused = i;
- }
- if(found){
- dprintf("... found unused bit in bitmap at index %d, func=bitmap_first_unused\n", firstUnused);
- break;
- }
- }
- //write new bit map to disk
- numBytes = bmpARRLen;
- currSec = start;
- index = 0;
- for(int i = 0; i < numSec; i++)
- {
- //check if remaining bytes to be copied (numBytes) is less than sector size
- if(numBytes <= SECTOR_SIZE)
- {
- bytesToCopy = numBytes;
- }
- else
- {
- bytesToCopy = SECTOR_SIZE;
- }
- //copy from bitmap to buff
- memcpy(buff, &bitmap[index], bytesToCopy);
- //write to currSec full bitmap or part of full bitmap
- if(Disk_Write(currSec, buff) < 0) return -1;
- //update index, beginning of next section of bitmap to copy
- index = SECTOR_SIZE * i;
- //update remaining number of bytes needing to be written to disk
- numBytes -= bytesToCopy;
- }
- //free allocated memory of bitmap
- free(bitmap);
- //if unused is found return its index, else return -1
- if(found)
- {
- return firstUnused;
- }
- else
- {
- dprintf("...no unused bits in bitmap, func=bitmap_first_unused\n");
- return -1;
- }
- }
- // reset the i-th bit of a bitmap with 'num' sectors starting from
- // 'start' sector; return 0 if successful, -1 otherwise
- static int bitmap_reset(int start, int num, int ibit)
- {
- dprintf("... called bitmap_reset\n");
- /* YOUR CODE */
- char buff[SECTOR_SIZE];
- //determine bitmap length
- int bmpARRLen = -1;
- //check if num of bits is a multiple of 8, if there is a remainder then the neccessary length
- //of an array to hold num bits is 1 + (number of bits / bits in a char = 8) otherwise its just (number of bits / bits in a char = 8)
- bmpARRLen = num*SECTOR_SIZE;
- //initialize bitmap arrary with length equal to bmpARRLen
- char *bitmap;
- //allocate the neccessary bytes for the num size bitmap
- bitmap = (char *)calloc(bmpARRLen, sizeof(char));
- //determine number of sectors the bitmap occupies
- int numSec = num;
- //check if bitmap only occupies one sector
- if(numSec == 1)
- {
- //bitmap only ooccupies one sector, read the bitmap from start sector
- if(Disk_Read(start, buff) < 0) {
- dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
- return -1;}
- //copy from buffer to bitmap
- memcpy(bitmap, buff, bmpARRLen);
- bzero(buff, SECTOR_SIZE);
- }
- else
- {
- for(int i = 0; i < numSec; i++)
- {
- int secRd = start + i;
- //read from sector
- if(Disk_Read(secRd, buff) < 0) {
- dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
- return -1;}
- //copy to bitmap
- int index = i * SECTOR_SIZE;
- memcpy(&bitmap[index], buff, SECTOR_SIZE);
- bzero(buff, SECTOR_SIZE);
- }
- }
- if(test_bit(bitmap, ibit) == 0)
- {
- dprintf("...error cannot clear bit in %d index, func=bitmap_reset\n", ibit);
- return -1;
- }
- else
- {
- clear_bit(bitmap, ibit);
- dprintf("...bit in %d index cleared successfully, func=bitmap_reset\n", ibit);
- }
- //bytes to transfer from bitmap to buffer then write to disk
- int toXfr = bmpARRLen;
- //track num bytes to write to disk for each write to disk
- int numBytes = -1;
- //write bitmap to memory
- for (int i = 0; i<= num; i+=SECTOR_SIZE)
- {
- if(toXfr>SECTOR_SIZE)//num bytes to write larger than sector size
- {
- numBytes = SECTOR_SIZE;
- }
- else
- {
- numBytes = toXfr;
- }
- memcpy(buff, &bitmap[i], numBytes);//copy to buff numBytes of bitmap
- if(Disk_Write(start++, buff)<0)
- {
- dprintf("...writing bitmap to disk, func=bitmap_reset\n");
- return -1;
- }
- toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
- bzero(buff, SECTOR_SIZE);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement