שתשת                              VERSION		= 10
REVISION	= 2
	incdir	asmone:
	include exec/libraries.i
	include	exec/resident.i
	include	exec/memory.i
	include	devpacmacros.i
	include	rtracker.i
main
	moveq	#-1,d0
	rts

;***************************************************************************
;
; the resident part
;
;***************************************************************************
resident:
	dc.w	RTC_MATCHWORD
	dc.l	resident
	dc.l	EndResident
	dc.b	$80
	dc.b	VERSION
	dc.b	9
	dc.b	0
	dc.l	ExLibName
	dc.l	ExLibID
	dc.l	InitTab

EndResident

;****************************************************************************
;
; the library base
;
;
	STRUCTURE RTrackerBase,LIB_SIZE
	  APTR	  segList
	  APTR    rt_SysBase
	  APTR    rt_IntBase
	  APTR    rt_DosBase
	  APTR    rt_GadBase
	  APTR    nodem
	  APTR    nodel
	  APTR    nodef
	  APTR    nodep
	  APTR    nodeg
	  APTR    nodee
	  APTR    ptrPool	  
	  LABEL   rt_SIZE	
;****************************************************************************
; constant
;**********
ExLibName	dc.b	'rtracker.library',0
ExLibID		dc.b	'rtracker.library 10.2 (05.08.99)',0
InitTab		dc.l	rt_SIZE
		dc.l	FuncTable
		dc.l	DataTable
		dc.l	InitLib

;****************************************************************************
;
; the functions
;
;

FuncTable	dc.l	OpenLib
		dc.l	CloseLib
		dc.l	ExpungeLib
		dc.l	ExtFuncLib
		dc.l	InitResource	
		dc.l	CloseResource
		dc.l	CloseTaskResource
		dc.l	NewAllocMem
		dc.l	NewFreeMem
		;* next 2 are for NewAllocVec
		;* and NewFreeVec
		dc.l	NewAllocMem
		dc.l	NewFreeMem
		dc.l	NewOpenLibrary
		dc.l	NewCloseLibrary
		dc.l	NewOpenFile
		dc.l	NewCloseFile
		dc.l	ExtFuncLib	; originaly: lister
		dc.l	ExtFuncLib	; CreatePool    \
		dc.l	ExtFuncLib	; DeletePool     \ This will never be added
		dc.l	ExtFuncLib	; AllocPooled    /
		dc.l	ExtFuncLib	; FreePooled    /
		dc.l	NewCreateContext	; fonctionnel
		dc.l	NewCreateGadget	; ok
		dc.l	NewFreeGadgets	; ok
		dc.l	NewGetVisInfo	; ok
		dc.l	NewFreeVisualInfo	
		dc.l	NewCreateMsgPort
		dc.l	NewDeleteMsgPort
		dc.l	NewCreateIORequest
		dc.l	NewDeleteIORequest
		dc.l	$ffffffff
	even

DataTable	dc.w	$e000,8,$900
		dc.b	$80,10
		dc.l	ExLibName
		dc.w	$e000,$e
		dc.w	$600
		dc.w	$d000,20,VERSION
		dc.w	$d000,22,REVISION
		dc.b	$80,24
		dc.l	ExLibID
		dc.l	0

	even
;**********************************************************************
;
; Initlib (opens library)
; P:	a6 = SysBase
; 	a0 = seglist
;	d0 = RTBase

InitLib:
	move.l	d0,-(a7)
	move.l	d0,a1
	move.l	4,rt_SysBase(a1)
	move.l	a0,segList(a1)
	bsr	RT_OpenLibs
	beq.s	err_OpLibs
	move.l	(a7)+,d0
	rts
err_OpLibs:	; can't open libs
	move.l	(a7)+,a1
	moveq	#0,d0
	move.w	LIB_NEGSIZE(a1),d0
	add.w	LIB_POSSIZE(a1),d0
	moveq	#0,d1
	move.w	LIB_NEGSIZE(a1),d1
	sub.l	d1,a1
	jsr	_LVOFreeMem(a6)
	moveq	#0,d0
	rts
;**********************************************************************
;
; OpenLibs
; d0 = rt_Base

RT_OpenLibs	
	movem.l	a5,-(a7)
	move.l	4,a6
	move.l	d0,a5	
	; open intuition
	lea.l	IntName,a1
	moveq	#37,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,rt_IntBase(a5)
	beq	noLib
	;open dos
	lea.l	DosName,a1
	moveq	#37,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,rt_DosBase(a5)
	beq	noLib
	; open gadtools
	lea.l	GadName,a1
	moveq	#37,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,rt_GadBase(a5)
	beq	noLib
	moveq	#-1,d0
	movem.l	(a7)+,a5
	rts	
	
noLib	moveq	#0,d0
	movem.l	(a7)+,a5
	rts

RT_CloseLibs
	move.l	rt_IntBase(a6),d0
	beq.s	closeDos
	move.l	d0,a1
	movem.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCloseLibrary(a6)
	movem.l	(a7)+,a6
closeDos
	move.l	rt_DosBase(a6),d0
	beq.s	closeGad
	move.l	d0,a1
	movem.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCloseLibrary(a6)
	movem.l	(a7)+,a6
closeGad
	move.l	rt_GadBase(a6),d0
	beq.s	closeEnd
	move.l	d0,a1
	movem.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCloseLibrary(a6)
	movem.l	(a7)+,a6
closeEnd
	moveq	#0,d0
	rts
IntName	dc.b	'intuition.library',0
DosName	dc.b	'dos.library',0
GadName	dc.b	'gadtools.library',0
	even

OpenLib
	addi.w	#1,LIB_OPENCNT(a6) ; opening library
	and.b	#$f7,LIB_FLAGS(a6) ; remove delexp flag
	move.l	a6,d0
	rts
	
CloseLib
	subi.w	#1,LIB_OPENCNT(a6) ; closing library
	bne.s	noDelExp
	move.b	LIB_FLAGS(a6),d0
	and.b	#$8,d0
	beq	noDelExp
	bsr	ExpungeLib
	rts
noDelExp
	moveq	#0,d0
	rts

ExpungeLib
	tst.w	LIB_OPENCNT(a6)
	bne.s	noExpunge
	;**********************************************
	;*
	;* CloseTaskRsc(FindTask(NULL))
	;*
	;**********************************************
	move.l	segList(a6),-(a7)
	move.l	a6,a1
	movem.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVORemove(a6)
	movem.l	(a7)+,a6
	bsr	RT_CloseLibs
	move.l	a6,a1
	moveq	#0,d0
	move.w	LIB_NEGSIZE(a1),d0
	add.w	LIB_POSSIZE(a1),d0
	moveq	#0,d1
	move.w	LIB_NEGSIZE(a1),d1
	sub.l	d1,a1
	move.l	rt_SysBase(a6),a6
	jsr	_LVOFreeMem(a6)
	move.l	(a7)+,d0
	rts
noExpunge
ExtFuncLib
	moveq	#0,d0
	rts



	  

;***********************************************************************
;*
;* public functions
;*
;*


;***********************************************************************
;*
;* executed just after the library is opened
;*
InitResource
	movem.l	a5,-(a7)
	move.l	a6,a5
	move.l	rt_SysBase(a6),a6
	tst.l	nodem(a5)
	bne.w	allocNodF
	move.l	#NM_SIZE,d0
	move.l	#MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,nodem(a5)
allocNodF
	tst.l	nodef(a5)
	bne.s	allocNodL
	move.l	#NF_SIZE,d0
	move.l	#MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,nodef(a5)
allocNodL
	tst.l	nodel(a5)
	bne.s	allocNodE
	move.l	#NL_SIZE,d0
	move.l	#MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,nodel(a5)
allocNodE
	tst.l	nodee(a5)
	bne.s	allocNodG
	move.l	#NE_SIZE,d0
	move.l	#MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,nodee(a5)
allocNodG
	tst.l	nodeg(a5)
	bne.s	endAlloc
	move.l	#NG_SIZE,d0
	move.l	#MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,nodeg(a5)
endAlloc
	move.l	a5,a6
	movem.l	(a7)+,a5
	moveq	#0,d0
	rts

;*********************************************************************
;*
;* Called at the end of the program
;* Free all remaining allocations
;*
;           /\
;          /  \
;         / !! \
;        /  !!  \    Rajouter les CloseAllxxxxxxxx
;       /   ..   \   
;      /__________\

CloseResource
	movem.l	a5/a4,-(a7)
	move.l	a6,a5
	move.l	rt_SysBase(a6),a6
	subi.l	a1,a1
	jsr	_LVOFindTask(a6)

CloseRsc:
	exg	a5,a6


	movem.l	a2,-(a7)
	move.l	d0,a2

	; nb: all CloseAllxxx trash d0/d1 & a0/a1

	move.l	a2,a1
	jsr	CloseAllLib

	move.l	a2,a1
	jsr	CloseAllFiles

	move.l	a2,a1
	jsr	FreeAllGads

	move.l	a2,a1
	jsr	DeleteAllExec

	move.l	a2,a1
	jsr	FreeAllMem

	movem.l	(a7)+,a2

	exg	a5,a6


	tst.l	nodem(a5)
	beq.s	freeNodF
	move.l	nodem(a5),a4
	tst.l	M_nextNode(a4)
	bne.s	freeNodF
	move.l	a4,a1
	jsr	_LVOFreeVec(a6)
	move.l	#0,nodem(a5)
freeNodF
	tst.l	nodef(a5)
	beq.s	freeNodL
	move.l	nodef(a5),a4
	tst.l	F_nextNode(a4)
	bne.s	freeNodL
	move.l	a4,a1
	jsr	_LVOFreeVec(a6)
	move.l	#0,nodef(a5)
freeNodL
	tst.l	nodel(a5)
	beq.s	freeNodE
	move.l	nodel(a5),a4
	tst.l	L_nextNode(a4)
	bne.s	freeNodE
	move.l	a4,a1
	jsr	_LVOFreeVec(a6)
	move.l	#0,nodel(a5)
freeNodE
	tst.l	nodee(a5)
	beq.s	freeNodG
	move.l	nodee(a5),a4
	tst.l	E_nextNode(a4)
	bne.s	freeNodG
	move.l	a4,a1
	jsr	_LVOFreeVec(a6)
	move.l	#0,nodee(a5)
freeNodG
	tst.l	nodeg(a5)
	beq.s	endFree
	move.l	nodeg(a5),a4
	tst.l	G_nextNode(a4)
	bne.s	endFree
	move.l	a4,a1
	jsr	_LVOFreeVec(a6)
	move.l	#0,nodeg(a5)
endFree
	moveq	#0,d0
	move.l	a5,a6
	movem.l	(a7)+,a5/a4
	rts

;***********************************************************************
;*
;* if you want to free voluntarly some prog (or all) resources
;*
CloseTaskResource
	movem.l	a5/a4,-(a7)
	move.l	a6,a5
	move.l	rt_SysBase(a6),a6
	moveq	#0,d0
	bra	CloseRsc
;***********************************************************************
;*
;* some utilities functions
;*
getTask:
	movem.l	d1/a0-a1/a6,-(a7)

	move.l	rt_SysBase(a6),a6
	subi.l	a1,a1
	jsr	_LVOFindTask(a6)

	movem.l	(a7)+,d1/a0-a1/a6
	rts
;***********************************************************************
;*
;* find a node, return the precedent found node
;*
;* In:
;* d0: data to find
;* a0: the first node list to search in
;*
;* Out:
;* d0: ptr to found node
;*
;* a0,a1: trashed
;*
findNode:
	bra.s	startSearch
searchNext
	move.l	(a0),a0
startSearch
	move.l	(a0),a1
	tst.l	a1
	beq.s	endFindNode
	cmp.l	4(a1),d0
	bne.s	searchNext

	move.l	a0,d0
	rts

endFindNode:
	moveq	#0,d0
	rts

;**********************************************************************
;**********************************************************************
;**********************************************************************
;
; *     * ****** *     *  ***  ****  *     *
; **   ** *      **   ** *   * *   *  *   *
; * * * * ***    * * * * *   * ****    * *
; *  *  * *      *  *  * *   * *   *    *
; *     * ****** *     *  ***  *    *   *
;
;**********************************************************************
;*
;* This allocate some memory
;* In:
;* d0: bytezise
;* d1: type
;*
;* Out:
;* d0: ptr tothe allocated bloc
NewAllocMem
	movem.l	d2-d4,-(a7)
	move.l	a6,-(a7)
	tst.l	nodem(a6)
	beq.s	noMemNode
	move.l	rt_SysBase(a6),a6
	move.l	d0,d3
	move.l	d1,d4
	move.l	#NM_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,d2
	beq.s	noMemNode
	move.l	d3,d0
	move.l	d4,d1
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	beq.s	noMemNode
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d3,M_size(a0)
	move.l	d0,M_bloc(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,M_task(a0)

	move.l	nodem(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d4
	rts
noMemNode
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d4
	moveq	#0,d0
	rts

;*****************************************************************************
;*
;* this free the memory previously allocated
;*
;* In:
;* A1: ptr to the memory bloc
;*
NewFreeMem
	movem.l	a2/a6,-(a7)
	tst.l	nodem(a6)
	beq.s	endFreeMem
	move.l	a1,d0
	beq.s	endFreeMem
	move.l	nodem(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endFreeMem
	
	move.l	d0,a0
	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	M_size(a1),d0
	move.l	M_bloc(a1),a1
	move.l	rt_SysBase(a6),a6
	jsr	_LVOFreeMem(a6)

	move.l	a2,a1
	jsr	_LVOFreeVec(a6)

endFreeMem
	movem.l	(a7)+,a2/a6
	rts

;**************************************************************************
;*
;* Free all task remaining memory allocation
;*
;* a1: task bloc (if 0 then free all)
;*
FreeAllMem:
	tst.l	nodem(a6)
	beq.w	endFreeAllMem
	tst.l	a1
	beq.s	FreeAllTaskMem
	move.l	nodem(a6),a0
	
nextFreeNode1
	move.l	a0,d0
	move.l	(a0),a0
nextFreeNode2
	tst.l	a0
	beq.s	endFreeAllMem
	cmpa.l	M_task(a0),a1
	bne.s	nextFreeNode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2


	movem.l	d0/a0/a1/a6,-(a7)
	move.l	M_bloc(a0),a1
	move.l	M_size(a0),d0
	move.l	rt_SysBase(a6),a6
	jsr	_LVOFreeMem(a6)
	movem.l	(a7)+,d0/a0/a1/a6
	move.l	(a0),d1
	movem.l	d0/d1/a1/a6,-(a7)
	move.l	a0,a1
	move.l	rt_SysBase(a6),a6
	jsr	_LVOFreeVec(a6)
	movem.l	(a7)+,d0/d1/a1/a6
	move.l	d1,a0
	bra.s	nextFreeNode2


	rts

FreeAllTaskMem
	movem.l	a2-a4/a6,-(a7)
	move.l	nodem(a6),a2
	move.l	rt_SysBase(a6),a6
freeNextAllTM
	move.l	(a2),a4
	tst.l	a4
	beq.s	endFreeAllTM
	move.l	(a4),(a2)
	move.l	a4,a1
	move.l	M_size(a1),d0
	move.l	M_bloc(a1),a1
	jsr	_LVOFreeMem(a6)
	move.l	a4,a1
	jsr	_LVOFreeVec(a6)
	bra.s	freeNextAllTM

endFreeAllTM
	movem.l	(a7)+,a2-a4/a6
endFreeAllMem
	rts

;**********************************************************************
;**********************************************************************
;**********************************************************************
;
; *     ***** ****   ****
; *       *   *   * *
; *       *   ****   ****
; *       *   *   *      *
; *       *   *   *      *
; ***** ***** ****  *****    
;
;**********************************************************************
;*
;* This opens a library
;* In:
;* d0: version
;* a1: libsname
;*
;* Out:
;* d0: ptr to the opened lib
NewOpenLibrary
	movem.l	d2-d5,-(a7)
	move.l	a6,-(a7)
	tst.l	nodel(a6)
	beq.s	noLibNode
	move.l	d0,d4
	move.l	a1,d5
	move.l	#NL_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d2
	beq.s	noLibNode
	move.l	d4,d0
	move.l	d5,a1
	move.l	rt_SysBase(a6),a6
	jsr	_LVOOpenLibrary(a6)
	tst.l	d0
	beq.s	noLibNode
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d0,L_library(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,L_task(a0)

	move.l	nodel(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d5
	rts
noLibNode
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d5
	moveq	#0,d0
	rts

;*****************************************************************************
;*
;* this closes a library
;*
;* In:
;* A1: ptr to the lib
;*
NewCloseLibrary
	movem.l	a2,-(a7)
	tst.l	nodel(a6)
	beq.s	endCloseLib
	move.l	a1,d0
	beq.s	endCloseLib
	move.l	nodel(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endCloseLib
	
	move.l	d0,a0
	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	L_library(a1),a1
	move.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCloseLibrary(a6)
	move.l	(a7)+,a6
	
	move.l	a2,a1
	jsr	NewFreeMem

endCloseLib
	movem.l	(a7)+,a2
	rts

;**************************************************************************
;*
;* Free all task remaining library allocation
;*
;* a1: task bloc (if 0 then free all)
;*
CloseAllLib:
	tst.l	nodel(a6)
	beq.w	endCloseAllLib
	tst.l	a1
	beq.s	CloseAllTaskLib
	move.l	nodel(a6),a0
	
nextCloseLNode1
	move.l	a0,d0
	move.l	(a0),a0
nextCloseLNode2
	tst.l	a0
	beq.s	endCloseAllLib
	cmpa.l	L_task(a0),a1
	bne.s	nextCloseLNode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2


	movem.l	d0/a0/a1/a6,-(a7)
	move.l	L_library(a0),a1
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCloseLibrary(a6)
	movem.l	(a7)+,d0/a0/a1/a6
	move.l	(a0),d1
	movem.l	d0/d1/a1,-(a7)
	move.l	a0,a1
	jsr	NewFreeMem
	movem.l	(a7)+,d0/d1/a1
	move.l	d1,a0
	bra.s	nextCloseLNode2


	rts

CloseAllTaskLib
	movem.l	a2-a4/a5-a6,-(a7)
	move.l	a6,a5
	move.l	nodel(a6),a2
	move.l	rt_SysBase(a6),a6
closeNextAllTL
	move.l	(a2),a4
	tst.l	a4
	beq.s	endCloseAllTL
	move.l	(a4),(a2)
	move.l	a4,a1
	move.l	L_library(a1),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	a4,a1
	exg	a5,a6
	jsr	NewFreeMem
	exg	a5,a6
	bra.s	closeNextAllTL

endCloseAllTL
	movem.l	(a7)+,a2-a4/a4-a6
endCloseAllLib
	rts

;**********************************************************************
;**********************************************************************
;**********************************************************************
;
; ***** ***** *     *****  ****
; *       *   *     *     *
; ***     *   *     **     ****
; *       *   *     *          *
; *       *   *     *          *
; *     ***** ***** ***** *****
;
;**********************************************************************
;*
;* This opens a file
;* In:
;* d1: name
;* d2: mode
;*
;* Out:
;* d0: ptr to the opened file
NewOpenFile
	movem.l	d2-d5,-(a7)
	move.l	a6,-(a7)
	tst.l	nodef(a6)
	beq.s	noFilNode
	move.l	d1,d4
	move.l	d2,d5
	move.l	#NF_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d3
	beq.s	noFilNode
	move.l	d4,d1
	move.l	d5,d2
	move.l	rt_DosBase(a6),a6
	jsr	_LVOOpen(a6)
	tst.l	d0
	beq.s	noFilNode
	move.l	(a7)+,a6

	move.l	d3,a0
	move.l	d0,F_file(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,F_task(a0)
	move.l	#0,F_filename(a0)	; for instance

	move.l	nodef(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d5
	rts
noFilNode
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d5
	moveq	#0,d0
	rts

;*****************************************************************************
;*
;* this closes a file
;*
;* In:
;* D1: ptr to the file
;*
NewCloseFile
	movem.l	a2,-(a7)
	tst.l	nodef(a6)
	beq.s	endCloseFile
	move.l	d1,d0
	beq.w	endCloseLib
	move.l	nodef(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endCloseFile
	
	move.l	d0,a0
	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	F_file(a1),d1
	move.l	a6,-(a7)
	move.l	rt_DosBase(a6),a6
	jsr	_LVOClose(a6)
	move.l	(a7)+,a6
	
	move.l	a2,a1
	jsr	NewFreeMem

endCloseFile
	movem.l	(a7)+,a2
	rts

;**************************************************************************
;*
;* Free all task remaining files allocation
;*
;* a1: task bloc (if 0 then free all)
;*
CloseAllFiles:
	tst.l	nodef(a6)
	beq.w	endCloseAllFile
	tst.l	a1
	beq.s	CloseAllTaskFile
	move.l	nodef(a6),a0

nextCloseFNode1	
	move.l	a0,d0
	move.l	(a0),a0
	
nextCloseFNode2
	tst.l	a0
	beq.s	endCloseAllFile
	cmpa.l	F_task(a0),a1
	bne.s	nextCloseFNode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2


	movem.l	d0/a0/a1/a6,-(a7)
	move.l	F_file(a0),d1
	move.l	rt_DosBase(a6),a6
	jsr	_LVOClose(a6)
	movem.l	(a7)+,d0/a0/a1/a6
	move.l	(a0),d1
	movem.l	d0/d1/a1,-(a7)
	move.l	a0,a1
	jsr	NewFreeMem
	movem.l	(a7)+,d0/d1/a1
	move.l	d1,a0
	bra.s	nextCloseFNode2


	rts

CloseAllTaskFile
	movem.l	a2-a4/a5-a6,-(a7)
	move.l	a6,a5
	move.l	nodef(a6),a2
	move.l	rt_DosBase(a6),a6
closeNextAllTF
	move.l	(a2),a4
	tst.l	a4
	beq.s	endCloseAllTF
	move.l	(a4),(a2)
	move.l	a4,a1
	move.l	F_file(a1),d1
	jsr	_LVOClose(a6)
	move.l	a4,a1
	exg	a5,a6
	jsr	NewFreeMem
	exg	a5,a6
	bra.s	closeNextAllTF

endCloseAllTF
	movem.l	(a7)+,a2-a4/a4-a6
endCloseAllFile
	rts
;**********************************************************************
;**********************************************************************
;**********************************************************************
;
;  ****     *    ****   **** ***** *****  ****
; *        * *   *   * *     *       *   *
; *       *   *  *   * *     **      *    ****
; *  **  ******* *   * *  ** *       *        *
;  ***   *     * ****   ***  *****   *   *****
;
;**********************************************************************
;*
;* This creates a gadget list
;* In:
;* a0: address of the first (null) pointer to gadgets 
;*
;* Out:
;* d0: ptr to the create glist
NewCreateContext
	movem.l	d2-d4,-(a7)
	move.l	a6,-(a7)
	tst.l	nodeg(a6)
	beq.s	noGadNode
	move.l	a0,d4
	move.l	#NG_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d2
	beq.s	noGadNode
	move.l	d4,a0
	move.l	rt_GadBase(a6),a6
	jsr	_LVOCreateContext(a6)
	tst.l	d0
	beq.s	noGadNode
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d0,G_gadget(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,G_task(a0)
	move.l	#RT_GLIST,G_type(a0)

	move.l	nodeg(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d4
	rts
noGadNode
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d4
	moveq	#0,d0
	rts

;**********************************************************************
;*
;* This creates a visual info
;* In:
;* a0: address of the first (null) pointer to gadgets 
;*
;* Out:
;* d0: ptr to the create glist
NewGetVisInfo
	movem.l	d2-d5,-(a7)
	move.l	a6,-(a7)
	tst.l	nodeg(a6)
	beq.s	noGadNode3
	move.l	a0,d4
	move.l	a1,d5
	move.l	#NG_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d2
	beq.s	noGadNode3
	move.l	d4,a0
	move.l	d5,a1
	move.l	rt_GadBase(a6),a6
	jsr	_LVOGetVisualInfoA(a6)
	tst.l	d0
	beq.s	noGadNode3
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d0,G_gadget(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,G_task(a0)
	move.l	#RT_VISINFO,G_type(a0)

	move.l	nodeg(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d5
	rts
noGadNode3
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d5
	moveq	#0,d0
	rts
;**********************************************************************
;*
;* This creates a gadget
;* In:
;* a0: address of the first (null) pointer to gadgets 
;*
;* Out:
;* d0: ptr to the create glist
NewCreateGadget
	movem.l	d2-d6,-(a7)
	move.l	a6,-(a7)
	tst.l	nodeg(a6)
	beq.s	noGadNode2
	move.l	d0,d3
	move.l	a0,d4
	move.l	a1,d5
	move.l	a2,d6
	move.l	#NG_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d2
	beq.s	noGadNode2
	move.l	d3,d0
	move.l	d4,a0
	move.l	d5,a1
	move.l	d6,a2
	move.l	rt_GadBase(a6),a6
	jsr	_LVOCreateGadgetA(a6)
	tst.l	d0
	beq.s	noGadNode2
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d0,G_gadget(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,G_task(a0)
	move.l	#RT_GADGET,G_type(a0)

	move.l	nodeg(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d6
	rts
noGadNode2
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d6
	moveq	#0,d0
	rts
	
;*****************************************************************************
;*
;* this delete all gadgets of a glist
;*
;* In:
;* A0: ptr to the glist
;*
NewFreeGadgets
	movem.l	a2,-(a7)
	tst.l	nodeg(a6)
	beq.s	endFreeGad
	move.l	a0,d0
	beq.s	endFreeGad
	move.l	nodeg(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endFreeGad

	move.l	d0,a0

	cmp.l	#RT_GLIST,G_type(a0)
	bne.s	endFreeGad

	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	G_gadget(a1),a0
	move.l	a6,-(a7)
	move.l	rt_GadBase(a6),a6
	jsr	_LVOFreeGadgets(a6)
	move.l	(a7)+,a6
	
	move.l	a2,a1
	jsr	NewFreeMem

endFreeGad
	movem.l	(a7)+,a2
	rts

;*****************************************************************************
;*
;* this delete a visual info
;*
;* In:
;* A0: ptr to the vi
;*
NewFreeVisualInfo
	movem.l	a2,-(a7)
	tst.l	nodeg(a6)
	beq.s	endFreeVI
	move.l	a0,d0
	beq.s	endFreeVI
	move.l	nodeg(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endFreeVI

	move.l	d0,a0

	cmp.l	#RT_VISINFO,G_type(a0)
	bne.s	endFreeVI

	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	G_gadget(a1),a0
	move.l	a6,-(a7)
	move.l	rt_GadBase(a6),a6
	jsr	_LVOFreeVisualInfo(a6)
	move.l	(a7)+,a6
	
	move.l	a2,a1
	jsr	NewFreeMem

endFreeVI
	movem.l	(a7)+,a2
	rts

;**************************************************************************
;*
;* Free all task remaining gadtools allocation
;*
;* a1: task bloc (if 0 then free all)
;*
FreeAllGads:
	tst.l	nodeg(a6)
	beq.w	endFreeAllGads
	tst.l	a1
	beq.w	FreeAllTaskGads
	
	move.l	nodeg(a6),a0
nextCloseGNode1
	move.l	a0,d0
	move.l	(a0),a0
nextCloseGNode2
	tst.l	a0
	beq.w	closeAllVisInfo
	cmpa.l	G_task(a0),a1
	bne.s	nextCloseGNode1
	cmp.l	#RT_GLIST,G_type(a0)
	bne.s	nextCloseGNode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2

	movem.l	d0/a0/a1/a6,-(a7)
	move.l	G_gadget(a0),a0
	move.l	rt_GadBase(a6),a6
	jsr	_LVOFreeGadgets(a6)
	movem.l	(a7)+,d0/a0/a1/a6
	move.l	(a0),d1
	movem.l	d0/d1/a1,-(a7)
	move.l	a0,a1
	jsr	NewFreeMem
	movem.l	(a7)+,d0/d1/a1
	move.l	d1,a0
	bra.s	nextCloseGNode2

	; free the visinfo
closeAllVisInfo

	move.l	nodeg(a6),a0
nextCloseGVISNode1
	move.l	a0,d0
	move.l	(a0),a0
nextCloseGVISNode2
	tst.l	a0
	beq.w	closeAllGads
	cmpa.l	G_task(a0),a1
	bne.s	nextCloseGVISNode1
	cmp.l	#RT_VISINFO,G_type(a0)
	bne.s	nextCloseGVISNode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2


	movem.l	d0/a0/a1/a6,-(a7)
	move.l	G_gadget(a0),a0
	move.l	rt_GadBase(a6),a6
	jsr	_LVOFreeVisualInfo(a6)
	movem.l	(a7)+,d0/a0/a1/a6
	move.l	(a0),d1
	movem.l	d0/d1/a1,-(a7)
	move.l	a0,a1
	jsr	NewFreeMem
	movem.l	(a7)+,d0/d1/a1
	move.l	d1,a0
	bra.s	nextCloseGVISNode2

	; free the gadgets
closeAllGads

	move.l	nodeg(a6),a0
nextCloseGadNode1
	move.l	a0,d0
	move.l	(a0),a0
nextCloseGadNode2
	tst.l	a0
	beq.w	endFreeAllGads
	cmpa.l	G_task(a0),a1
	bne.s	nextCloseGadNode1
	cmp.l	#RT_GADGET,G_type(a0)
	bne.s	nextCloseGadNode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2

	move.l	(a0),d1
	movem.l	d0/d1/a1,-(a7)
	move.l	a0,a1
	jsr	NewFreeMem
	movem.l	(a7)+,d0/d1/a1
	move.l	d1,a0
	bra.s	nextCloseGadNode2
	rts

FreeAllTaskGads
	movem.l	a2-a4/a5-a6,-(a7)
	move.l	a6,a5
	move.l	nodeg(a6),a2
	move.l	rt_GadBase(a6),a6
closeNextAllTG
	move.l	(a2),a4
	tst.l	a4
	beq.s	endCloseAllTG
	move.l	(a4),(a2)
	move.l	a4,a1
	cmp.l	#RT_GLIST,G_type(a1)
	bne.s	checkVisInfo
	move.l	G_gadget(a1),a0
	jsr	_LVOFreeGadgets(a6)
	move.l	a4,a1
checkVisInfo
	cmp.l	#RT_VISINFO, G_type(a1)
	bne.s	checkGadget
	move.l	G_gadget(a1),a0
	jsr	_LVOFreeVisualInfo(a6)
	move.l	a4,a1
checkGadget
	exg	a5,a6
	jsr	NewFreeMem
	exg	a5,a6
	bra.s	closeNextAllTG

endCloseAllTG
	movem.l	(a7)+,a2-a4/a4-a6
endFreeAllGads
	rts
;**********************************************************************
;**********************************************************************
;**********************************************************************
;
; ****** *   * *****  ****
; *       * *  *     *
; **       *   **    *
; *       * *  *     *
; ****** *   * *****  ****
;
;**********************************************************************
;*
;* This create a msgport
;* In:
;* NULL
;*
;* Out:
;* d0: ptr to the created port
NewCreateMsgPort
	movem.l	d2,-(a7)
	move.l	a6,-(a7)
	tst.l	nodee(a6)
	beq.s	noMsgNode
	move.l	#NE_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d2
	beq.s	noMsgNode
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCreateMsgPort(a6)
	tst.l	d0
	beq.s	noMsgNode
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d0,E_data(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,E_task(a0)
	move.l	#RT_MSGPORT,E_type(a0)
	
	move.l	nodee(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2
	rts
noMsgNode
	move.l	(a7)+,a6
	movem.l	(a7)+,d2
	moveq	#0,d0
	rts

;*****************************************************************************
;*
;* this delete a msgport
;*
;* In:
;* A0: ptr to the msgport
;*
NewDeleteMsgPort
	movem.l	a2,-(a7)
	tst.l	nodee(a6)
	beq.s	endDelMsg
	move.l	a0,d0
	beq.s	endDelMsg
	move.l	nodee(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endDelMsg
	
	move.l	d0,a0
	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	E_data(a1),a0
	move.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVODeleteMsgPort(a6)
	move.l	(a7)+,a6
	
	move.l	a2,a1
	jsr	NewFreeMem

endDelMsg
	movem.l	(a7)+,a2
	rts

;**********************************************************************
;*
;* This create an iorequest
;* In:
;* a0: msgport
;* d0: size
;*
;* Out:
;* d0: ptr to the created iorequest
NewCreateIORequest
	movem.l	d2-d4,-(a7)
	move.l	a6,-(a7)
	tst.l	nodee(a6)
	beq.s	noIORNode
	move.l	d0,d3
	move.l	a0,d4
	move.l	#NE_SIZE,d0
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	NewAllocMem
	move.l	d0,d2
	beq.s	noIORNode
	move.l	d3,d0
	move.l	d4,a0
	move.l	rt_SysBase(a6),a6
	jsr	_LVOCreateIORequest(a6)
	tst.l	d0
	beq.s	noIORNode
	move.l	(a7)+,a6

	move.l	d2,a0
	move.l	d0,E_data(a0)
	move.l	d0,d2
	bsr	getTask
	move.l	d0,E_task(a0)
	move.l	#RT_REQUEST,E_type(a0)

	move.l	nodee(a6),a1
	move.l	(a1),(a0)
	move.l	a0,(a1)

	move.l	d2,d0

	movem.l	(a7)+,d2-d4
	rts
noIORNode
	move.l	(a7)+,a6
	movem.l	(a7)+,d2-d4
	moveq	#0,d0
	rts

;*****************************************************************************
;*
;* this delete an iorequest
;*
;* In:
;* A0: ptr to the msgport
;*
NewDeleteIORequest
	movem.l	a2,-(a7)
	tst.l	nodee(a6)
	beq.s	endDelIOR
	move.l	a0,d0
	beq.s	endDelIOR
	move.l	nodee(a6),a0	
	bsr.w	findNode
	
	tst.l	d0
	beq.s	endDelIOR
	
	move.l	d0,a0
	move.l	(a0),a1
	move.l	d0,a2
	move.l	(a2),a2
	move.l	(a1),(a0)
	
	move.l	E_data(a1),a0
	move.l	a6,-(a7)
	move.l	rt_SysBase(a6),a6
	jsr	_LVODeleteIORequest(a6)
	move.l	(a7)+,a6
	
	move.l	a2,a1
	jsr	NewFreeMem

endDelIOR
	movem.l	(a7)+,a2
	rts

;**************************************************************************
;*
;* Free all task remaining exec allocation
;*
;* a1: task bloc (if 0 then free all)
;*
DeleteAllExec:
	tst.l	nodee(a6)
	beq.w	endFreeAllExec
	tst.l	a1
	beq.s	CloseAllTaskExec
	move.l	nodee(a6),a0
	
nextCloseENode1
	move.l	a0,d0
	move.l	(a0),a0
nextCloseENode2
	tst.l	a0
	beq.w	endFreeAllExec
	cmpa.l	E_task(a0),a1
	bne.s	nextCloseENode1
	;* retire this node from the list
	movem.l	a2,-(a7)
	move.l	d0,a2
	move.l	(a0),(a2)
	movem.l	(a7)+,a2


	movem.l	d0/a0/a1/a6,-(a7)
	cmp.l	#RT_MSGPORT,E_type(a0)
	bne.s	checkIOR
	move.l	E_data(a0),a0
	move.l	rt_SysBase(a6),a6
	jsr	_LVODeleteMsgPort(a6)
	bra.s	endCheckExec
checkIOR
	cmp.l	#RT_REQUEST,E_type(a0)
	bne.s	endCheckExec
	move.l	E_data(a0),a0
	move.l	rt_SysBase(a6),a6
	jsr	_LVODeleteIORequest(a6)
endCheckExec
	movem.l	(a7)+,d0/a0/a1/a6
	move.l	(a0),d1
	movem.l	d0/d1/a1,-(a7)
	move.l	a0,a1
	jsr	NewFreeMem
	movem.l	(a7)+,d0/d1/a1
	move.l	d1,a0
	bra.s	nextCloseENode2
	rts

CloseAllTaskExec
	movem.l	a2-a4/a5-a6,-(a7)
	move.l	a6,a5
	move.l	nodee(a6),a2
	move.l	rt_SysBase(a6),a6
closeNextAllTE
	move.l	(a2),a4
	tst.l	a4
	beq.s	endCloseAllTE
	move.l	(a4),(a2)
	move.l	a4,a1
	cmp.l	#RT_MSGPORT,E_type(a1)
	bne.s	checkAllIOR
	move.l	E_data(a1),a0
	jsr	_LVODeleteMsgPort(a6)
	bra.s	endCheckAllExec
checkAllIOR
	cmp.l	#RT_REQUEST,E_type(a1)
	bne.s	endCheckAllExec
	move.l	E_data(a1),a0
	jsr	_LVODeleteIORequest(a6)
endCheckAllExec
	move.l	a4,a1
	exg	a5,a6
	jsr	NewFreeMem
	exg	a5,a6
	bra.w	closeNextAllTL

endCloseAllTE
	movem.l	(a7)+,a2-a4/a4-a6
endFreeAllExec
	rts
