
	INCLUDE	'exec/types.i'
	INCLUDE	'exec/exec.i'
	INCLUDE 'exec/execbase.i'

	xref	_OldAllocMem
	xref	_OldFreeMem
	xref	_KPutFmt
	xref	_KPutS
	xref	_LVOForbid
	xref	_LVOPermit
	xref	_LVOSetSR
	xref	_LVOAlert

C_CALLER	equ	8
A_CALLER	equ	4


	xdef	_SnoopAllocMem
_SnoopAllocMem:
		move.l	a2,-(sp)

		jsr	_LVOForbid(a6)		; only one print stack
		lea	printStack,a2
		move.l	ThisTask(a6),a0
		move.l  LN_NAME(a0),-(a2)
		move.l	#' ',-(a2)

		move.l  C_CALLER(sp),-(a2)
		move.l  A_CALLER(sp),-(a2)
		movem.l	d0/d1,-(a2)

		move.l	_OldAllocMem,a0
		jsr	(a0)

		move.l	d0,-(a2)
		pea	(a2)
		pea	AMFmt(pc)
		jsr	_KPutFmt
		addq.l	#8,sp

		bsr	AssertUserMode

		move.l	(a2),d0

		jsr	_LVOPermit(a6)

		move.l	(sp)+,a2
		rts

AMFmt:
	dc.b	'$%06lx=AllocMem(%6ld,$%05lx) '
	dc.b	'A:%06lx C:%06lx %lc "%s"',10,0
	ds.w	0


	xdef	_SnoopFreeMem
_SnoopFreeMem:
		move.l	a2,-(sp)

		jsr	_LVOForbid(a6)		; only one print stack
		lea	printStack,a2
		move.l	ThisTask(a6),a0
		move.l  LN_NAME(a0),-(a2)
		move.l	#' ',-(a2)

		move.l  C_CALLER(sp),-(a2)
		move.l  A_CALLER(sp),-(a2)
		move.l	D0,-(a2)
		move.l	A1,-(a2)

		move.l	_OldFreeMem,a0
		jsr	(a0)

		move.l	d0,-(a2)
		pea	(a2)
		pea	FMFmt(pc)
		jsr	_KPutFmt
		addq.l	#8,sp

		bsr	AssertUserMode

		move.l	(a2),d0
		jsr	_LVOPermit(a6)
		move.l	(sp)+,a2

		rts


AssertUserMode:
		moveq.l	#0,d0
		moveq.l	#0,d1
		jsr	_LVOSetSR(a6)
		btst.l	#13,d0
		beq.s	1$
		pea	msgSuperCrash(PC)
		jsr	_KPutS
		move.l	#$88880000,d7
		jmp	_LVOAlert(a6)
1$:		rts

FMFmt
	dc.b	'$%06lx= FreeMem($%06lx,%5ld) '
	dc.b	'A:%06lx C:%06lx %lc "%s"',10,0

msgSuperCrash:
	dc.b	10,'*** SUPERVISOR MODE MEMORY FUNCTION ***',10,0
	ds.w	0

	DATA

	ds.l	512
printStack


	END
