Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 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. toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
  80.  
  81. //for testing
  82. /*
  83. for(int y = 0; y < numBytes; y++)
  84. {
  85. printf("%d",buff[y]);
  86. }
  87.  
  88. printf("\n");
  89. printf("Copied %d bytes, %d remaining\n", numBytes, toXfr);
  90. */
  91. }
  92. dprintf("...bitmap written to disk using %d sectors to store on disk, func=bitmap_init\n", numSectorUsed);
  93. }
  94. free(bitmap);
  95. dprintf("... mem freed for bitmap, func=bitmap_init\n");
  96. }
  97.  
  98. // set the first unused bit from a bitmap of 'nbits' bits (flip the
  99. // first zero appeared in the bitmap to one) and return its location;
  100. // return -1 if the bitmap is already full (no more zeros)
  101. static int bitmap_first_unused(int start, int num, int nbits)
  102. {
  103. /* YOUR CODE */
  104.  
  105. dprintf("... bitmap_first_unused called\n");
  106.  
  107. char buff[SECTOR_SIZE];
  108.  
  109. //determine number of sectors the bitmap occupies
  110. int secRemain = (nbits / CHARBITS)%SECTOR_SIZE;
  111. //int numSec = (nbits / CHARBITS)/SECTOR_SIZE;
  112. int numSec = num;
  113. dprintf("... numsec=%d\n", numSec);
  114. int found = 0;
  115. int firstUnused = -1;
  116. if(secRemain)
  117. {
  118. numSec += 1;
  119. }
  120.  
  121. //determine number of bytes needed to represent bitmap with nbits
  122. int bmpARRLen = array_size(nbits);
  123. int currSec = start;
  124.  
  125. //prep bitmap array
  126. char *bitmap;
  127. bitmap = (char*)calloc(bmpARRLen, sizeof(char));
  128. int index = 0; //track index in bmp where next chunk from buff will be stored
  129. int numBytes = bmpARRLen; //track remaining bytes to be copied to form complete bitmap
  130. int bytesToCopy = -1;
  131.  
  132. for(int i = 0; i < numSec; i++)
  133. {
  134. //read each sector and build full bitmap
  135. if(Disk_Read(currSec, buff) < 0)
  136. {
  137. dprintf("... could not retrieve bitmap from disk in func bitmap_first_unused\n");
  138. return -1;
  139. }
  140. //copy buff to bitmap
  141. index = SECTOR_SIZE * i;
  142. if(numBytes<=SECTOR_SIZE)
  143. {
  144. bytesToCopy = numBytes;
  145. }
  146. else
  147. {
  148. bytesToCopy = SECTOR_SIZE;
  149. numBytes -= SECTOR_SIZE;
  150. }
  151. memcpy(&bitmap[index], buff, bytesToCopy);
  152. }
  153.  
  154. //search for first bit equal to 0
  155. for(int i =0; i < nbits; i++)
  156. {
  157. //if equal to 0 set bit and return i
  158. if (test_bit(bitmap, i) == 0)
  159. {
  160. //set ith bit to 1
  161. set_bit(bitmap, i);
  162. found = 1;
  163. firstUnused = i;
  164. }
  165. if(found){
  166. dprintf("... found unused bit in bitmap at index %d, func=bitmap_first_unused\n", firstUnused);
  167. break;
  168. }
  169. }
  170.  
  171. //write new bit map to disk
  172. numBytes = bmpARRLen;
  173. currSec = start;
  174. index = 0;
  175. for(int i = 0; i < numSec; i++)
  176. {
  177. //check if remaining bytes to be copied (numBytes) is less than sector size
  178. if(numBytes <= SECTOR_SIZE)
  179. {
  180. bytesToCopy = numBytes;
  181. }
  182. else
  183. {
  184. bytesToCopy = SECTOR_SIZE;
  185. }
  186. //copy from bitmap to buff
  187. memcpy(buff, &bitmap[index], bytesToCopy);
  188. //write to currSec full bitmap or part of full bitmap
  189. if(Disk_Write(currSec, buff) < 0) return -1;
  190. //update index, beginning of next section of bitmap to copy
  191. index = SECTOR_SIZE * i;
  192. //update remaining number of bytes needing to be written to disk
  193. numBytes -= bytesToCopy;
  194. }
  195. //free allocated memory of bitmap
  196. free(bitmap);
  197.  
  198. //if unused is found return its index, else return -1
  199. if(found)
  200. {
  201. return firstUnused;
  202. }
  203. else
  204. {
  205. dprintf("...no unused bits in bitmap, func=bitmap_first_unused\n");
  206. return -1;
  207. }
  208. }
  209.  
  210. // reset the i-th bit of a bitmap with 'num' sectors starting from
  211. // 'start' sector; return 0 if successful, -1 otherwise
  212. static int bitmap_reset(int start, int num, int ibit)
  213. {
  214. dprintf("... called bitmap_reset\n");
  215. /* YOUR CODE */
  216. char buff[SECTOR_SIZE];
  217. //determine bitmap length
  218. int bmpARRLen = -1;
  219.  
  220. //check if num of bits is a multiple of 8, if there is a remainder then the neccessary length
  221. //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)
  222. bmpARRLen = num*SECTOR_SIZE;
  223.  
  224. //initialize bitmap arrary with length equal to bmpARRLen
  225. char *bitmap;
  226.  
  227. //allocate the neccessary bytes for the num size bitmap
  228. bitmap = (char *)calloc(bmpARRLen, sizeof(char));
  229.  
  230. //determine number of sectors the bitmap occupies
  231. int numSec = num;
  232.  
  233. //check if bitmap only occupies one sector
  234. if(numSec == 1)
  235. {
  236. //bitmap only ooccupies one sector, read the bitmap from start sector
  237. if(Disk_Read(start, buff) < 0) {
  238. dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
  239. return -1;}
  240.  
  241. //copy from buffer to bitmap
  242. memcpy(bitmap, buff, bmpARRLen);
  243.  
  244. }
  245. else
  246. {
  247. for(int i = 0; i < numSec; i++)
  248. {
  249. int secRd = start + i;
  250. //read from sector
  251. if(Disk_Read(secRd, buff) < 0) {
  252. dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
  253. return -1;}
  254. //copy to bitmap
  255. int index = i * SECTOR_SIZE;
  256.  
  257. memcpy(&bitmap[index], buff, SECTOR_SIZE);
  258. }
  259. }
  260.  
  261. if(test_bit(bitmap, ibit) == 0)
  262. {
  263. dprintf("...error cannot clear bit in %d index, func=bitmap_reset\n", ibit);
  264. return -1;
  265. }
  266. else
  267. {
  268. clear_bit(bitmap, ibit);
  269. dprintf("...bit in %d index cleared successfully, func=bitmap_reset\n", ibit);
  270. }
  271. //bytes to transfer from bitmap to buffer then write to disk
  272. int toXfr = bmpARRLen;
  273. //track num bytes to write to disk for each write to disk
  274. int numBytes = -1;
  275. //write bitmap to memory
  276. for (int i = 0; i<= num; i+=SECTOR_SIZE)
  277. {
  278. if(toXfr>SECTOR_SIZE)//num bytes to write larger than sector size
  279. {
  280. numBytes = SECTOR_SIZE;
  281. }
  282. else
  283. {
  284. numBytes = toXfr;
  285. }
  286. memcpy(buff, &bitmap[i], numBytes);//copy to buff numBytes of bitmap
  287. if(Disk_Write(start++, buff)<0)
  288. {
  289. dprintf("...writing bitmap to disk, func=bitmap_reset\n");
  290. return -1;
  291. }
  292. toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
  293.  
  294. }
  295.  
  296.  
  297. return 0;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement