Versatile/PB core qdev conversion
[qemu/mini2440.git] / module.c
blob113eeefc6e2deda0152c1eae8da45b769ed35353
1 /*
2 * QEMU Module Infrastructure
4 * Copyright IBM, Corp. 2009
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "sys-queue.h"
16 #include "module.h"
18 typedef struct ModuleEntry
20 module_init_type type;
21 void (*init)(void);
22 TAILQ_ENTRY(ModuleEntry) node;
23 } ModuleEntry;
25 typedef struct ModuleTypeList
27 module_init_type type;
28 TAILQ_HEAD(, ModuleEntry) entry_list;
29 TAILQ_ENTRY(ModuleTypeList) node;
30 } ModuleTypeList;
32 static TAILQ_HEAD(, ModuleTypeList) init_type_list;
34 static ModuleTypeList *find_type_or_alloc(module_init_type type, int alloc)
36 ModuleTypeList *n;
38 TAILQ_FOREACH(n, &init_type_list, node) {
39 if (type >= n->type)
40 break;
43 if (!n || n->type != type) {
44 ModuleTypeList *o;
46 if (!alloc)
47 return NULL;
49 o = qemu_mallocz(sizeof(*o));
50 o->type = type;
51 TAILQ_INIT(&o->entry_list);
53 if (n) {
54 TAILQ_INSERT_AFTER(&init_type_list, n, o, node);
55 } else {
56 TAILQ_INSERT_HEAD(&init_type_list, o, node);
59 n = o;
62 return n;
65 void register_module_init(void (*fn)(void), module_init_type type)
67 ModuleEntry *e;
68 ModuleTypeList *l;
70 e = qemu_mallocz(sizeof(*e));
71 e->init = fn;
73 l = find_type_or_alloc(type, 1);
75 TAILQ_INSERT_TAIL(&l->entry_list, e, node);
78 void module_call_init(module_init_type type)
80 ModuleTypeList *l;
81 ModuleEntry *e;
83 l = find_type_or_alloc(type, 0);
84 if (!l) {
85 return;
88 TAILQ_FOREACH(e, &l->entry_list, node) {
89 e->init();