/*static char *SCCSID = "@(#)ccd.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)ccd.c	13.2 90/01/25";*/
#include <os2.h>
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "pb_ifs.h"
#include "superb.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"

#include "pr_DEBUG.h"
#include "pr_MISC.h"


/*
 ***	CCD - Check for Cache DUplicates
 *
 *	CCD makes sure that there's only one copy of each sector
 *	in the cache.  Right now, it assumes that they were hashed
 *	correectly, so it only looks for duplicates within each hash chain.
 *
 */
void far
CCD ()
{

	#ifdef DEBUG

	PBUFNODE pbufCurA, pbufCurB;  /*  Used to traverse chains  */
	USHORT i;

	/*  BUGBUG -- this is not a translation of the assembly
	 *  routine;  this is just a conceptually similar check.
	 *  The assembly routine has a return immediately following
	 *  the beginning of the procedure...
	 */

	/*  Each hash chain is scanned for duplicates in a brute-force
	 *  method:  each buffer on the chain is compared to each
	 *  element beyond it on the chain.
	 */
	for (i = 0;  i < HASHCNT;  i++) {

	    if (apbufHashTable[i] != NULL) {

		pbufCurA = apbufHashTable[i];

		/*  Successively, compare each element on
		 *  hash chain i...
		 */
		do {

		    /*	...to each buffer that appears after it
		     *	on that hash chain.
		     */
		    pbufCurB = pbufCurA -> dcbpHash.pbufFwd;

		    while (pbufCurB != apbufHashTable[i]) {

			if (pbufCurB == pbufCurA)
			    INTERR ("CCD:  Duplicates found on hash chain.");

			pbufCurB = pbufCurB -> dcbpHash.pbufFwd;
		    }

		    pbufCurA = pbufCurA -> dcbpHash.pbufFwd;

		} while (pbufCurA != apbufHashTable[i]);
	    }
	}

	#endif
}
