#include <ecoli_log.h>
#include <ecoli_node.h>
+/**
+ * Register initialization and exit callbacks. These callbacks are
+ * ordered by priority: for initialization, the lowest priority is called
+ * first. For exit, the callbacks are invoked in reverse order.
+ */
#define EC_INIT_REGISTER(t) \
static void ec_init_init_##t(void); \
static void __attribute__((constructor, used)) \
*/
typedef int (ec_init_t)(void);
+/**
+ * Type of exit function.
+ */
+typedef void (ec_exit_t)(void);
+
TAILQ_HEAD(ec_init_list, ec_init);
/**
struct ec_init {
TAILQ_ENTRY(ec_init) next; /**< Next in list. */
ec_init_t *init; /**< Init function. */
+ ec_exit_t *exit; /**< Exit function. */
unsigned int priority; /**< Priority (0=first, 99=last) */
};
void ec_init_register(struct ec_init *test);
/**
- * Initialize ecoli library
+ * Initialize ecoli library.
*
* Must be called before any other function from libecoli, except
* ec_malloc_register().
*/
int ec_init(void);
+/**
+ * Uninitialize ecoli library.
+ */
+void ec_exit(void);
+
#endif
struct ec_init *init;
TAILQ_FOREACH(init, &init_list, next) {
- if (init->init() < 0)
+ if (init->init != NULL && init->init() < 0)
return -1;
}
return 0;
}
+
+void ec_exit(void)
+{
+ struct ec_init *init;
+
+ TAILQ_FOREACH_REVERSE(init, &init_list, ec_init_list, next) {
+ if (init->exit != NULL)
+ init->exit();
+ }
+}