home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / recycle < prev    next >
Encoding:
Text File  |  1989-12-31  |  2.2 KB  |  109 lines

  1. /* recycle.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    16820 SW Tallac Way
  6.  *    Beaverton, OR 97006
  7.  *    kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This file contains the functions perform garbage collection and allocate
  12.  * reusable blocks.
  13.  */
  14.  
  15. #include "vi.h"
  16.  
  17. #ifndef NO_RECYCLE
  18. /* this whole file would have be skipped if NO_RECYCLE is defined */
  19.  
  20. extern long    lseek();
  21.  
  22. #define BTST(bitno, byte)    ((byte) & (1 << (bitno)))
  23. #define BSET(bitno, byte)    ((byte) |= (1 << (bitno)))
  24. #define BCLR(bitno, byte)    ((byte) &= ~(1 << (bitno)))
  25.  
  26. #define TST(blkno)        ((blkno) < MAXBIT ? BTST((blkno) & 7, bitmap[(blkno) >> 3]) : 1)
  27. #define SET(blkno)        if ((blkno) < MAXBIT) BSET((blkno) & 7, bitmap[(blkno) >> 3])
  28. #define CLR(blkno)        if ((blkno) < MAXBIT) BCLR((blkno) & 7, bitmap[(blkno) >> 3])
  29.  
  30. /* bitmap of free blocks in first 4096k of tmp file */
  31. static unsigned char bitmap[512];
  32. #define MAXBIT    (sizeof bitmap << 3)
  33.  
  34. /* this function locates all free blocks in the current tmp file */
  35. garbage()
  36. {
  37.     int    i;
  38.     BLK    oldhdr;
  39.  
  40.     /* start by assuming every block is free */
  41.     for (i = 0; i < sizeof bitmap; i++)
  42.     {
  43.         bitmap[i] = 255;
  44.     }
  45.  
  46.     /* header block isn't free */
  47.     CLR(0);
  48.  
  49.     /* blocks needed for current hdr aren't free */
  50.     for (i = 1; i < MAXBLKS; i++)
  51.     {
  52.         CLR(hdr.n[i]);
  53.     }
  54.  
  55.     /* blocks needed for undo version aren't free */
  56.     lseek(tmpfd, 0L, 0);
  57.     if (read(tmpfd, &oldhdr, sizeof oldhdr) != sizeof oldhdr)
  58.     {
  59.         msg("garbage() failed to read oldhdr??");
  60.         for (i = 0; i < sizeof bitmap; i++)
  61.         {
  62.             bitmap[i] = 0;
  63.         }
  64.         return;
  65.     }
  66.     for (i = 1; i < MAXBLKS; i++)
  67.     {
  68.         CLR(oldhdr.n[i]);
  69.     }
  70.  
  71.     /* blocks needed for cut buffers aren't free */
  72.     for (i = cutneeds(&oldhdr) - 1; i >= 0; i--)
  73.     {
  74.         CLR(oldhdr.n[i]);
  75.     }
  76. }
  77.  
  78. /* This function allocates the first available block in the tmp file */
  79. long allocate()
  80. {
  81.     int    i;
  82.     long    offset;
  83.  
  84.     /* search for the first byte with a free bit set */
  85.     for (i = 0; i < sizeof bitmap && bitmap[i] == 0; i++)
  86.     {
  87.     }
  88.  
  89.     /* if we hit the end of the bitmap, return the end of the file */
  90.     if (i == sizeof bitmap)
  91.     {
  92.         offset = lseek(tmpfd, 0L, 2);
  93.     }
  94.     else /* compute the offset for the free block */
  95.     {
  96.         for (i <<= 3; TST(i) == 0; i++)
  97.         {
  98.         }
  99.         offset = (long)i * (long)BLKSIZE;
  100.  
  101.         /* mark the block as "allocated" */
  102.         CLR(i);
  103.     }
  104.  
  105.     return offset;
  106. }
  107.  
  108. #endif
  109.