5 #include "../core/error.h"
10 #define HF_CONTIGUOUS 0x01
13 HookCallback callback
;
19 typedef struct HookHead
{
24 static HookHead hooks
[HOOKMAX
];
25 static int dirtyhooks
[HOOKMAX
];
26 static int dirtyhookcount
;
28 unsigned int hookqueuelength
= 0;
30 static void collectgarbage(HookHead
*h
);
31 static void markdirty(int hook
);
36 int registerhook(int hooknum
, HookCallback callback
) {
37 return registerpriorityhook(hooknum
, callback
, PRIORITY_DEFAULT
);
40 int registerpriorityhook(int hooknum
, HookCallback callback
, long priority
) {
47 if(hookqueuelength
> 0)
48 Error("core", ERR_WARNING
, "Attempting to register hook %d inside a hook queue: %p", hooknum
, callback
);
50 for(pred
=NULL
,h
=&hooks
[hooknum
],hp
=h
->head
;hp
;hp
=hp
->next
) {
51 if(hp
->callback
==callback
)
53 if(priority
>=hp
->priority
)
57 n
= malloc(sizeof(Hook
));
58 n
->priority
= priority
;
59 n
->callback
= callback
;
73 int deregisterhook(int hooknum
, HookCallback callback
) {
80 if(hookqueuelength
> 0)
81 Error("core", ERR_WARNING
, "Attempting to deregister hook %d inside a hook queue: %p", hooknum
, callback
);
83 for(h
=&hooks
[hooknum
],hp
=h
->head
;hp
;hp
=hp
->next
) {
84 if(hp
->callback
==callback
) {
94 void triggerhook(int hooknum
, void *arg
) {
102 for(hp
=hooks
[hooknum
].head
;hp
;hp
=hp
->next
) {
104 (hp
->callback
)(hooknum
, arg
);
108 if (!hookqueuelength
&& hooknum
!=HOOK_CORE_ENDOFHOOKSQUEUE
) {
109 triggerhook(HOOK_CORE_ENDOFHOOKSQUEUE
, 0);
111 for(i
=0;i
<dirtyhookcount
;i
++) {
112 collectgarbage(&hooks
[dirtyhooks
[i
]]);
118 static void markdirty(int hook
) {
119 if(hooks
[hook
].dirty
)
122 hooks
[hook
].dirty
= 1;
123 dirtyhooks
[dirtyhookcount
++] = hook
;
126 static void collectgarbage(HookHead
*h
) {
129 for(pp
=NULL
,hp
=h
->head
;hp
;hp
=np
) {
131 if(hp
->callback
==NULL
) {