-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.h
84 lines (68 loc) · 2.93 KB
/
module.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef AEM_MODULE_H
#define AEM_MODULE_H
#include <aem/log.h>
#include <aem/stringbuf.h>
#include <aem/stringslice.h>
struct aem_module;
struct aem_module_def {
// TODO: Generic key-value store
const char *name;
const char *version;
const char *deps;
// Called after module is loaded
int (*reg)(struct aem_module *mod, struct aem_stringslice args);
// Called before module is unloaded
void (*dereg)(struct aem_module *mod);
// Called before a module is registered to determine whether it can be
// safely loaded. Return 1 if OK, else 0. NULL => OK
int (*check_reg)(struct aem_module *mod);
// Called before a module is deregistered to determine whether it can
// be safely unloaded. Return 1 if OK, else 0. NULL => OK
int (*check_dereg)(struct aem_module *mod);
struct aem_log_module *logmodule;
};
// Set def->check_reg to this for modules that should never have more than one
// instance loaded at a time.
// You must define this yourself to return 0 if a module with the same mod->def
// is already loaded.
extern int aem_module_check_reg_singleton(struct aem_module *mod);
// Set def->check_dereg to this for modules that should never be unloaded
int aem_module_disable_dereg(struct aem_module *mod);
enum aem_module_state {
AEM_MODULE_UNREGISTERED,
AEM_MODULE_LOADED,
AEM_MODULE_REGISTERED,
};
struct aem_module {
struct aem_stringbuf name;
struct aem_stringbuf path;
void *handle;
const struct aem_module_def *def;
struct aem_log_module *logmodule;
enum aem_module_state state;
};
extern struct aem_stringbuf aem_module_path;
void aem_module_path_set(const char *dir);
extern struct aem_log_module *aem_module_logmodule;
void aem_module_init(struct aem_module *mod);
// Calls aem_module_unload and then frees memory
void aem_module_dtor(struct aem_module *mod);
int aem_module_resolve_path(struct aem_module *mod);
// Open a module. Call after calling aem_module_init and either setting the
// name field and calling aem_module_resolve_path, or setting the path field
// yourself. Returns non-zero on error, in which case you must destroy the
// module yourself via aem_module_unload or aem_module_dtor.
int aem_module_open(struct aem_module *mod);
// Tell a module to register itself. Call after successfully calling
// aem_module_open. Returns non-zero on error, in which case you must destroy
// the module yourself via aem_module_unload or aem_module_dtor.
int aem_module_register(struct aem_module *mod, struct aem_stringslice args);
// Returns 1 if a module can be unloaded, else 0
int aem_module_unload_check(struct aem_module *mod);
// Unload a module, even if only partially loaded. Idempotent.
// You should normally call aem_module_unload_check on a fully-loaded module
// before unloading it via this function.
int aem_module_unload(struct aem_module *mod);
void *aem_module_get_sym(struct aem_module *mod, const char *symbol);
void aem_module_identify(struct aem_stringbuf *out, struct aem_module *mod);
#endif /* AEM_MODULE_H */