Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. static void bitmap_init(int start, int num, int nbits, int type)
  2. {
  3. /* YOUR CODE */
  4. dprintf("...start bitmap initialization with start=%d, num=%d, nbits=%d\n",start, num, nbits);
  5. //create bitmap with size of arry of chars neccessary to support num bits
  6. char *bitmap;
  7. //get size of bit array in bytes
  8. //if type then bitmap sector, else inode sector
  9. int size = -1;
  10. if(type){
  11. size = array_size(SECTOR_BITMAP_SIZE * CHARBITS);
  12. }else{
  13. size = array_size(INODE_BITMAP_SIZE * CHARBITS);
  14. }
  15. //allocate the neccessary bytes for the num size bitmap
  16. bitmap = (char *)calloc(size, sizeof(char));
  17.  
  18. //initialize all bits to 0 (clear all bits)
  19. for (int i = 0; i < num; i++)
  20. {
  21. clear_bit(bitmap,i);
  22. }
  23.  
  24. //for nbits set the bit (bit = 1)
  25. for (int i = 0; i < nbits; i++)
  26. {
  27. set_bit(bitmap,i);
  28. }
  29.  
  30. //set the bits of the sector that thec current bitmap occupies
  31. for(int i = start; i < start + num; i++) set_bit(bitmap, i);
  32.  
  33. //print all bits for testing
  34. /*
  35. for (int i = 0; i < num; i++)
  36. {
  37. printf("%d: %lu\n",i, test_bit(bitmap,i));
  38. }
  39. */
  40. // check if bitmap will fit in one sector (SECTOR_SIZE > size(bitmap))
  41. if(SECTOR_SIZE >= size)
  42. {
  43. //printf("SECTOR_SIZE >= size -> %d > %lu", SECTOR_SIZE, size);
  44. if(Disk_Write(start, bitmap)<0)
  45. {
  46. dprintf("Error initializing bitmap, func=bitmap_init\n");
  47. }
  48. else{
  49. dprintf("...bitmap wrote to disk successfully using 1 sector to store, func=bitmap_init\n");
  50. }
  51.  
  52.  
  53. }
  54. else
  55. {
  56. //printf("size > SECTOR_SIZE -> %lu > %d", size, SECTOR_SIZE );
  57. char buff[SECTOR_SIZE];
  58. int toXfr = size;//track total bytes to write to disk
  59. int numBytes = 0;
  60. int i = -1;
  61. int numSectorUsed = 0;
  62.  
  63. for (i = 0; i<= size; i+=SECTOR_SIZE)
  64. {
  65. if(toXfr>SECTOR_SIZE)
  66. {
  67. numBytes = SECTOR_SIZE;
  68. }
  69. else
  70. {
  71. numBytes = toXfr;
  72. }
  73. memcpy(buff, &bitmap[i], numBytes);//copy to buff numBytes of bitmap
  74. if(Disk_Write(start++, buff)<0)
  75. {
  76. dprintf("Error initializing bitmap");
  77. }
  78. numSectorUsed +=1;
  79. bzero(buff, SECTOR_SIZE);
  80. toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
  81.  
  82. //for testing
  83. /*
  84. for(int y = 0; y < numBytes; y++)
  85. {
  86. printf("%d",buff[y]);
  87. }
  88.  
  89. printf("\n");
  90. printf("Copied %d bytes, %d remaining\n", numBytes, toXfr);
  91. */
  92. }
  93. dprintf("...bitmap written to disk using %d sectors to store on disk, func=bitmap_init\n", numSectorUsed);
  94. }
  95. free(bitmap);
  96. dprintf("... mem freed for bitmap, func=bitmap_init\n");
  97. }
  98.  
  99. // set the first unused bit from a bitmap of 'nbits' bits (flip the
  100. // first zero appeared in the bitmap to one) and return its location;
  101. // return -1 if the bitmap is already full (no more zeros)
  102. static int bitmap_first_unused(int start, int num, int nbits)
  103. {
  104. /* YOUR CODE */
  105.  
  106. dprintf("... bitmap_first_unused called start=%d, num=%d, nbits=%d\n", start, num, nbits);
  107.  
  108. char buff[SECTOR_SIZE];
  109.  
  110. //determine number of sectors the bitmap occupies
  111. //int secRemain = (nbits / CHARBITS)%SECTOR_SIZE;
  112. //int numSec = (nbits / CHARBITS)/SECTOR_SIZE;
  113. int numSec = num;
  114. dprintf("... numsec=%d\n", numSec);
  115. int found = 0;
  116. int firstUnused = -1;
  117.  
  118. //determine number of bytes needed to represent bitmap with nbits
  119. int bmpARRLen = array_size(nbits);
  120. int realBMPLen = nbits;
  121. dprintf("... bmpARRLen(in bytes)=%d\n", bmpARRLen);
  122. dprintf("... real len of bm(in bytes)=%d\n", realBMPLen);
  123. int currSec = start;
  124.  
  125. //prep bitmap array
  126. char *bitmap;
  127. //bitmap = (char*)calloc(bmpARRLen, sizeof(char));
  128. bitmap = (char*)malloc(realBMPLen);
  129. int index = 0; //track index in bmp where next chunk from buff will be stored
  130. int numBytes = bmpARRLen; //track remaining bytes to be copied to form complete bitmap
  131. int bytesToCopy = -1;
  132.  
  133. for(int i = 0; i < numSec; i++)
  134. {
  135. //read each sector and build full bitmap
  136. if(Disk_Read(currSec, buff) < 0)
  137. {
  138. dprintf("... could not retrieve bitmap from disk in func bitmap_first_unused\n");
  139. return -1;
  140. }
  141. //copy buff to bitmap
  142. index = SECTOR_SIZE * i;
  143. if(numBytes<=SECTOR_SIZE)
  144. {
  145. bytesToCopy = numBytes;
  146. }
  147. else
  148. {
  149. bytesToCopy = SECTOR_SIZE;
  150. numBytes -= SECTOR_SIZE;
  151. }
  152. memcpy(&bitmap[index], buff, bytesToCopy);
  153. bzero(buff, SECTOR_SIZE);
  154. }
  155. /*if(nbits*CHARBITS == 1000){
  156. for(int i=0; i <nbits*CHARBITS; i++){
  157. dprintf("%d=%d ", i, test_bit(bitmap, i));
  158. }}*/
  159. int act_size = (int)malloc_usable_size(bitmap);
  160. dprintf("... act size of bmp=%d", act_size);
  161.  
  162. dprintf("... bits for bmp=%d", nbits*CHARBITS);
  163. //search for first bit equal to 0
  164. for(int i =0; i < nbits*CHARBITS; i++)
  165. {
  166. //if equal to 0 set bit and return i
  167. if (test_bit(bitmap, i) == 0)
  168. {
  169. //set ith bit to 1
  170. set_bit(bitmap, i);
  171. found = 1;
  172. firstUnused = i;
  173. }
  174. if(found){
  175. dprintf("... found unused bit in bitmap at index %d, func=bitmap_first_unused\n", firstUnused);
  176. break;
  177. }
  178. }
  179.  
  180. //write new bit map to disk
  181. numBytes = bmpARRLen;
  182. currSec = start;
  183. index = 0;
  184. for(int i = 0; i < numSec; i++)
  185. {
  186. //check if remaining bytes to be copied (numBytes) is less than sector size
  187. if(numBytes <= SECTOR_SIZE)
  188. {
  189. bytesToCopy = numBytes;
  190. }
  191. else
  192. {
  193. bytesToCopy = SECTOR_SIZE;
  194. }
  195. //copy from bitmap to buff
  196. memcpy(buff, &bitmap[index], bytesToCopy);
  197. //write to currSec full bitmap or part of full bitmap
  198. if(Disk_Write(currSec, buff) < 0) return -1;
  199. //update index, beginning of next section of bitmap to copy
  200. index = SECTOR_SIZE * i;
  201. //update remaining number of bytes needing to be written to disk
  202. numBytes -= bytesToCopy;
  203. }
  204. //free allocated memory of bitmap
  205. free(bitmap);
  206.  
  207. //if unused is found return its index, else return -1
  208. if(found)
  209. {
  210. return firstUnused;
  211. }
  212. else
  213. {
  214. dprintf("...no unused bits in bitmap, func=bitmap_first_unused\n");
  215. return -1;
  216. }
  217. }
  218.  
  219. // reset the i-th bit of a bitmap with 'num' sectors starting from
  220. // 'start' sector; return 0 if successful, -1 otherwise
  221. static int bitmap_reset(int start, int num, int ibit)
  222. {
  223. dprintf("... called bitmap_reset\n");
  224. /* YOUR CODE */
  225. char buff[SECTOR_SIZE];
  226. //determine bitmap length
  227. int bmpARRLen = -1;
  228.  
  229. //check if num of bits is a multiple of 8, if there is a remainder then the neccessary length
  230. //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)
  231. bmpARRLen = num*SECTOR_SIZE;
  232.  
  233. //initialize bitmap arrary with length equal to bmpARRLen
  234. char *bitmap;
  235.  
  236. //allocate the neccessary bytes for the num size bitmap
  237. bitmap = (char *)calloc(bmpARRLen, sizeof(char));
  238.  
  239. //determine number of sectors the bitmap occupies
  240. int numSec = num;
  241.  
  242. //check if bitmap only occupies one sector
  243. if(numSec == 1)
  244. {
  245. //bitmap only ooccupies one sector, read the bitmap from start sector
  246. if(Disk_Read(start, buff) < 0) {
  247. dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
  248. return -1;}
  249.  
  250. //copy from buffer to bitmap
  251. memcpy(bitmap, buff, bmpARRLen);
  252. bzero(buff, SECTOR_SIZE);
  253.  
  254. }
  255. else
  256. {
  257. for(int i = 0; i < numSec; i++)
  258. {
  259. int secRd = start + i;
  260. //read from sector
  261. if(Disk_Read(secRd, buff) < 0) {
  262. dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
  263. return -1;}
  264. //copy to bitmap
  265. int index = i * SECTOR_SIZE;
  266.  
  267. memcpy(&bitmap[index], buff, SECTOR_SIZE);
  268. bzero(buff, SECTOR_SIZE);
  269. }
  270. }
  271.  
  272. if(test_bit(bitmap, ibit) == 0)
  273. {
  274. dprintf("...error cannot clear bit in %d index, func=bitmap_reset\n", ibit);
  275. return -1;
  276. }
  277. else
  278. {
  279. clear_bit(bitmap, ibit);
  280. dprintf("...bit in %d index cleared successfully, func=bitmap_reset\n", ibit);
  281. }
  282. //bytes to transfer from bitmap to buffer then write to disk
  283. int toXfr = bmpARRLen;
  284. //track num bytes to write to disk for each write to disk
  285. int numBytes = -1;
  286. //write bitmap to memory
  287. for (int i = 0; i<= num; i+=SECTOR_SIZE)
  288. {
  289. if(toXfr>SECTOR_SIZE)//num bytes to write larger than sector size
  290. {
  291. numBytes = SECTOR_SIZE;
  292. }
  293. else
  294. {
  295. numBytes = toXfr;
  296. }
  297. memcpy(buff, &bitmap[i], numBytes);//copy to buff numBytes of bitmap
  298. if(Disk_Write(start++, buff)<0)
  299. {
  300. dprintf("...writing bitmap to disk, func=bitmap_reset\n");
  301. return -1;
  302. }
  303. toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
  304. bzero(buff, SECTOR_SIZE);
  305.  
  306. }
  307.  
  308.  
  309. return 0;
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement