Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Module Infrastructure |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2009 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "qemu-common.h" |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 15 | #include "qemu-queue.h" |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 16 | #include "module.h" |
| 17 | |
| 18 | typedef struct ModuleEntry |
| 19 | { |
| 20 | module_init_type type; |
| 21 | void (*init)(void); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 22 | QTAILQ_ENTRY(ModuleEntry) node; |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 23 | } ModuleEntry; |
| 24 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 25 | typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList; |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 26 | |
| 27 | static ModuleTypeList init_type_list[MODULE_INIT_MAX]; |
| 28 | |
| 29 | static void init_types(void) |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 30 | { |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 31 | static int inited; |
| 32 | int i; |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 33 | |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 34 | if (inited) { |
| 35 | return; |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 38 | for (i = 0; i < MODULE_INIT_MAX; i++) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 39 | QTAILQ_INIT(&init_type_list[i]); |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 42 | inited = 1; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static ModuleTypeList *find_type(module_init_type type) |
| 47 | { |
| 48 | ModuleTypeList *l; |
| 49 | |
| 50 | init_types(); |
| 51 | |
| 52 | l = &init_type_list[type]; |
| 53 | |
| 54 | return l; |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void register_module_init(void (*fn)(void), module_init_type type) |
| 58 | { |
| 59 | ModuleEntry *e; |
| 60 | ModuleTypeList *l; |
| 61 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 62 | e = g_malloc0(sizeof(*e)); |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 63 | e->init = fn; |
| 64 | |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 65 | l = find_type(type); |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 66 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 67 | QTAILQ_INSERT_TAIL(l, e, node); |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void module_call_init(module_init_type type) |
| 71 | { |
| 72 | ModuleTypeList *l; |
| 73 | ModuleEntry *e; |
| 74 | |
Anthony Liguori | f789743 | 2009-05-14 17:57:31 -0500 | [diff] [blame] | 75 | l = find_type(type); |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 76 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 77 | QTAILQ_FOREACH(e, l, node) { |
Anthony Liguori | 0bfe3ca | 2009-05-14 19:29:53 +0100 | [diff] [blame] | 78 | e->init(); |
| 79 | } |
| 80 | } |