// alloc.sr

get "bravo1.df";
get "q.DF";
get "VM.DF";

// Incoming Procedures

external [
	flushvm;
	Enqueue;
	remakevmtb;
	Dequeue;
	umin;
	ugt;
	ult;
	min;
	clearbp;
	];

// Incoming Statics

external [
	dnbp;
	macbp;
	rgvpa;
	rgbs
	rglastused
	];

// Outgoing Procedures

external [
	AllocVm;
	];


// A L L O C V M
//
let AllocVm(qbuf,poolBuf,cbp,flock; numargs na) be
[ if na ls 4 then flock = false
for bp = 0 to macbp-1 do
	clearbp(bp)
cbp = min(cbp,macbp); let tc = 0
qbuf >> Q.head = 0;
let tBuf = poolBuf
// ClearFalloc()
// 	[ 
	for bp = 0 to macbp-1 do
		[ if tc eq cbp then break
// 		if ((rgbs ! bp) << BS.dirty) % ((rgbs ! bp) << BS.falloced) % (rglastused ! bp eq -1) then
// 			loop
		if (rglastused ! bp eq -1) then
			loop
// 		(rgbs ! bp) << BS.falloced = true; 
		tc = tc+1; 
		tBuf >> BUF.ca = dnbp ! bp
		tBuf >> BUF.bp = bp
		tBuf >> BUF.pgn = 0
		Enqueue(qbuf,tBuf)
		tBuf = tBuf+lBuf
		rgvpa ! bp = -1
		if flock then rglastused ! bp = -1
		] 
// 	if tc ls cbp then
// 		loop
// 		] 
// 	break
// 	] repeat
// ClearFalloc()
remakevmtb()
] 

// and ClearFalloc() be
// [ for bp = 0 to macbp-1 do
// 	(rgbs ! bp) << BS.falloced = false; 
// ]