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

@@ -61,10 +61,22 @@ static Autocomplete enabled_ac;
static void _save_accounts(void);
static void
_accounts_close(void)
{
autocomplete_free(all_ac);
autocomplete_free(enabled_ac);
free_keyfile(&accounts_prof_keyfile);
accounts = NULL;
}
void
accounts_load(void)
{
log_info("Loading accounts");
prof_add_shutdown_routine(_accounts_close);
all_ac = autocomplete_new();
enabled_ac = autocomplete_new();
load_data_keyfile(&accounts_prof_keyfile, FILE_ACCOUNTS);
@@ -82,15 +94,6 @@ accounts_load(void)
}
}
void
accounts_close(void)
{
autocomplete_free(all_ac);
autocomplete_free(enabled_ac);
free_keyfile(&accounts_prof_keyfile);
accounts = NULL;
}
char*
accounts_find_enabled(const char* const prefix, gboolean previous, void* context)
{

View File

@@ -42,7 +42,6 @@
#include "config/account.h"
void accounts_load(void);
void accounts_close(void);
char* accounts_find_all(const char* const prefix, gboolean previous, void* context);
char* accounts_find_enabled(const char* const prefix, gboolean previous, void* context);

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)
{

View File

@@ -56,10 +56,25 @@ static Autocomplete certs_ac;
static char* current_fp;
static void
_tlscerts_close(void)
{
free_keyfile(&tlscerts_prof_keyfile);
tlscerts = NULL;
free(current_fp);
current_fp = NULL;
autocomplete_free(certs_ac);
}
void
tlscerts_init(void)
{
log_info("Loading TLS certificates");
prof_add_shutdown_routine(_tlscerts_close);
load_data_keyfile(&tlscerts_prof_keyfile, FILE_TLSCERTS);
tlscerts = tlscerts_prof_keyfile.keyfile;
@@ -356,18 +371,6 @@ tlscerts_free(TLSCertificate* cert)
}
}
void
tlscerts_close(void)
{
free_keyfile(&tlscerts_prof_keyfile);
tlscerts = NULL;
free(current_fp);
current_fp = NULL;
autocomplete_free(certs_ac);
}
static void
_save_tlscerts(void)
{

View File

@@ -96,6 +96,4 @@ char* tlscerts_complete(const char* const prefix, gboolean previous, void* conte
void tlscerts_reset_ac(void);
void tlscerts_close(void);
#endif