	IFND LIBRARIES_PATCHPRIVATE_I
LIBRARIES_PATCHPRIVATE_I SET 1
**
**	Filename:	libraries/patch.i
**	Release:	6.0
**	Date:		13.12.97
**
**	(C) Copyright 1993-97 Stefan Fuchs
**	All rights reserved
**
**      definition of patch.library PRIVATE structures


	IFND EXEC_TYPES_I
	include "exec/types.i"
	ENDC

	IFND EXEC_LISTS_I
	include "exec/lists.i"
	ENDC

	IFND EXEC_LIBRARIES_I
	include "exec/libraries.i"
	ENDC

	IFND EXEC_SEMAPHORES_I
	include "exec/semaphores.i"
	ENDC

	IFND UTILITY_TAGITEM_I
	INCLUDE "utility/tagitem.i"
	ENDC
;-------------------------------------------------------------------------
STACKSLOTS = 5	; (V6) Maximum number of stacks, which can be preallocated for stackextension feature


 STRUCTURE MasterPatch,0	;This structure is PRIVATE
	APTR	MPS_SUCC	;Pointer to next (successor)
	APTR	MPS_PRED	;Pointer to previous (predecessor)
	UBYTE	MPS_TYPE	;MUST be MASTERTYPE
	BYTE	MPS_PRI		;Priority, for sorting 			(not used)
	APTR	MPS_NAME	;pointer to libraryname
	UWORD	MPS_Flags	;see below
	STRUCT	MPS_PatchHeader,MLH_SIZE	;Listheader for Patch structures
	APTR	MPS_PatchedLibraryBase		;Pointer to patched Library
	WORD	MPS_PatchedLVO			;Library Vektor Offset in patched Library
	STRUCT	MPS_OLDEntry,6			;original entry point
	APTR	MPS_PatchIORequest		;Pointer to IORequest for CloseDevice() (if available)
	APTR	MPS_SetFunctionResult		;Pointer to result of SetFunction
						;(only used when the UseSetFunction Bit is set)
						;This need not equal the contents of MPS_OLDEntry, if a SetFunction Manager is installed

	STRUCT  MPS_StackSlots,(4*STACKSLOTS)	;(V6) Array of pointers of preallocated stacks


 LABEL MPS_SIZEOF

;Master Patch Struct Flags
 BITDEF MPS,CloseLib,0		;Patch.library opened the library
 BITDEF MPS,CloseDev,1		;Patch.library opened the device
 BITDEF MPS,IncLibUsage,2	;Patch.library did increase library usage counter (was MPS_PatchSV)

 BITDEF MPS,Attached,3		;Masterpatch is attached to library
 BITDEF MPS,UseSetFunction,4	;Use SetFunction for installing and removing patches

;---------------------------

 STRUCTURE Patch,0		;This structure is PRIVATE
	APTR	PS_SUCC		;Pointer to next (successor)
	APTR	PS_PRED		;Pointer to previous (predecessor)
	UBYTE	PS_TYPE		;may be USERTYPE, STARTTYPE, ENDTYPE, ORIGTYPE, SYSTEMTYPE
	BYTE	PS_PRI		;Priority, for sorting
	APTR	PS_NAME		;ID string, null terminated
	UWORD	PS_Flags	;see below
	APTR	PS_PatchCode	;Pointer to PatchCode (Code begins at Offset + 12)
	LONG	PS_UserPatchCodeSize		;Length of NewCode in bytes (if this field is >0 the memory starting at the UserPatch must be freed)
	STRUCT	PS_RemoveHookList,LH_SIZE	;Header of a double linked list containing
						;hooks to be called, when a patch is finally removed
	STRUCT  PS_ProjectNode,MLN_SIZE		;ListNode identifing the Project(if any), to which the
						;patch belongs
	ULONG	PS_UserData		;Application specific data
	ULONG	PS_StackSize		;(V6) Minimum Stacksize for this patch
	UBYTE	PS_PatchCodeType	;(V6) Type of PatchCode attached via PS_PatchCode (see below)
	UBYTE	PS_Pad			;reserved

	APTR	PS_JmpNext		;(V6) Pointer to instruction to change, when linkage changes or NULL
	APTR	PS_JmpOrig		;(V6) Pointer to instruction to change, when linkage changes or NULL
	APTR	PS_Jsr			;(V6) Pointer to instruction to change, when linkage changes or NULL
	APTR	PS_SystemEntry		;(V6) Address to reenter patchcode or NULL

	APTR	PS_TestUsage1		;(V6) Pointer to instruction, which is not covered by a usage counter or NULL
	APTR	PS_TestUsage2		;(V6) Pointer to instruction, which is not covered by a usage counter or NULL
	APTR	PS_TestUsage3		;(V6) Pointer to instruction, which is not covered by a usage counter or NULL

 LABEL PS_SIZEOF

;Patch Struct PRIVATE Flags
 BITDEF PS,PatchSV,0		;Object was installed with PatchSupervisor active
 BITDEF PS,OriginalType,2	;(V6) Patch was installed with PATT_Original
 BITDEF PS,InternalCall,3	;(V6) Calling SetPatch() from internal function
				; (SetPatchA() will always clear this flag)

;PS_PatchCodeType:
PCODE_GENERIC	=  0	;
PCODE_START	=  6	;For PS_TYPE_START
PCODE_ORIG	=  7	;For PS_TYPE_ORIG
PCODE_END	=  8	;For PS_TYPE_END
PCODE_USER0	= 10	;For PS_TYPE_USER   ->Replace + No UseXResult
PCODE_USER1	= 11	;For PS_TYPE_USER   ->Replace + UseXResult
PCODE_USER2	= 12	;For PS_TYPE_USER   ->Pri <> 0 + NoOrig + No UseXResult
PCODE_USER3	= 13	;For PS_TYPE_USER   ->Pri <> 0 + NoOrig + UseXResult
PCODE_USER4	= 14	;For PS_TYPE_USER   ->Pri >  0 + Orig + No UseXResult
PCODE_USER5	= 15	;For PS_TYPE_USER   ->Pri >  0 + Orig + UseXResult

PCODE_SYSTEM 	= PCODE_USER4

;----------------------------

 STRUCTURE PatchProject,0	;This structure is PRIVATE
	STRUCT  PPR_Node,LN_SIZE
	UWORD	PPR_Flags		;none defined yet
	STRUCT	PPR_PatchListHeader,14	;Header of a double linked list containing
					;patches belonging to the Project
	ULONG	PPR_UserData		;Application specific data (not implemented)

 LABEL PPR_SIZEOF
;LN_TYPE: PS_TYPE_PROJECT
;LN_NAME: Name of Project

;---------------------------

 STRUCTURE TLHeader,LH_SIZE	;This structure is PRIVATE
	ULONG	TL_Result1	;Result1: 0 if Type: TL_TYPE_INCLUDE otherwise 1
	ULONG	TL_Result2	;Result2: 1 if Type: TL_TYPE_INCLUDE otherwise 0

 LABEL TL_SIZEOF


;---------------------------

;ErrorCodes:
PATERR_Ok		equ	0	;Everything Ok
PATERR_PatchInUse	equ	1	;Patch Usecount <> 0
PATERR_InvalidHandle	equ	2	;Pointer to patch is not valid
PATERR_OutOfMem		equ	3	;Out of memory
PATERR_OpenLib		equ	4	;Failed to open requested library
PATERR_FuncNotStd	equ	5	;Function to patch is not in the standard format
PATERR_PatchInstalled	equ	6	;Can't remove patch because another program has installed a non-patch.library patch later
PATERR_InvalidTags	equ	7	;There was an error in the combination of the specified Tags
PATERR_OpenDev		equ	8	;Failed to open requested device
PATERR_NoTaskList	equ	9	;PATT_AddTask..., PATT_RemTask... PATT_TaskListType
					;was specified for a patch, which had no PATT_CreateTaskList set
PATERR_TaskListExists	equ    10	;PATT_CreateTaskList was specified, but there already exists a TaskList
PATERR_InvalidTaskList	equ    11	;PATT_CreateTaskList was specified with a wrong parameter
PATERR_PatchUnnamed	equ    12	;PATT_PatchName with GetPatch was specified, for a patch without an IDString
PATERR_NoSemaphore	equ    13	;Could not allocate semaphore

PATERR_KickTooOld	equ    14	;Kickstart version is too old for this function
PATERR_InvalidPattern	equ    15	;Pattern passed via PATT_AddTaskPattern is not valid
PATERR_Restricted	equ    16	;Certain tags/features may not be used when patching certain functions

;-------------------------------------------------------------------------
 STRUCTURE PatchNotifyMessage,0
    STRUCT pnm_ExecMessage,MN_SIZE
    ULONG  pnm_Class
    UWORD  pnm_Code
    APTR   pnm_Object
    LABEL  PatchNotifyMessage_SIZEOF

;-------------------------------------------------------------------------
PATCHNOTIFY_CLASS	equ	$50000000
;-------------------------------------------------------------------------
;Notification codes (pnm_Code) (ignore messages with other codes):
PATCOD_PatchInstalled	equ 	1	;new patch has been added
PATCOD_PatchRemoved	equ	2	;a patch has been removed
PATCOD_PatchChanged	equ	3	;attributes of a patch have been changed
;-------------------------------------------------------------------------
 STRUCTURE PatchXResult,0	;Use PatchAlloc(PS_TYPE_XRESULT) to allocate this
    STRUCT pxr_Node,LN_SIZE	;Reserved
    ULONG  pxr_RegPattern	;Which of the following register fields are used
    ULONG  pxr_RegD0
    ULONG  pxr_RegD1
    ULONG  pxr_RegD2
    ULONG  pxr_RegD3
    ULONG  pxr_RegD4
    ULONG  pxr_RegD5
    ULONG  pxr_RegD6
    ULONG  pxr_RegD7
    ULONG  pxr_RegA0
    ULONG  pxr_RegA1
    ULONG  pxr_RegA2
    ULONG  pxr_RegA3
    ULONG  pxr_RegA4
    ULONG  pxr_RegA5
    ULONG  pxr_RegA6
    UWORD  pxr_FlowControl	;What patches follow this one (see below)
    LABEL  PatchXResult_SIZEOF	;Don't depend on this
;-------------------------------------------------------------------------
;Valid values for per_FlowControl:
PATFC_Normal		equ	0	;No change in the sequnce of patchcodes
PATFC_FallBack		equ	1	;Use original function (only valid, if PATT_Priority is NULL)

;-------------------------------------------------------------------------
;Types:
PS_TYPE_UNKNOWN		= 0	;do not use
PS_TYPE_MASTER		= 1	;Structure is MPS Struct
PS_TYPE_USER		= 2	;this struct points to a routine that calls the user function
PS_TYPE_SYSTEM		= 3	;this struct points to a routine that was installed by the SetFunction patch
PS_TYPE_DELUSER		= 4	;this struct is PS_TYPE_USER, but should be removed
PS_TYPE_DELSYSTEM	= 5	;this struct is PS_TYPE_SYSTEM, but should be removed
PS_TYPE_START		= 6	;this struct points to a JMP
PS_TYPE_ORIG		= 7	;this struct points to a routine that calls the old routine
PS_TYPE_END		= 8	;this struct points to a RTS
PS_TYPE_PROJECT		= 20	;this struct is a Header structure to a list of PS_TYPE_USER structures belonging to one project
PS_TYPE_XRESULT		= 30	;this struct is an extended result structure


TLI_TYPE_TASKNAME	= 3	;LN_NAME points to TaskName
TLI_TYPE_TASKID		= 4	;LN_NAME points to Task structure
TLI_TYPE_TASKPATTERN    = 5     ;LN_NAME points to Token string for MatchPattern() (V5)

; ----------------------------------------------------------------------------
; This structure is at the high adresses (the bottom) of an extended stack
; .... stack ....
 STRUCTURE PatchStack,0
	ULONG PST_Reserved2		; $CCDDEEFF
	ULONG PST_Reserved1		; $8899AABB
	STRUCT PST_StackSwap,StackSwapStruct_SIZEOF	;structure for StackSwap
	ULONG PST_Usage			; 1: this stack is in use  0: stack is free
	APTR  PST_OldStack		; Pointer to oldstack
	ULONG PST_Stacksize		; Size of this stack
	APTR  PST_Magic2		; pointer to PST_Magic1
	ULONG PST_Magic1		; magic PatchStackID 'PATC'
	LABEL PatchStack_SIZEOF


	ENUM TAG_USER+$5000+50		;Beginning of GetPatch tags (currently PRIVATE)
	EITEM PATT_NewCode		;G: BOOL Return pointer to installed patch (newcode)
					;	default: FALSE


	ENUM TAG_USER+$5000+60		;Beginning of PRIVATE tags
	EITEM PATT_SYSTEM		;I: True: Install patch in system mode
					;	FOR PatchSetFunc *ONLY*
	 				;F: APTR Return pointer to patch structure
					;	of the given code pointer
					;G: True: return Setfunction() result
	EITEM PATT_LVO			;F: UWORD Return pointer to master patch structure
					;	of the given LibraryBase and LVO
					;	if you specify this tag you must also specify
					;	the public tag PATT_LibraryBase or PATT_LibraryName
	EITEM PATT_Detach		;S: BOOL the library for this patch has been closed
					;	Parameter (MasterPatch structure instead of Patch struct)
					;	FOR PatchSupervisor *ONLY*
	EITEM PATT_Attach		;S: struct Library *
					;	Reattach a MasterStruct to a Library
					;	Parameter (MasterPatch structure instead of Patch struct)
					;	FOR PatchSupervisor *ONLY*

;		PATT_LibraryBase	;F: APTR Return pointer to master patch structure
					;	of the given LibraryBase and LVO
					;	if you specify this tag you must also specify
					;	the private tag PATT_LVO
;		PATT_LibraryName	;F: APTR Return pointer to master patch structure
					;	of the given Libraryname and LVO
					;	if you specify this tag you must also specify
					;	the private tag PATT_LVO

	ENUM TAG_USER+$5000+24		;Beginning of OBSOLETE tags
	EITEM PATT_SaveRegisters	;I:  ULONG (V4) Register Mask


PATCHNAME	MACRO
	dc.b "patch.library",0
	ENDM

;---------------------------------------------------------------
;--- FALLBACK - Call this macro instead of a 'rts' instruction
;--- in your patchcode, if you want to return to the original
;--- library code
;---------------------------------------------------------------
FALLBACK	MACRO
	move.l (sp),-(sp)
	clr.l 4(sp)
	rts

	ENDM


	ENDC	;LIBRARIES_PATCHPRIVATE_I
