提交 f7897430 编写于 作者: A Anthony Liguori

Fix module initialization when more than 1 class is in use

Now that we're using enums for module types, it makes no sense to bother keeping
a list of module types when we know exactly how many they are.

Switching to an array simplifies the code and eliminates the aforementioned bug.
Signed-off-by: NAnthony Liguori <aliguori@us.ibm.com>
上级 4af39611
...@@ -22,44 +22,36 @@ typedef struct ModuleEntry ...@@ -22,44 +22,36 @@ typedef struct ModuleEntry
TAILQ_ENTRY(ModuleEntry) node; TAILQ_ENTRY(ModuleEntry) node;
} ModuleEntry; } ModuleEntry;
typedef struct ModuleTypeList typedef TAILQ_HEAD(, ModuleEntry) ModuleTypeList;
{
module_init_type type;
TAILQ_HEAD(, ModuleEntry) entry_list;
TAILQ_ENTRY(ModuleTypeList) node;
} ModuleTypeList;
static TAILQ_HEAD(, ModuleTypeList) init_type_list; static ModuleTypeList init_type_list[MODULE_INIT_MAX];
static ModuleTypeList *find_type_or_alloc(module_init_type type, int alloc) static void init_types(void)
{ {
ModuleTypeList *n; static int inited;
int i;
TAILQ_FOREACH(n, &init_type_list, node) { if (inited) {
if (type >= n->type) return;
break;
} }
if (!n || n->type != type) { for (i = 0; i < MODULE_INIT_MAX; i++) {
ModuleTypeList *o; TAILQ_INIT(&init_type_list[i]);
}
if (!alloc) inited = 1;
return NULL; }
o = qemu_mallocz(sizeof(*o));
o->type = type;
TAILQ_INIT(&o->entry_list);
if (n) { static ModuleTypeList *find_type(module_init_type type)
TAILQ_INSERT_AFTER(&init_type_list, n, o, node); {
} else { ModuleTypeList *l;
TAILQ_INSERT_HEAD(&init_type_list, o, node);
}
n = o; init_types();
}
l = &init_type_list[type];
return n; return l;
} }
void register_module_init(void (*fn)(void), module_init_type type) void register_module_init(void (*fn)(void), module_init_type type)
...@@ -70,9 +62,9 @@ void register_module_init(void (*fn)(void), module_init_type type) ...@@ -70,9 +62,9 @@ void register_module_init(void (*fn)(void), module_init_type type)
e = qemu_mallocz(sizeof(*e)); e = qemu_mallocz(sizeof(*e));
e->init = fn; e->init = fn;
l = find_type_or_alloc(type, 1); l = find_type(type);
TAILQ_INSERT_TAIL(&l->entry_list, e, node); TAILQ_INSERT_TAIL(l, e, node);
} }
void module_call_init(module_init_type type) void module_call_init(module_init_type type)
...@@ -80,12 +72,9 @@ void module_call_init(module_init_type type) ...@@ -80,12 +72,9 @@ void module_call_init(module_init_type type)
ModuleTypeList *l; ModuleTypeList *l;
ModuleEntry *e; ModuleEntry *e;
l = find_type_or_alloc(type, 0); l = find_type(type);
if (!l) {
return;
}
TAILQ_FOREACH(e, &l->entry_list, node) { TAILQ_FOREACH(e, l, node) {
e->init(); e->init();
} }
} }
...@@ -17,12 +17,13 @@ ...@@ -17,12 +17,13 @@
/* This should not be used directly. Use block_init etc. instead. */ /* This should not be used directly. Use block_init etc. instead. */
#define module_init(function, type) \ #define module_init(function, type) \
static void __attribute__((constructor)) do_qemu_init_ ## function(void) { \ static void __attribute__((constructor)) do_qemu_init_ ## function(void) { \
register_module_init(function, type); \ register_module_init(function, type); \
} }
typedef enum { typedef enum {
MODULE_INIT_BLOCK, MODULE_INIT_BLOCK,
MODULE_INIT_DEVICE MODULE_INIT_DEVICE,
MODULE_INIT_MAX
} module_init_type; } module_init_type;
#define block_init(function) module_init(function, MODULE_INIT_BLOCK) #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册