**********************************************************************************
* Client.s	26/MAR/92	written by Laurence Vanhelsuwé
* --------
*
* This is an example program to demonstrate the usage of the new MEMF_VM allocation
* option with AllocMem().
*
**********************************************************************************

		include	std
		include	exec/vmemory.i		;note: V_memory

PAGES		equ	VMEM_PAGESIZE

START:		move.l	4.w,a6
		lea	dosname,a1
		moveq	#0,d0
		EXEC	OpenLibrary		;get DOS
		move.l	d0,DOS_LIB_PTR

;------------------------------------------------
		move.l	#4*1024*1024,d0		;give me 4 Megabytes
		move.l	#MEMF_VM,d1		;of Virtual Memory please !
		EXEC	AllocMem
		move.l	d0,d7
		beq	failed_alloc
;---------------
		move.l	d7,a0			;-> VM block
		move.b	(a0),d0			;access page 0

		add.l	#32768,a0		;goto page 2
		move.l	16382(a0),-16384(a0)	;demand page 2,3 and 1
	
		move.l	DOS_LIB_PTR,a6
		move.l	#fname,d1
		move.l	#MODE_OLDFILE,d2	;open a file that's N bytes long
		DOS	Open
		move.l	d0,fhandle

		move.l	fhandle,d1
		move.l	d7,d2			;load entire file into VM !
		move.l	#46507,d3
		DOS	Read

		move.l	fhandle,d1
		DOS	Close			;release file

		move.l	a6,a1
		move.l	4.w,a6			;release DOS
		EXEC	CloseLibrary
;---------------
done_VM		move.l	4.w,a6
		move.l	#4*1024*1024,d0		;release 4 Megabytes
		move.l	d7,a1
		EXEC	FreeMem
		rts
;---------------
	IFD bla
		move.l	#20*PAGES,d0
		move.l	#MEMF_VM,d1
		EXEC	AllocMem
		move.l	d0,ptr1

		move.l	#30*PAGES,d0
		move.l	#MEMF_VM,d1
		EXEC	AllocMem
		move.l	d0,ptr2

		move.l	#15*PAGES,d0
		move.l	#MEMF_VM,d1
		EXEC	AllocMem
		move.l	d0,ptr3

		move.l	#7*PAGES,d0
		move.l	#MEMF_VM,d1
		EXEC	AllocMem
		move.l	d0,ptr4

		move.l	#42*PAGES,d0
		move.l	#MEMF_VM,d1
		EXEC	AllocMem
		move.l	d0,ptr5
;---------------
		move.l	#15*PAGES,d0		;3rd alloc first
		move.l	ptr3,a1
		EXEC	FreeMem

		move.l	#20*PAGES,d0		;1st alloc next
		move.l	ptr1,a1
		EXEC	FreeMem
		
		move.l	#7*PAGES,d0		;4th next
		move.l	ptr4,a1
		EXEC	FreeMem

		move.l	#30*PAGES,d0		2nd next
		move.l	ptr2,a1
		EXEC	FreeMem

		move.l	#42*PAGES,d0		;last last
		move.l	ptr5,a1
		EXEC	FreeMem
	ENDC

failed_alloc	rts				;back to CLI (phew!)
;------------------------------------------------
ptr1		ds.l	1
ptr2		ds.l	1
ptr3		ds.l	1
ptr4		ds.l	1
ptr5		ds.l	1

fhandle		ds.l	1
DOS_LIB_PTR	ds.l	1

fname		dc.b	"DOCS:HANDSHAKE.DOC",0

dosname		DOSNAME

		END
