qemu/module.c
<<
>>
Prefs
   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
  16#include "qemu-common.h"
  17#include "qemu-queue.h"
  18#include "module.h"
  19
  20typedef struct ModuleEntry
  21{
  22    void (*init)(void);
  23    QTAILQ_ENTRY(ModuleEntry) node;
  24} ModuleEntry;
  25
  26typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
  27
  28static ModuleTypeList init_type_list[MODULE_INIT_MAX];
  29
  30static void init_types(void)
  31{
  32    static int inited;
  33    int i;
  34
  35    if (inited) {
  36        return;
  37    }
  38
  39    for (i = 0; i < MODULE_INIT_MAX; i++) {
  40        QTAILQ_INIT(&init_type_list[i]);
  41    }
  42
  43    inited = 1;
  44}
  45
  46
  47static ModuleTypeList *find_type(module_init_type type)
  48{
  49    ModuleTypeList *l;
  50
  51    init_types();
  52
  53    l = &init_type_list[type];
  54
  55    return l;
  56}
  57
  58void register_module_init(void (*fn)(void), module_init_type type)
  59{
  60    ModuleEntry *e;
  61    ModuleTypeList *l;
  62
  63    e = g_malloc0(sizeof(*e));
  64    e->init = fn;
  65
  66    l = find_type(type);
  67
  68    QTAILQ_INSERT_TAIL(l, e, node);
  69}
  70
  71void module_call_init(module_init_type type)
  72{
  73    ModuleTypeList *l;
  74    ModuleEntry *e;
  75
  76    l = find_type(type);
  77
  78    QTAILQ_FOREACH(e, l, node) {
  79        e->init();
  80    }
  81}
  82