2
0

module.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu-common.h"
  16. #include "qemu-queue.h"
  17. #include "module.h"
  18. typedef struct ModuleEntry
  19. {
  20. void (*init)(void);
  21. QTAILQ_ENTRY(ModuleEntry) node;
  22. } ModuleEntry;
  23. typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
  24. static ModuleTypeList init_type_list[MODULE_INIT_MAX];
  25. static void init_types(void)
  26. {
  27. static int inited;
  28. int i;
  29. if (inited) {
  30. return;
  31. }
  32. for (i = 0; i < MODULE_INIT_MAX; i++) {
  33. QTAILQ_INIT(&init_type_list[i]);
  34. }
  35. inited = 1;
  36. }
  37. static ModuleTypeList *find_type(module_init_type type)
  38. {
  39. ModuleTypeList *l;
  40. init_types();
  41. l = &init_type_list[type];
  42. return l;
  43. }
  44. void register_module_init(void (*fn)(void), module_init_type type)
  45. {
  46. ModuleEntry *e;
  47. ModuleTypeList *l;
  48. e = g_malloc0(sizeof(*e));
  49. e->init = fn;
  50. l = find_type(type);
  51. QTAILQ_INSERT_TAIL(l, e, node);
  52. }
  53. void module_call_init(module_init_type type)
  54. {
  55. ModuleTypeList *l;
  56. ModuleEntry *e;
  57. l = find_type(type);
  58. QTAILQ_FOREACH(e, l, node) {
  59. e->init();
  60. }
  61. }