config: make passing talloc context optional for some functions

Until now, the config functions added various allocations to the user-
provided talloc context. Make it so that they're all under the returned
allocation instead. This allows avoiding having to create an extra
temporary context for some callers, and also avoids adding random memory
leaks by accidentally passing a NULL context.

mp_find_all_config_files() has to be changed not to return a pointer
into the middle array for this to work. Make it add paths in order
(instead of reverse), and then reverse the array entries after that.

Also remove the declarations for the win-specific private functions.
Remove STRNULL(); it's barely needed anymore and the functions are
not called with NULL filenames anymore.
This commit is contained in:
wm4 committed 2014-06-26 19:56:45 +02:00
1 parent 0dcde951b9
commit 3e1c0e5853
3 files changed
+28 -31

No files matched your search

+21 -17
View File
@@ -35,6 +35,7 @@
#include "config.h"
#include "common/common.h"
#include "common/global.h"
#include "common/msg.h"
#include "options/options.h"
@@ -43,11 +44,10 @@
#include "osdep/io.h"
#include "osdep/path.h"
#define STRNULL(s) ((s) ? (s) : "(NULL)")
static void mp_add_xdg_config_dirs(void *talloc_ctx, struct mpv_global *global,
char **dirs, int i)
static void mp_add_xdg_config_dirs(struct mpv_global *global, char **dirs, int i)
{
void *talloc_ctx = dirs;
char *home = getenv("HOME");
char *tmp = NULL;
@@ -106,7 +106,7 @@ static char **mp_config_dirs(void *talloc_ctx, struct mpv_global *global)
char **ret = talloc_zero_array(talloc_ctx, char*, MAX_CONFIG_PATHS);
if (global->opts->force_configdir && global->opts->force_configdir[0]) {
ret[0] = talloc_strdup(talloc_ctx, global->opts->force_configdir);
ret[0] = talloc_strdup(ret, global->opts->force_configdir);
return ret;
}
@@ -115,12 +115,12 @@ static char **mp_config_dirs(void *talloc_ctx, struct mpv_global *global)
tmp = getenv("MPV_HOME");
if (tmp && *tmp)
ret[i++] = talloc_strdup(talloc_ctx, tmp);
ret[i++] = talloc_strdup(ret, tmp);
#if defined(_WIN32) && !defined(__CYGWIN__)
mp_add_win_config_dirs(talloc_ctx, global, ret, i);
mp_add_win_config_dirs(global, ret, i);
#else
mp_add_xdg_config_dirs(talloc_ctx, global, ret, i);
mp_add_xdg_config_dirs(global, ret, i);
#endif
MP_VERBOSE(global, "search dirs:");
@@ -151,8 +151,8 @@ char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
talloc_free(tmp);
MP_VERBOSE(global, "config path: '%s' -> '%s'\n", STRNULL(filename),
STRNULL(res));
MP_VERBOSE(global, "config path: '%s' -> '%s'\n", filename,
res ? res : "(NULL)");
return res;
}
@@ -161,21 +161,25 @@ char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global,
{
struct MPOpts *opts = global->opts;
char **front = talloc_zero_array(talloc_ctx, char*, MAX_CONFIG_PATHS);
char **ret = front + (MAX_CONFIG_PATHS - 1);
char **ret = talloc_zero_array(talloc_ctx, char*, MAX_CONFIG_PATHS + 1);
int num_ret = 0;
if (opts->load_config) {
for (char **dir = mp_config_dirs(talloc_ctx, global); *dir; dir++) {
char *config_file = talloc_asprintf(talloc_ctx, "%s/%s", *dir, filename);
char **dir = mp_config_dirs(NULL, global);
for (int i = 0; dir && dir[i]; i++) {
char *file = talloc_asprintf(ret, "%s/%s", dir[i], filename);
if (!mp_path_exists(config_file))
if (!mp_path_exists(file) || num_ret >= MAX_CONFIG_PATHS)
continue;
*(--ret) = config_file;
ret[num_ret++] = file;
}
}
MP_VERBOSE(global, "config file: '%s'\n", STRNULL(filename));
for (int n = 0; n < num_ret / 2; n++)
MPSWAP(char*, ret[n], ret[num_ret - n - 1]);
MP_VERBOSE(global, "config file: '%s'\n", filename);
for (char** c = ret; *c; c++)
MP_VERBOSE(global, " -> '%s'\n", *c);
+6 -7
View File
@@ -24,7 +24,7 @@
// Warning: do not use PATH_MAX. Cygwin messed it up.
char *mp_get_win_exe_dir(void *talloc_ctx)
static char *mp_get_win_exe_dir(void *talloc_ctx)
{
wchar_t w_exedir[MAX_PATH + 1] = {0};
@@ -41,12 +41,13 @@ char *mp_get_win_exe_dir(void *talloc_ctx)
return mp_to_utf8(talloc_ctx, w_exedir);
}
char *mp_get_win_exe_subdir(void *talloc_ctx)
static char *mp_get_win_exe_subdir(void *talloc_ctx)
{
return talloc_asprintf(talloc_ctx, "%s/mpv", mp_get_win_exe_dir(talloc_ctx));
}
char *mp_get_win_app_dir(void *talloc_ctx)
static char *mp_get_win_app_dir(void *talloc_ctx)
{
wchar_t w_appdir[MAX_PATH + 1] = {0};
@@ -57,11 +58,9 @@ char *mp_get_win_app_dir(void *talloc_ctx)
return talloc_asprintf(talloc_ctx, "%s/mpv", mp_to_utf8(talloc_ctx, w_appdir));
}
void mp_add_win_config_dirs(void *talloc_ctx, struct mpv_global *global,
char **dirs, int i)
void mp_add_win_config_dirs(struct mpv_global *global, char **dirs, int i)
{
void *talloc_ctx = dirs;
if ((dirs[i] = mp_get_win_exe_subdir(talloc_ctx)))
i++;
if ((dirs[i] = mp_get_win_exe_dir(talloc_ctx)))
+1 -7
View File
@@ -5,13 +5,7 @@
struct mpv_global;
// Windows config directories
char *mp_get_win_exe_dir(void *talloc_ctx);
char *mp_get_win_exe_subdir(void *talloc_ctx);
char *mp_get_win_app_dir(void *talloc_ctx);
void mp_add_win_config_dirs(void *talloc_ctx, struct mpv_global *global,
char **dirs, int i);
void mp_add_win_config_dirs(struct mpv_global *global, char **dirs, int i);
// Returns Mac OS X application bundle directory.
char *mp_get_macosx_bundle_dir(void *talloc_ctx);