Introduce our own shutdown callback mechanism.

Instead of adding stuff to `_shutdown()`, we can now register a shutdown
routine from the respective `init()` function of our modules.

This also has the advantage, that we're sure they're called in reverse
order from how the initialization happened.

I didn't simply use `atexit()` because POSIX says that the number of
possible callbacks is limited (min 32) and I was not sure whether
we will maybe extend this number at one point.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2025-03-07 11:44:31 +01:00
parent 662a0be633
commit c5a131ee46
39 changed files with 321 additions and 327 deletions

View File

@@ -66,6 +66,28 @@ static void _theme_list_dir(const gchar* const dir, GSList** result);
static GString* _theme_find(const char* const theme_name);
static gboolean _theme_load_file(const char* const theme_name);
static void
_theme_close(void)
{
color_pair_cache_free();
if (theme) {
g_key_file_free(theme);
theme = NULL;
}
if (theme_loc) {
g_string_free(theme_loc, TRUE);
theme_loc = NULL;
}
if (bold_items) {
g_hash_table_destroy(bold_items);
bold_items = NULL;
}
if (defaults) {
g_hash_table_destroy(defaults);
defaults = NULL;
}
}
void
theme_init(const char* const theme_name)
{
@@ -77,6 +99,8 @@ theme_init(const char* const theme_name)
}
}
prof_add_shutdown_routine(_theme_close);
defaults = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
// Set default colors
@@ -248,28 +272,6 @@ theme_list(void)
return result;
}
void
theme_close(void)
{
color_pair_cache_free();
if (theme) {
g_key_file_free(theme);
theme = NULL;
}
if (theme_loc) {
g_string_free(theme_loc, TRUE);
theme_loc = NULL;
}
if (bold_items) {
g_hash_table_destroy(bold_items);
bold_items = NULL;
}
if (defaults) {
g_hash_table_destroy(defaults);
defaults = NULL;
}
}
void
theme_init_colours(void)
{