/*static char *SCCSID = "@(#)clrlzbit.c 6.4 92/01/31";*/

#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsh.h>
#include <strat2.h>

#include "heap.h"
#include "filemode.h"
#include "const.h"
#include "chain.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "pb_ifs.h"
#include "lw.h"
#include "frame.h"
#include "mmphiseg.h"
#include "log.h"
#define  INCL_DOSINFOSEG /* for mmph */
#include "fsdata.h"
#include "pbmacros.h"
#include "asyncio.h"

#include "pr_debug.h"
#include "pr_cache.h"
#include "pr_heap.h"
#include "pr_misc.h"
#include "pr_read.h"
#include "pr_write.h"
#include "mmph.h"
/*
 ***    ClrLzRdBit - Clear bits from a bit map
 *
 *      ClrLzRdBit is called to clear bits from a cache-based lazy-read bit map
 *      to show that the corresponding data bytes in the cache block are valid.
 *      ClrLzBit should be called right after the bytes are allocated before
 *      any blocks have been done.
 *
 *
 *      ENTRY   usFirstToClr = number of first bit/byte to mark valid
 *              cusBitsToClr = # of bits to clear
 *              pbuf = lazy read cache block
 *
 *      EXIT
 *                NONE
 *
 *      HISTORY
 * WHEN     WHO          DEFECT   DESCRIPTION
 * ---------------------------------------------------------------------------
 * 02Aug95  D.Azzarito   @130910  use SFB instead of a "zero" buffer.
 *
 *      NOTE
 *
 */

VOID
ClrLzRdBit (usFirstToClr, cusBitsToClr, pbuf, pfrm)
USHORT usFirstToClr;    /* first bit to clear */
USHORT cusBitsToClr;    /* Number of bits to clear */
PBUFNODE pbuf;
PFRAME pfrm;
{
    PBYTE pbWork;
    UINT  ui;
// @130910 no one is using this    USHORT usRet;

    if (cusBitsToClr == SPB * SECSIZE) {
     /*
      * Entire cache block has been modified;
      * no longer need to allocate lazy read buffer
      */
      RetHeap(pbuf->pLzRdBitMap);

     /* Hold on pbuf not needed since BFL_LZREAD has same effect */
      pbuf->fFlag &= ~BFL_LZREAD;

      cBufLZRD--;

      return;
    }

    pbWork = (PBYTE) pbuf->pLzRdBitMap + (usFirstToClr / 8);

    /*
     * If we are clearing less than 1 byte then, do it now
     * note we want to cast the lsn to a UINT BEFORE doing the %
     * since the result is the same and UINT % is much faster than ULONG %
     */

    if ( (ui = ((UINT)usFirstToClr % 8)) + (UINT)cusBitsToClr < 9 ) {
           *pbWork &= (abFillMask[ui] | ~abFillMask[ui + (UINT)cusBitsToClr]);
    } else {

        /* Clear bits in the first byte */
         *pbWork &= abFillMask[ui];

        /* Clear all the middle bytes */
         memset( ++pbWork, 0, (ui = ((UINT)cusBitsToClr - (8 - ui)) / 8) );
         pbWork += ui;

        /* Clear the leftover bits in the last byte */
         ui = ((UINT)usFirstToClr + (UINT)cusBitsToClr) % 8;
          if ( ui != 0 ) {
            *pbWork &= ~abFillMask[ui];
          }
    }

    pbWork = (PBYTE) pbuf->pLzRdBitMap;

    if (!SFB ( 1, LZ_RD_BIT_MAP_SIZE, pbWork, (PUSHORT)&pbWork))  // @130910
    {
     /*
      * Entire cache block has been modified;
      * no longer need lazy read buffer
      */
      RetHeap(pbuf->pLzRdBitMap);
      pbuf->fFlag &= ~BFL_LZREAD;
      cBufLZRD--;
    }

}

VOID far
ClrLzRdBitFar (usFirstToClr, cusBitsToClr, pbuf, pfrm)
USHORT usFirstToClr;    /* first bit to clear */
USHORT cusBitsToClr;    /* Number of bits to clear */
PBUFNODE pbuf;
PFRAME pfrm;
{
ClrLzRdBit (usFirstToClr, cusBitsToClr, pbuf, pfrm);
}
