Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 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("... real len of bm(in bytes)=%d\n", realBMPLen);
  122. int currSec = start;
  123.  
  124. //prep bitmap array
  125. char *bitmap;
  126. //bitmap = (char*)calloc(bmpARRLen, sizeof(char));
  127. bitmap = (char*)malloc(realBMPLen);
  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. bzero(buff, SECTOR_SIZE);
  153. }
  154. /*if(nbits*CHARBITS == 1000){
  155. for(int i=0; i <nbits*CHARBITS; i++){
  156. dprintf("%d=%d ", i, test_bit(bitmap, i));
  157. }}*/
  158. int act_size = (int)malloc_usable_size(bitmap);
  159. dprintf("... act size of bmp=%d", act_size);
  160.  
  161. dprintf("... bits for bmp=%d", nbits*CHARBITS);
  162. //search for first bit equal to 0
  163. for(int i =0; i < nbits*CHARBITS; i++)
  164. {
  165. //if equal to 0 set bit and return i
  166. if (test_bit(bitmap, i) == 0)
  167. {
  168. //set ith bit to 1
  169. set_bit(bitmap, i);
  170. found = 1;
  171. firstUnused = i;
  172. }
  173. if(found){
  174. dprintf("... found unused bit in bitmap at index %d, func=bitmap_first_unused\n", firstUnused);
  175. break;
  176. }
  177. }
  178.  
  179. //write new bit map to disk
  180. numBytes = bmpARRLen;
  181. currSec = start;
  182. index = 0;
  183. for(int i = 0; i < numSec; i++)
  184. {
  185. //check if remaining bytes to be copied (numBytes) is less than sector size
  186. if(numBytes <= SECTOR_SIZE)
  187. {
  188. bytesToCopy = numBytes;
  189. }
  190. else
  191. {
  192. bytesToCopy = SECTOR_SIZE;
  193. }
  194. //copy from bitmap to buff
  195. memcpy(buff, &bitmap[index], bytesToCopy);
  196. //write to currSec full bitmap or part of full bitmap
  197. if(Disk_Write(currSec, buff) < 0) return -1;
  198. //update index, beginning of next section of bitmap to copy
  199. index = SECTOR_SIZE * i;
  200. //update remaining number of bytes needing to be written to disk
  201. numBytes -= bytesToCopy;
  202. }
  203. //free allocated memory of bitmap
  204. free(bitmap);
  205.  
  206. //if unused is found return its index, else return -1
  207. if(found)
  208. {
  209. return firstUnused;
  210. }
  211. else
  212. {
  213. dprintf("...no unused bits in bitmap, func=bitmap_first_unused\n");
  214. return -1;
  215. }
  216. }
  217.  
  218. // reset the i-th bit of a bitmap with 'num' sectors starting from
  219. // 'start' sector; return 0 if successful, -1 otherwise
  220. static int bitmap_reset(int start, int num, int ibit)
  221. {
  222. dprintf("... called bitmap_reset\n");
  223. /* YOUR CODE */
  224. char buff[SECTOR_SIZE];
  225. //determine bitmap length
  226. int bmpARRLen = -1;
  227.  
  228. //check if num of bits is a multiple of 8, if there is a remainder then the neccessary length
  229. //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)
  230. bmpARRLen = num*SECTOR_SIZE;
  231.  
  232. //initialize bitmap arrary with length equal to bmpARRLen
  233. char *bitmap;
  234.  
  235. //allocate the neccessary bytes for the num size bitmap
  236. bitmap = (char *)calloc(bmpARRLen, sizeof(char));
  237.  
  238. //determine number of sectors the bitmap occupies
  239. int numSec = num;
  240.  
  241. //check if bitmap only occupies one sector
  242. if(numSec == 1)
  243. {
  244. //bitmap only ooccupies one sector, read the bitmap from start sector
  245. if(Disk_Read(start, buff) < 0) {
  246. dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
  247. return -1;}
  248.  
  249. //copy from buffer to bitmap
  250. memcpy(bitmap, buff, bmpARRLen);
  251. bzero(buff, SECTOR_SIZE);
  252.  
  253. }
  254. else
  255. {
  256. for(int i = 0; i < numSec; i++)
  257. {
  258. int secRd = start + i;
  259. //read from sector
  260. if(Disk_Read(secRd, buff) < 0) {
  261. dprintf("...cannot read bitmap from disk, func=bitmap_reset\n");
  262. return -1;}
  263. //copy to bitmap
  264. int index = i * SECTOR_SIZE;
  265.  
  266. memcpy(&bitmap[index], buff, SECTOR_SIZE);
  267. bzero(buff, SECTOR_SIZE);
  268. }
  269. }
  270.  
  271. if(test_bit(bitmap, ibit) == 0)
  272. {
  273. dprintf("...error cannot clear bit in %d index, func=bitmap_reset\n", ibit);
  274. return -1;
  275. }
  276. else
  277. {
  278. clear_bit(bitmap, ibit);
  279. dprintf("...bit in %d index cleared successfully, func=bitmap_reset\n", ibit);
  280. }
  281. //bytes to transfer from bitmap to buffer then write to disk
  282. int toXfr = bmpARRLen;
  283. //track num bytes to write to disk for each write to disk
  284. int numBytes = -1;
  285. //write bitmap to memory
  286. for (int i = 0; i<= num; i+=SECTOR_SIZE)
  287. {
  288. if(toXfr>SECTOR_SIZE)//num bytes to write larger than sector size
  289. {
  290. numBytes = SECTOR_SIZE;
  291. }
  292. else
  293. {
  294. numBytes = toXfr;
  295. }
  296. memcpy(buff, &bitmap[i], numBytes);//copy to buff numBytes of bitmap
  297. if(Disk_Write(start++, buff)<0)
  298. {
  299. dprintf("...writing bitmap to disk, func=bitmap_reset\n");
  300. return -1;
  301. }
  302. toXfr = toXfr - numBytes;//update number of bytes remaining to write to disk
  303. bzero(buff, SECTOR_SIZE);
  304.  
  305. }
  306.  
  307.  
  308. return 0;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement