home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1707 / recycle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.3 KB  |  110 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 "config.h"
  16. #include "vi.h"
  17.  
  18. #ifndef NO_RECYCLE
  19. /* this whole file would have be skipped if NO_RECYCLE is defined */
  20.  
  21. extern long    lseek();
  22.  
  23. #define BTST(bitno, byte)    ((byte) & (1 << (bitno)))
  24. #define BSET(bitno, byte)    ((byte) |= (1 << (bitno)))
  25. #define BCLR(bitno, byte)    ((byte) &= ~(1 << (bitno)))
  26.  
  27. #define TST(blkno)        ((blkno) < MAXBIT ? BTST((blkno) & 7, bitmap[(blkno) >> 3]) : 1)
  28. #define SET(blkno)        if ((blkno) < MAXBIT) BSET((blkno) & 7, bitmap[(blkno) >> 3])
  29. #define CLR(blkno)        if ((blkno) < MAXBIT) BCLR((blkno) & 7, bitmap[(blkno) >> 3])
  30.  
  31. /* bitmap of free blocks in first 4096k of tmp file */
  32. static unsigned char bitmap[512];
  33. #define MAXBIT    (sizeof bitmap << 3)
  34.  
  35. /* this function locates all free blocks in the current tmp file */
  36. garbage()
  37. {
  38.     int    i;
  39.     BLK    oldhdr;
  40.  
  41.     /* start by assuming every block is free */
  42.     for (i = 0; i < sizeof bitmap; i++)
  43.     {
  44.         bitmap[i] = 255;
  45.     }
  46.  
  47.     /* header block isn't free */
  48.     CLR(0);
  49.  
  50.     /* blocks needed for current hdr aren't free */
  51.     for (i = 1; i < MAXBLKS; i++)
  52.     {
  53.         CLR(hdr.n[i]);
  54.     }
  55.  
  56.     /* blocks needed for undo version aren't free */
  57.     lseek(tmpfd, 0L, 0);
  58.     if (read(tmpfd, &oldhdr, sizeof oldhdr) != sizeof oldhdr)
  59.     {
  60.         msg("garbage() failed to read oldhdr??");
  61.         for (i = 0; i < sizeof bitmap; i++)
  62.         {
  63.             bitmap[i] = 0;
  64.         }
  65.         return;
  66.     }
  67.     for (i = 1; i < MAXBLKS; i++)
  68.     {
  69.         CLR(oldhdr.n[i]);
  70.     }
  71.  
  72.     /* blocks needed for cut buffers aren't free */
  73.     for (i = cutneeds(&oldhdr) - 1; i >= 0; i--)
  74.     {
  75.         CLR(oldhdr.n[i]);
  76.     }
  77. }
  78.  
  79. /* This function allocates the first available block in the tmp file */
  80. long allocate()
  81. {
  82.     int    i;
  83.     long    offset;
  84.  
  85.     /* search for the first byte with a free bit set */
  86.     for (i = 0; i < sizeof bitmap && bitmap[i] == 0; i++)
  87.     {
  88.     }
  89.  
  90.     /* if we hit the end of the bitmap, return the end of the file */
  91.     if (i == sizeof bitmap)
  92.     {
  93.         offset = lseek(tmpfd, 0L, 2);
  94.     }
  95.     else /* compute the offset for the free block */
  96.     {
  97.         for (i <<= 3; TST(i) == 0; i++)
  98.         {
  99.         }
  100.         offset = (long)i * (long)BLKSIZE;
  101.  
  102.         /* mark the block as "allocated" */
  103.         CLR(i);
  104.     }
  105.  
  106.     return offset;
  107. }
  108.  
  109. #endif
  110.