*
*	Name:				libhdr.a
*
*	Description:	Universal Amiga shared library controller
*
*	Copyright:		1991-1993 by David Jones.
*
*	Distribution:
*    This source file is released under the terms and conditions of the
*	GNU General Public Library License, which should have been included
*	ith this source distribution.  If you do not have a copy of this
*	license, then write to both of the following:
*
*				The Free Software Foundation		David Jones
*				675 Mass Ave							6730 Tooney Drive
*				Cambridge, MA							Orleans, Ontario
*				02139										K1C 6R4
*				USA										Canada
*
*	Usenet:	gnu@prep.ai.mit.edu					dej@qpoint.ocunix.on.ca
*	Fidonet:												1:163/109.8
*
*
*	There is NO WARRANTY associated with this software; refer to "gpll.doc"
*	for details.
*
*		$Log: $
*
*

		include 'exec/types.i'
		include 'exec/libraries.i'
		include 'exec/resident.i'
		include 'exec/alerts.i'
		include 'exec/semaphores.i'
		include 'dejmac.i'
		include 'exec.offsets'
		include 'xferq_rev.i'
		
		STRUCTURE	myLibBase,0
		  STRUCT	ml_LibNode,LIB_SIZE
		  LONG		ml_SegList
		  BYTE		ml_Flags
		  BYTE		ml_Pad
		  LONG		ml_A4
		  LONG		ml_Data
		  STRUCT		ml_Lock,SS_SIZE
		LABEL		MYLIB_SIZEOF

		XREF	_LibName
		XREF	_LibID
		XREF	_FuncTable
		XREF	_LibInit
		XREF	_LibOpen
		XREF	_LibClose
		XREF	_LibExpunge
		
		XREF	_LinkerDB
		XREF	_LibBase
		XREF	_DOSBase
		XREF	_SysBase
		
		XDEF	_aLibOpen
		XDEF	_aLibClose
		XDEF	_aLibExpunge
		XDEF	_aLibExtFunc
		XDEF	_RomTag

*
*	You will always get some doofus who will try to execute us.  Let's
*	execute him instead.
*

DoofusKick:
		moveq		#-1,D0
		rts

*
*  This is the RomTag structure.
*

_RomTag:
		dc.w	RTC_MATCHWORD
		dc.l	_RomTag
		dc.l	EndCode
		dc.b	RTF_AUTOINIT
		dc.b	VERSION	; version
		dc.b	NT_LIBRARY
		dc.b	0				; pri
		dc.l	_LibName
		dc.l	_LibID
		dc.l	InitTab

		ds.w	0		; force word alignment
		
InitTab:
		dc.l	MYLIB_SIZEOF
		dc.l	_FuncTable
		dc.l	0		; we do our own init
		dc.l	_aLibInit

_aLibInit:
		move.l	A6,_SysBase
		move.l	D0,_LibBase	; set global variables
		move.l	D0,A1		; initialize base
		move.l	A0,ml_SegList(A1)
		move.l	#_LinkerDB,ml_A4(A1)
		move.b	#NT_LIBRARY,LN_TYPE(A1)
		move.l	#_LibName,LN_NAME(A1)
		move.b	#LIBF_SUMUSED!LIBF_CHANGED,LIB_FLAGS(A1)
		move.w	#VERSION,LIB_VERSION(A1)
		move.w	#REVISION,LIB_REVISION(A1)
		move.l	#_LibID,LIB_IDSTRING(A1)
		moveq		#0,D0
		move.w	D0,ml_Flags(A1)
		lea		ml_Lock(A1),A0
		CALL		InitSemaphore
		lea		DosName(PC),A1	; open DOS
		moveq		#0,D0
		CALL		OpenLibrary
		move.l	D0,_DOSBase
		bne.s		1$
		ALERT		AG_OpenLib!AO_DOSLib
1$		jsr		_LibInit	; call user init
		rts


_aLibOpen:
		lea		ml_Lock(A6),A0
		LINKSYS	ObtainSemaphore,_SysBase
		addq.w	#1,LIB_OPENCNT(A6)
		bclr		#LIBB_DELEXP,ml_Flags(A6)
		jsr		_LibOpen	; call user init
		move.l	D0,-(SP)
		lea		ml_Lock(A6),A0
		LINKSYS	ReleaseSemaphore,_SysBase
		move.l	(SP)+,D0
		rts

_aLibClose:
		lea		ml_Lock(A6),A0
		LINKSYS	ObtainSemaphore,_SysBase
		jsr		_LibClose
		moveq		#0,D0
		subq.w	#1,LIB_OPENCNT(A6)
		bne.s		1$
		btst		#LIBB_DELEXP,ml_Flags(A6)
		beq.s		1$
		bsr		_aLibExpunge
1$		move.l	D0,-(SP)
		lea		ml_Lock(A6),A0
		LINKSYS	ReleaseSemaphore,_SysBase
		move.l	(SP)+,D0
		rts

_aLibExpunge:
		move.l	A6,-(SP)
		move.l	_LibBase,A1
		lea		ml_Lock(A1),A0
		LINKSYS	AttemptSemaphore,_SysBase
		tst.l		D0
		beq.s		2$		; seglist is zero, just like D0
		move.l	_LibBase,A1
		tst.w		LIB_OPENCNT(A1)
		beq.s		1$
		bset		#LIBB_DELEXP,ml_Flags(A1)
		moveq		#0,D0
		bra.s		2$
1$
		jsr		_LibExpunge
		tst.l		D0
		bne.s		3$
		
		move.l	_LibBase,A1
		lea		ml_Lock(A1),A0
		CALLSYS	ReleaseSemaphore,_SysBase
		moveq		#0,D0
		beq.s		2$
3$
		move.l	_LibBase,A1
		CALLSYS	Remove,_SysBase
		move.l	_DOSBase,A1
		CALL		CloseLibrary
		moveq		#0,D0
		move.l	_LibBase,A1
		move.l	A1,A0
		move.l	ml_SegList(A0),-(SP)
		move.w	LIB_NEGSIZE(A0),D0
		sub.l		D0,A1
		add.w		LIB_POSSIZE(A0),D0
		CALL		FreeMem
		move.l	(SP)+,D0
2$
		move.l	(SP)+,A6
		rts


_aLibExtFunc:
		moveq		#0,D0
		rts

DosName:
		dc.b	'dos.library',0
		ds.w	0
EndCode:

		END
