/*static char *SCCSID = "@(#)dozap.c    6.1 90/11/17";*/
/*static char *SCCSID = "@(#)dozap.c      13.2 90/01/25";*/
#include <os2.h>

#include "const.h"
#include "chain.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "superb.h"
#include "pb_ifs.h"
#include "pbmacros.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "pr_CACHE.h"
#include "pr_DEBUG.h"
#include "pr_MISC.h"


//@76193  #if defined (DEBUG)

/*
 ***    DoZap - Zap Buffers
 *
 *      DoZap is a debugging routine which is called whenever
 *      a context switch might take place.  DoZap simulates a worst
 *      case context switch by wiping out all unheld and unlocked buffers.
 *      Dirty buffers are flushed first, of course.
 *
 *      Note that BufZap calls FLW who can eventually call back to
 *      BufZap again.  We have a flag to prevent an infinite loop.
 *
 *      ENTRY   none
 *
 *      EXIT    none
 */

#define flFastFile  (FF_FLUSHLAZY | FF_ZAPSEC)
STATIC USHORT InZap = 0;

void far
DoZap (PFRAME pfrm)
{
    USHORT us;
    PBUFNODE pbuf;

    if (InZap++ == 0) {

        if (flFastFile & (FF_FLUSHLAZY | FF_ZAPSEC | FF_CHKSUM)) {

            if (flFastFile & FF_FLUSHLAZY) {
                FLW (pfrm);
            }

            /*  Walk buffer list
             */
            for (us = 0; us < cBufs; us++) {
                pbuf = & abufCache[us];

                /*  If the buffer is not in use and contains reasonable
                 *  data
                 */
                if ((pbuf->cusHolds == 0) &&
                    (pbuf->cLocked == 0) &&
                    (pbuf->lsnSec != 0x11223344L)) {

                    /*  FLW should have flushed it
                     */
                    if (((flFastFile & FF_FLUSHLAZY) != 0) &&
                        (pbuf->bDirtMask != 0)) {
                        INTERR ("Dozap: FLW should have flushed dirty buffer");
                    }

                    /*  Make sure checksums match
                     */
                    if ((flFastFile & FF_CHKSUM) != 0 && !CSC (pbuf))
                        INTERR ("Checksum does not match");

                    /*  Torch buffer contents, forces LSD to reread and
                     *  bogus ASSERT's to fail.
                     */
                    if (flFastFile & FF_ZAPSEC) {
                        pbuf->lsnSec = 0x11223344L;
                        BYTE_FILL (pbuf->pblkData, 'Z', SPB * SECSIZE);
                        if (flFastFile & FF_CHKSUM) {
 // not ready yet!         RSC (pbuf);
                        }
                    }
                }
            }
        }
    }
    InZap--;
}

// #endif
