Annotation of coherent/d/usr/bin/elvis/recycle.c, revision 1.1

1.1     ! root        1: /* recycle.c */
        !             2: 
        !             3: /* Author:
        !             4:  *     Steve Kirkendall
        !             5:  *     16820 SW Tallac Way
        !             6:  *     Beaverton, OR 97006
        !             7:  *     [email protected], 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: void 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: #ifndef lint
        !            49:        CLR(0);
        !            50: #endif
        !            51: 
        !            52:        /* blocks needed for current hdr aren't free */
        !            53:        for (i = 1; i < MAXBLKS; i++)
        !            54:        {
        !            55:                CLR(hdr.n[i]);
        !            56:        }
        !            57: 
        !            58:        /* blocks needed for undo version aren't free */
        !            59:        lseek(tmpfd, 0L, 0);
        !            60:        if (read(tmpfd, &oldhdr, (unsigned)sizeof oldhdr) != sizeof oldhdr)
        !            61:        {
        !            62:                msg("garbage() failed to read oldhdr??");
        !            63:                for (i = 0; i < sizeof bitmap; i++)
        !            64:                {
        !            65:                        bitmap[i] = 0;
        !            66:                }
        !            67:                return;
        !            68:        }
        !            69:        for (i = 1; i < MAXBLKS; i++)
        !            70:        {
        !            71:                CLR(oldhdr.n[i]);
        !            72:        }
        !            73: 
        !            74:        /* blocks needed for cut buffers aren't free */
        !            75:        for (i = cutneeds(&oldhdr) - 1; i >= 0; i--)
        !            76:        {
        !            77:                CLR(oldhdr.n[i]);
        !            78:        }
        !            79: }
        !            80: 
        !            81: /* This function allocates the first available block in the tmp file */
        !            82: long allocate()
        !            83: {
        !            84:        int     i;
        !            85:        long    offset;
        !            86: 
        !            87:        /* search for the first byte with a free bit set */
        !            88:        for (i = 0; i < sizeof bitmap && bitmap[i] == 0; i++)
        !            89:        {
        !            90:        }
        !            91: 
        !            92:        /* if we hit the end of the bitmap, return the end of the file */
        !            93:        if (i == sizeof bitmap)
        !            94:        {
        !            95:                offset = lseek(tmpfd, 0L, 2);
        !            96:        }
        !            97:        else /* compute the offset for the free block */
        !            98:        {
        !            99:                for (i <<= 3; TST(i) == 0; i++)
        !           100:                {
        !           101:                }
        !           102:                offset = (long)i * (long)BLKSIZE;
        !           103: 
        !           104:                /* mark the block as "allocated" */
        !           105:                CLR(i);
        !           106:        }
        !           107: 
        !           108:        return offset;
        !           109: }
        !           110: 
        !           111: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.