home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / filesy~1 / mfs6011s.zoo / minixfs / bitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-25  |  2.8 KB  |  142 lines

  1. /* This file is part of 'minixfs' Copyright 1991,1992,1993 S.N.Henson */
  2.  
  3. /* Bitmap handling stuff */
  4.  
  5. #include "minixfs.h"
  6. #include "proto.h"
  7. #include "global.h"
  8.  
  9. long count_bits(buf,num)
  10. ushort *buf;
  11. long num;
  12. {
  13.     long i,count;
  14.     count = 0;
  15.     for(i = 0; i < num/16; i++) {
  16.         if(buf[i]==65535U)count+=16;
  17.         else count+=bitcount(buf[i]);
  18.     }
  19.     count += bitcount((ushort)(buf[num/16] & (( 1l<< (num%16)) -1l)));
  20.     return(count);
  21. }
  22.  
  23. int bitcount(wrd)
  24. unsigned int wrd;
  25. {
  26.     ushort i;
  27.     int count;
  28.  
  29.     count=0;
  30.     for (i = 1; i != 0; i <<= 1)
  31.       if (wrd & i)
  32.         count++;    
  33.     return(count);
  34. }
  35.  
  36. long alloc_zone(drive)
  37. int drive;
  38. {
  39.     long save;
  40.  
  41.     super_info *psblk=super_ptr[drive];
  42.  
  43.     if( !(save=alloc_bit(psblk->zbitmap,psblk->sblk.s_zones
  44.             -psblk->sblk.s_firstdatazn+1,psblk->zlast)) )
  45.         return 0;
  46.     psblk->zdirty=1; /* Mark zone bitmap as dirty */
  47.     if(save>psblk->zlast)psblk->zlast=save;
  48.     return(save+psblk->sblk.s_firstdatazn-1);
  49. }
  50.  
  51. unshort alloc_inode(drive)
  52. int drive;
  53. {
  54.     ushort save;    
  55.     super_info *psblk=super_ptr[drive];
  56.  
  57.     if(!(save=alloc_bit(psblk->ibitmap,psblk->sblk.s_ninodes+1l,
  58.                                 psblk->ilast)))
  59.         return 0;
  60.     psblk->idirty=1;    /* Mark inode bitmap as dirty */
  61.     if(save>psblk->ilast)psblk->ilast=save;
  62.     return(save);
  63. }
  64.  
  65. /* Release a zone */
  66. int free_zone(zone,drive)
  67. long zone;
  68. int drive;
  69. {
  70.     super_info *psblk=super_ptr[drive];
  71.     long save,ret;
  72.     save=zone+1-psblk->sblk.s_firstdatazn;
  73.     ret=free_bit(psblk->zbitmap,save);
  74.     psblk->zdirty=1; /* Mark zone bitmap as dirty */
  75.     if(save<psblk->zlast)psblk->zlast=save;
  76.     if(!ret) ALERT("Drive %d zone %ld freeing already free zone !",drive,zone);
  77.     return(ret);
  78. }
  79.  
  80. /* Release an inode */
  81. int free_inode(inum,drive)
  82. unsigned inum;
  83. int drive;
  84. {
  85.     long ret;
  86.     super_info *psblk=super_ptr[drive];
  87.     ret=free_bit(psblk->ibitmap,inum);
  88.     if(inum<psblk->ilast)psblk->ilast=inum;
  89.     psblk->idirty=1;    /* Mark inode bitmap as dirty */
  90.     if(!ret) ALERT("Drive %d inode %d , freeing already free inode!",drive,inum);
  91.     return(ret);
  92. }
  93.  
  94.  
  95. /* This routine is used for allocating both free inodes and free zones 
  96.  * Search a bitmap for a zero , then return its bit number and change it
  97.  * to a one ...... but without exceeding 'num' bits 
  98.  */
  99.  
  100. long alloc_bit(buf,num,last)
  101. ushort *buf;
  102. long num,last;
  103. {
  104.     long i,j,k;
  105.  
  106.     k=1;
  107.     for(i=last>>4;(i<=(num>>4)) && (buf[i]==65535U);i++)
  108.         ;
  109.     if( i > (num>>4) )
  110.         return(0);
  111.     else {
  112.         for(j=0;j<16;j++) {
  113.             if(!(buf[i] & k)) {
  114.                 long free;
  115.                 free=i*16+j;
  116.                 if(free>=num)return 0;
  117.                 buf[i]|=k;
  118.                 return(free);
  119.             }
  120.             k<<=1;
  121.         }
  122.     }
  123.     ALERT("minixfs: alloc_bit: This can't happen !");
  124.     return 0;
  125. }
  126.  
  127. /* zero a bit of a bitmap return 0 if already zero */
  128.  
  129. long free_bit(buf,bitnum)
  130. ushort *buf;
  131. long bitnum;
  132. {
  133.     register long index=bitnum>>4;
  134.     register ushort bit = 1 << (bitnum & 15);
  135.     long ret;
  136.  
  137.     ret=buf[index] & bit;
  138.     buf[index]&= ~bit;
  139.     return(ret);
  140. }
  141.  
  142.