7 Versionstring: $VER: SDI_hook.h 1.24 (03.03.2011)
8 Author: SDI & Jens Langner
10 Project page: http://www.sf.net/projects/sditools/
11 Description: defines to hide compiler specific hook stuff
13 1.0 21.06.02 : based on the work made for freeciv and YAM with
14 additional texts partly taken from YAM_hook.h changes made
15 by Jens Langner, largely reworked the mechanism
16 1.1 07.10.02 : added HOOKPROTONP and HOOKPROTONONP requested by Jens
17 1.2 18.10.02 : reverted to old MorphOS-method for GCC
18 1.3 08.02.04 : modified to get it compatible to AmigaOS4
19 1.4 17.02.04 : modified to get compatible to latest SDI_compiler.h changes
20 1.5 02.03.04 : added UNUSED define to OS4 hook specification so that the
21 compiler can ignore some warnings.
22 1.6 02.03.04 : added (APTR) casts to MorphOS prototype definition to
23 reduce compiler warnings.
24 1.7 04.07.04 : removed static from all DISPATCHERPROTO definitions as there
25 may be dispatchers that are of course non static.
26 1.8 07.04.05 : added MakeHookWithData (Sebastian Bauer)
27 1.9 08.04.05 : changed MorphOS hooks to use HookEntry (Ilkka Lehtoranta)
28 1.10 16.05.05 : simplified and fixed for vbcc/MorphOS (Frank Wille)
29 1.11 17.05.05 : changed cast in DISPATCHERPROTO from (void(*)()) to APTR
30 cause SDI version of the EmulLibEntry uses APTR for easy
32 Added #ifndef SDI_TRAP_LIB to avoid double defines when
33 combining with SDI_interrupt.h or SDI_misc.h (Guido
35 1.12 18.05.05 : DISPATCHERPROTO wasn't working, because of the missing REG_Ax
36 definitions. Added include <emul/emulregs.h> (Guido Mersmann)
37 1.13 11.12.05 : fixed a minor typo in the PPC HOOKPROTONP macro.
39 1.14 20.04.06 : unified static of MorphOs with non-MorphOS vesion
40 1.15 30.04.06 : modified to get it compatible to AROS. (Guido Mersmann)
41 1.16 06.10.06 : added new DISPATCHER() macro and separated it from the
42 DISPATCHERPROTO() definition. Now the DISPATCHERPROTO() should
43 only be used to get the correct prototype and the plain
44 DISPATCHER() for defining the dispatcher itself.
45 1.17 14.07.08 : added "_" to all UNUSED variable specifications to make sure
46 a user does not use those definition on accident.
47 1.18 20.03.09 : modified macros to be somewhat more compatible for an AROS
49 1.19 25.03.09 : fixed the DISPATCHERPROTO() macros for x86_64 AROS.
50 1.20 26.03.09 : fixed m68k define checks.
51 1.21 19.05.09 : added SDISPATCHER() to generate a static dispatcher.
52 1.22 24.06.10 : fixed AROS macros (Matthias Rustler).
53 1.23 12.08.10 : added missing proto/alib.h include for AROS
54 1.24 03.03.11 : fixed AROS macros for m68k (Jason McMullan)
59 ** This is PD (Public Domain). This means you can do with it whatever you want
60 ** without any restrictions. I only ask you to tell me improvements, so I may
61 ** fix the main line of this files as well.
63 ** To keep confusion level low: When changing this file, please note it in
64 ** above history list and indicate that the change was not made by myself
65 ** (e.g. add your name or nick name).
67 ** Find the latest version of this file at:
68 ** http://cvs.sourceforge.net/viewcvs.py/sditools/sditools/headers/
70 ** Jens Langner <Jens.Langner@light-speed.de> and
71 ** Dirk Stoecker <soft@dstoecker.de>
74 #include "SDI_compiler.h"
77 ** Hook macros to handle the creation of Hooks/Dispatchers for different
78 ** Operating System versions.
79 ** Currently AmigaOS, AROS, and MorphOS are supported.
81 ** For more information about hooks see include file <utility/hooks.h> or
82 ** the relevant descriptions in utility.library autodocs.
86 ** Creates a hook with the name "TestHook" that calls a corresponding
87 ** function "TestFunc" that will be called with a pointer "text"
88 ** (in register A1) and returns a long.
90 ** HOOKPROTONHNO(TestFunc, LONG, STRPTR text)
95 ** MakeHook(TestHook, TestFunc);
97 ** Every function that is created with HOOKPROTO* must have a MakeHook() or
98 ** MakeStaticHook() to create the corresponding hook. Best is to call this
99 ** directly after the hook function. This is required by the GCC macros.
101 ** The naming convention for the Hook Prototype macros is as followed:
103 ** HOOKPROTO[NH][NO][NP]
105 ** NoHook | NoParameter
108 ** So a plain HOOKPROTO() creates you a Hook function that requires
109 ** 4 parameters, the "name" of the hookfunction, the "obj" in REG_A2,
110 ** the "param" in REG_A1 and a "hook" in REG_A0. Usually you will always
111 ** use NH, as the hook structure itself is nearly never required.
113 ** The DISPATCHERPROTO macro is for MUI dispatcher functions. It gets the
114 ** functionname as argument. To supply this function for use by MUI, use
115 ** The ENTRY macro, which also gets the function name as argument.
118 #if !defined(__AROS__) && (defined(_M68000) || defined(__M68000) || defined(__mc68000))
119 #define HOOKPROTO(name, ret, obj, param) static SAVEDS ASM ret \
120 name(REG(a0, struct Hook *hook), REG(a2, obj), REG(a1, param))
121 #define HOOKPROTONO(name, ret, param) static SAVEDS ASM ret \
122 name(REG(a0, struct Hook *hook), REG(a1, param))
123 #define HOOKPROTONP(name, ret, obj) static SAVEDS ASM ret \
124 name(REG(a0, struct Hook *hook), REG(a2, obj))
125 #define HOOKPROTONONP(name, ret) static SAVEDS ASM ret \
126 name(REG(a0, struct Hook *hook))
127 #define HOOKPROTONH(name, ret, obj, param) static SAVEDS ASM ret \
128 name(REG(a2, obj), REG(a1, param))
129 #define HOOKPROTONHNO(name, ret, param) static SAVEDS ASM ret \
131 #define HOOKPROTONHNP(name, ret, obj) static SAVEDS ASM ret \
133 #define HOOKPROTONHNONP(name, ret) static SAVEDS ret name(void)
135 /* NOTE: This is fine for AROS, since HookEntry will handle stack params
137 #define HOOKPROTO(name, ret, obj, param) static SAVEDS ret \
138 name(struct Hook *hook, obj, param)
139 #define HOOKPROTONO(name, ret, param) static SAVEDS ret \
140 name(struct Hook *hook, UNUSED APTR _obj, param)
141 #define HOOKPROTONP(name, ret, obj) static SAVEDS ret \
142 name(struct Hook *hook, obj, UNUSED APTR _param)
143 #define HOOKPROTONONP(name, ret) static SAVEDS ret \
144 name(struct Hook *hook, UNUSED APTR _obj, UNUSED APTR _param)
145 #define HOOKPROTONH(name, ret, obj, param) static SAVEDS ret \
146 name(UNUSED struct Hook *_hook, obj, param)
147 #define HOOKPROTONHNO(name, ret, param) static SAVEDS ret \
148 name(UNUSED struct Hook *_hook, UNUSED APTR _obj, param)
149 #define HOOKPROTONHNP(name, ret, obj) static SAVEDS ret \
150 name(UNUSED struct Hook *_hook, obj, UNUSED APTR _param)
151 #define HOOKPROTONHNONP(name, ret) static SAVEDS ret name(void)
156 #ifndef SDI_TRAP_LIB /* avoid defining this twice */
157 #include <proto/alib.h>
158 #include <emul/emulregs.h>
160 #define SDI_TRAP_LIB 0xFF00 /* SDI prefix to reduce conflicts */
162 struct SDI_EmulLibEntry
170 #define MakeHook(hookname, funcname) struct Hook hookname = {{NULL, NULL}, \
171 (HOOKFUNC)HookEntry, (HOOKFUNC)funcname, NULL}
172 #define MakeHookWithData(hookname, funcname, data) struct Hook hookname = \
173 {{NULL, NULL}, (HOOKFUNC)HookEntry, (HOOKFUNC)funcname, (APTR)data}
174 #define MakeStaticHook(hookname, funcname) static struct Hook hookname = \
175 {{NULL, NULL}, (HOOKFUNC)HookEntry, (HOOKFUNC)funcname, NULL}
176 #define DISPATCHERPROTO(name) ULONG name(struct IClass * cl, Object * obj, \
178 extern const struct SDI_EmulLibEntry Gate_##name
179 #define DISPATCHER(name) \
181 ULONG name(struct IClass * cl, Object * obj, Msg msg); \
182 static ULONG Trampoline_##name(void) {return name((struct IClass *) \
183 REG_A0, (Object *) REG_A2, (Msg) REG_A1);} \
184 const struct SDI_EmulLibEntry Gate_##name = {SDI_TRAP_LIB, 0, \
185 (APTR) Trampoline_##name}; \
186 ULONG name(struct IClass * cl, Object * obj, Msg msg)
187 #define SDISPATCHER(name) \
189 static ULONG name(struct IClass * cl, Object * obj, Msg msg); \
190 static ULONG Trampoline_##name(void) {return name((struct IClass *) \
191 REG_A0, (Object *) REG_A2, (Msg) REG_A1);} \
192 static const struct SDI_EmulLibEntry Gate_##name = {SDI_TRAP_LIB, 0, \
193 (APTR) Trampoline_##name}; \
194 static ULONG name(struct IClass * cl, Object * obj, Msg msg)
195 #define ENTRY(func) (APTR)&Gate_##func
197 #elif defined(__AROS__)
198 #include <proto/alib.h>
200 #define MakeHook(hookname, funcname) struct Hook hookname = {{NULL, NULL}, \
201 (HOOKFUNC)HookEntry, (HOOKFUNC)funcname, NULL}
202 #define MakeHookWithData(hookname, funcname, data) struct Hook hookname = \
203 {{NULL, NULL}, (HOOKFUNC)HookEntry, (HOOKFUNC)funcname, (APTR)data}
204 #define MakeStaticHook(hookname, funcname) static struct Hook hookname = \
205 {{NULL, NULL}, (HOOKFUNC)HookEntry, (HOOKFUNC)funcname, NULL}
206 #define DISPATCHERPROTO(name) \
207 IPTR name(struct IClass * cl, Object * obj, Msg msg); \
208 AROS_UFP3(IPTR, Gate_##name, \
209 AROS_UFPA(struct IClass *, cl, A0), \
210 AROS_UFPA(Object *, obj, A2), \
211 AROS_UFPA(Msg, msg, A1))
212 #define DISPATCHERx(x,name) \
213 x IPTR name(struct IClass * cl, Object * obj, Msg msg); \
214 x AROS_UFH3(IPTR, Gate_##name, \
215 AROS_UFHA(struct IClass *, cl, A0), \
216 AROS_UFHA(Object *, obj, A2), \
217 AROS_UFHA(Msg, msg, A1)) \
218 { AROS_USERFUNC_INIT \
219 return name(cl, obj, msg); \
222 x IPTR name(struct IClass * cl, Object * obj, Msg msg)
223 #define DISPATCHER(name) DISPATCHERx(,name)
224 #define SDISPATCHER(name) DISPATCHERx(static,name)
225 #define ENTRY(func) (APTR)Gate_##func
227 #else /* !__MORPHOS__ && !__AROS__*/
229 #define MakeHook(hookname, funcname) struct Hook hookname = {{NULL, NULL}, \
230 (HOOKFUNC)funcname, NULL, NULL}
231 #define MakeHookWithData(hookname, funcname, data) struct Hook hookname = \
232 {{NULL, NULL}, (HOOKFUNC)funcname, NULL, (APTR)data}
233 #define MakeStaticHook(hookname, funcname) static struct Hook hookname = \
234 {{NULL, NULL}, (HOOKFUNC)funcname, NULL, NULL}
235 #define ENTRY(func) (APTR)func
236 #define DISPATCHERPROTO(name) SAVEDS ASM IPTR name(REG(a0, \
237 struct IClass * cl), REG(a2, Object * obj), REG(a1, Msg msg))
238 #define DISPATCHER(name) DISPATCHERPROTO(name)
239 #define SDISPATCHER(name) static DISPATCHERPROTO(name)
243 #define InitHook(hook, orighook, data) ((hook)->h_Entry = (orighook).h_Entry,\
244 (hook)->h_SubEntry = (orighook).h_SubEntry,(hook)->h_Data = (APTR)(data))
246 #endif /* SDI_HOOK_H */