From b4d64ac43432f99ff20357226c8b41a2e71583f7 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 26 Jun 2014 19:13:37 +0200 Subject: [PATCH] config, player: avoid some temporary talloc contexts IMO a semi-bad concept, that the mpv code unfortunately uses way too much. --- options/path.c | 29 +++++++++++++++-------------- player/configfiles.c | 25 ++++++++++--------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/options/path.c b/options/path.c index eaae35154f..0c3a7ba335 100644 --- a/options/path.c +++ b/options/path.c @@ -136,20 +136,21 @@ char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global, { struct MPOpts *opts = global->opts; - void *tmp = talloc_new(NULL); char *res = NULL; if (opts->load_config) { - for (char **dir = mp_config_dirs(tmp, global); *dir; dir++) { - char *config_file = talloc_asprintf(tmp, "%s/%s", *dir, filename); + char **dirs = mp_config_dirs(NULL, global); + for (int i = 0; dirs && dirs[i]; i++) { + char *file = talloc_asprintf(talloc_ctx, "%s/%s", dirs[i], filename); - if (mp_path_exists(config_file)) { - res = talloc_strdup(talloc_ctx, config_file); + if (mp_path_exists(file)) { + res = file; break; } - } - } - talloc_free(tmp); + talloc_free(file); + } + talloc_free(dirs); + } MP_VERBOSE(global, "config path: '%s' -> '%s'\n", filename, res ? res : "(NULL)"); @@ -165,15 +166,16 @@ char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global, int num_ret = 0; if (opts->load_config) { - 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); + char **dirs = mp_config_dirs(NULL, global); + for (int i = 0; dirs && dirs[i]; i++) { + char *file = talloc_asprintf(ret, "%s/%s", dirs[i], filename); if (!mp_path_exists(file) || num_ret >= MAX_CONFIG_PATHS) continue; ret[num_ret++] = file; } + talloc_free(dirs); } for (int n = 0; n < num_ret / 2; n++) @@ -331,8 +333,7 @@ bstr mp_split_proto(bstr path, bstr *out_url) void mp_mkdirp(const char *dir) { - void *tmp = talloc_new(NULL); - char *path = talloc_strdup(tmp, dir); + char *path = talloc_strdup(NULL, dir); char *cdir = path + 1; while (cdir) { @@ -346,7 +347,7 @@ void mp_mkdirp(const char *dir) *cdir++ = '/'; } - talloc_free(tmp); + talloc_free(path); } void mp_mk_config_dir(struct mpv_global *global, char *subdir) diff --git a/player/configfiles.c b/player/configfiles.c index 855ea64fd1..dab26b9df2 100644 --- a/player/configfiles.c +++ b/player/configfiles.c @@ -49,11 +49,10 @@ static void load_all_cfgfiles(struct MPContext *mpctx, char *section, char *filename) { - void *tmp = talloc_new(NULL); - char **cf = mp_find_all_config_files(tmp, mpctx->global, filename); + char **cf = mp_find_all_config_files(NULL, mpctx->global, filename); for (int i = 0; cf && cf[i]; i++) m_config_parse_config_file(mpctx->mconfig, cf[i], section, 0); - talloc_free(tmp); + talloc_free(cf); } #define SECT_ENCODE "encoding" @@ -67,8 +66,6 @@ void mp_parse_cfgfiles(struct MPContext *mpctx) mp_mk_config_dir(mpctx->global, ""); m_config_t *conf = mpctx->mconfig; - void *tmp = talloc_new(NULL); - char *conffile; char *section = NULL; bool encoding = opts->encode_opts && opts->encode_opts->file && opts->encode_opts->file[0]; @@ -81,9 +78,10 @@ void mp_parse_cfgfiles(struct MPContext *mpctx) // The #if is a stupid hack to avoid errors if libavfilter is not available. #if HAVE_LIBAVFILTER && HAVE_ENCODING - conffile = mp_find_config_file(tmp, mpctx->global, "encoding-profiles.conf"); - if (conffile) - m_config_parse_config_file(mpctx->mconfig, conffile, SECT_ENCODE, 0); + char *cf = mp_find_config_file(NULL, mpctx->global, "encoding-profiles.conf"); + if (cf) + m_config_parse_config_file(mpctx->mconfig, cf, SECT_ENCODE, 0); + talloc_free(cf); #endif load_all_cfgfiles(mpctx, section, "config"); @@ -91,8 +89,6 @@ void mp_parse_cfgfiles(struct MPContext *mpctx) if (encoding) m_config_set_profile(conf, m_config_add_profile(conf, SECT_ENCODE), 0); - - talloc_free(tmp); } static int try_load_config(struct MPContext *mpctx, const char *file, int flags) @@ -279,8 +275,8 @@ static bool needs_config_quoting(const char *s) void mp_write_watch_later_conf(struct MPContext *mpctx) { - void *tmp = talloc_new(NULL); char *filename = mpctx->filename; + char *conffile = NULL; if (!filename) goto exit; @@ -290,9 +286,8 @@ void mp_write_watch_later_conf(struct MPContext *mpctx) mp_mk_config_dir(mpctx->global, MP_WATCH_LATER_CONF); - char *conffile = mp_get_playback_resume_config_filename(mpctx->global, - mpctx->filename); - talloc_steal(tmp, conffile); + conffile = mp_get_playback_resume_config_filename(mpctx->global, + mpctx->filename); if (!conffile) goto exit; @@ -327,7 +322,7 @@ void mp_write_watch_later_conf(struct MPContext *mpctx) fclose(file); exit: - talloc_free(tmp); + talloc_free(conffile); } void mp_load_playback_resume(struct MPContext *mpctx, const char *file)