xref: /minix3/minix/lib/libddekit/src/initcall.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include "common.h"
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc #include <ddekit/initcall.h>
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc 
6*433d6423SLionel Sambuc #ifdef DDEKIT_DEBUG_INITCALL
7*433d6423SLionel Sambuc #undef DDEBUG
8*433d6423SLionel Sambuc #define DDEBUG DDEKIT_DEBUG_INITCALL
9*433d6423SLionel Sambuc #endif
10*433d6423SLionel Sambuc 
11*433d6423SLionel Sambuc #include "debug.h"
12*433d6423SLionel Sambuc 
13*433d6423SLionel Sambuc static struct __ddekit_initcall_s head = {0,0,0};
14*433d6423SLionel Sambuc 
15*433d6423SLionel Sambuc /****************************************************************************/
16*433d6423SLionel Sambuc /*        __ddekit_add_initcall                                             */
17*433d6423SLionel Sambuc /****************************************************************************/
18*433d6423SLionel Sambuc void __attribute__((used))
__ddekit_add_initcall(struct __ddekit_initcall_s * ic)19*433d6423SLionel Sambuc __ddekit_add_initcall(struct __ddekit_initcall_s * ic) {
20*433d6423SLionel Sambuc 
21*433d6423SLionel Sambuc 	/* This function is required for the DDEKIT_INITCALL makro */
22*433d6423SLionel Sambuc 
23*433d6423SLionel Sambuc 	struct __ddekit_initcall_s *i = 0;
24*433d6423SLionel Sambuc 
25*433d6423SLionel Sambuc 	DDEBUG_MSG_VERBOSE("adding initcall (%p) to %p with prio %d head at %p",
26*433d6423SLionel Sambuc 	ic, ic->func, ic->prio, &head);
27*433d6423SLionel Sambuc 
28*433d6423SLionel Sambuc 	for (i = &head; i; i=i->next)
29*433d6423SLionel Sambuc 	{
30*433d6423SLionel Sambuc 		if (!i->next) {
31*433d6423SLionel Sambuc 			i->next = ic;
32*433d6423SLionel Sambuc 			return;
33*433d6423SLionel Sambuc 		}
34*433d6423SLionel Sambuc 		if (i->next->prio > ic->prio) {
35*433d6423SLionel Sambuc 			ic->next = i->next;
36*433d6423SLionel Sambuc 			i->next = ic;
37*433d6423SLionel Sambuc 			return;
38*433d6423SLionel Sambuc 		}
39*433d6423SLionel Sambuc 	}
40*433d6423SLionel Sambuc }
41*433d6423SLionel Sambuc 
42*433d6423SLionel Sambuc /****************************************************************************/
43*433d6423SLionel Sambuc /*        ddekit_do_initcalls                                               */
44*433d6423SLionel Sambuc /****************************************************************************/
ddekit_do_initcalls()45*433d6423SLionel Sambuc void ddekit_do_initcalls()
46*433d6423SLionel Sambuc {
47*433d6423SLionel Sambuc 	struct __ddekit_initcall_s *i = 0;
48*433d6423SLionel Sambuc 
49*433d6423SLionel Sambuc 	DDEBUG_MSG_VERBOSE("exectuing initcalls (head at %p, head->next = %p)",
50*433d6423SLionel Sambuc 		&head, head.next);
51*433d6423SLionel Sambuc 
52*433d6423SLionel Sambuc 	for (i = head.next; i; i=i->next) {
53*433d6423SLionel Sambuc 		DDEBUG_MSG_VERBOSE("executing initcall: %p with prio %d",
54*433d6423SLionel Sambuc 		   i->func, i->prio);
55*433d6423SLionel Sambuc 		i->func();
56*433d6423SLionel Sambuc 	}
57*433d6423SLionel Sambuc }
58