1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 /* A module contains some functionality which becomes available atfer it is
25 initialized and before it is destroyed. A module is characterized by a name,
26 a description (short and long), a list of names of other modules on which
27 it depends, and an initialization and a destruction function.
29 A library contains one or more modules and may be loaded dynamically.
30 The modules contained in a library are automatically registered through
31 constructors which are called when the library is loaded. For modules
32 directly linked with the main program (builtin), the constructors are
33 called before the main program starts. (However, neither malloc nor glib
34 functions are used during the registration process).
36 The library loading path is a set of directories, where requested
37 libraries and modules are searched for.
40 typedef struct _LttvModule LttvModule
;
42 typedef struct _LttvLibrary LttvLibrary
;
44 typedef void (*LttvModuleInit
)();
46 typedef void (*LttvModuleDestroy
)();
48 typedef struct _LttvModuleInfo
51 char *short_description
;
54 LttvModuleDestroy destroy
;
56 unsigned require_count
;
58 unsigned prerequisites_number
;
62 typedef struct _LttvLibraryInfo
70 typedef enum _LttvModuleError
72 LTTV_MODULE_NOT_FOUND
,
77 /* Insure that a module is loaded and initialized. Require count
78 (number of times the module was required) and use count
79 (number of times required or used as prerequisite) serve to
80 insure that a module is destroyed only after it has been released
81 as many times as it was required (and prerequired).
83 The module is searched among the modules currently loaded, then as a
84 similarly named library to load which should contain the named module.
85 If the module cannot be found or loaded, NULL is returned and an
86 explanation is provided in error. */
88 LttvModule
*lttv_module_require(char *name
, GError
**error
);
90 void lttv_module_release(LttvModule
*m
);
93 /* Obtain information about the module, including the containing library */
95 void lttv_module_info(LttvModule
*m
, LttvModuleInfo
*info
);
98 /* List the modules on which this module depends */
100 unsigned lttv_module_prerequisite_number(LttvModule
*m
);
102 LttvModule
*lttv_module_prerequisite_get(LttvModule
*m
, unsigned i
);
105 /* Insure that a library is loaded. A load count insures that a library
106 is unloaded only after it has been asked to unload as
107 many times as it was loaded, and its modules are not in use. The library
108 is searched along the library path if name is a relative pathname.
109 If the library cannot be found or loaded, NULL is returned and an
110 explanation is provided in error. */
112 LttvLibrary
*lttv_library_load(char *name
, GError
**error
);
114 /* Returns 0 if library is unloaded, > 0 otherwise */
115 gint
lttv_library_unload(LttvLibrary
*l
);
118 /* Obtain information about the library */
120 void lttv_library_info(LttvLibrary
*l
, LttvLibraryInfo
*info
);
123 /* List the modules contained in a library */
125 unsigned lttv_library_module_number(LttvLibrary
*l
);
127 LttvModule
*lttv_library_module_get(LttvLibrary
*l
, unsigned i
);
130 /* List the currently loaded libraries */
132 unsigned lttv_library_number();
134 LttvLibrary
*lttv_library_get(unsigned i
);
138 /* Add or remove directory names to the library search path */
140 void lttv_library_path_add(const char *name
);
142 void lttv_library_path_remove(const char *name
);
145 /* List the directory names in the library search path */
147 unsigned lttv_library_path_number();
149 char *lttv_library_path_get(unsigned i
);
152 /* To define a module, simply call the LTTV_MODULE macro with the needed
153 arguments: single word name, one line short description, larger
154 description, initialization function, destruction function, and
155 list of names for required modules (e.g., "moduleA", "moduleB").
156 This will insure that the module is registered at library load time.
160 LTTV_MODULE("option", "Command line options processing", "...", \
161 init, destroy, "moduleA", "moduleB")
164 #define LTTV_MODULE(name, short_desc, desc, init, destroy, ...) \
166 static void _LTTV_MODULE_REGISTER(__LINE__)() \
167 __attribute__((constructor)); \
169 static void _LTTV_MODULE_REGISTER(__LINE__)() \
171 static char *module_prerequisites[] = { __VA_ARGS__ }; \
173 static struct _LttvModuleDescription module = { \
174 name, short_desc, desc, init, destroy, \
175 sizeof(module_prerequisites) / sizeof(char *), \
176 module_prerequisites, NULL}; \
178 lttv_module_register(&module); \
182 /* Internal structure and function used to register modules, called by
185 #define __LTTV_MODULE_REGISTER(line) _lttv_module_register_ ## line
186 #define _LTTV_MODULE_REGISTER(line) __LTTV_MODULE_REGISTER(line)
188 struct _LttvModuleDescription
191 char *short_description
;
194 LttvModuleDestroy destroy
;
195 unsigned prerequisites_number
;
196 char **prerequisites
;
197 struct _LttvModuleDescription
*next
;
200 void lttv_module_register(struct _LttvModuleDescription
*d
);