m_config: allow not allocating option struct, and use it

In cases we're just listing options or checking their values (as it
happens with -vo/-vf etc. suboption parsing), we don't need to allocate
abd initialize the actual option struct. All we're interested in is
the list of options.
This commit is contained in:
wm4 2013-10-25 15:31:47 +02:00
parent cb3327fe48
commit 75e1c6f295
3 changed files with 23 additions and 8 deletions

View File

@ -198,13 +198,14 @@ struct m_config *m_config_new(void *talloc_parent, size_t size,
.optstruct_defaults = defaults,
.options = options,
};
if (size) { // size==0 means a dummy object is created
// size==0 means a dummy object is created
if (size) {
config->optstruct = talloc_zero_size(config, size);
if (defaults)
memcpy(config->optstruct, defaults, size);
if (options)
add_options(config, "", config->optstruct, defaults, options);
}
if (options)
add_options(config, "", config->optstruct, defaults, options);
return config;
}
@ -215,6 +216,13 @@ struct m_config *m_config_from_obj_desc(void *talloc_parent,
desc->options);
}
// Like m_config_from_obj_desc(), but don't allocate option struct.
struct m_config *m_config_from_obj_desc_noalloc(void *talloc_parent,
struct m_obj_desc *desc)
{
return m_config_new(talloc_parent, 0, desc->priv_defaults, desc->options);
}
int m_config_set_obj_params(struct m_config *conf, char **args)
{
for (int n = 0; args && args[n * 2 + 0]; n++) {
@ -390,7 +398,7 @@ static void m_config_add_option(struct m_config *config,
if (arg->defval)
co.default_data = arg->defval;
if (co.data && !co.default_data)
if (!co.default_data)
co.default_data = &default_value;
// Fill in the full name
@ -405,8 +413,11 @@ static void m_config_add_option(struct m_config *config,
if (arg->type->flags & M_OPT_TYPE_USE_SUBSTRUCT) {
const struct m_sub_options *subopts = arg->priv;
void *new_optstruct = m_config_alloc_struct(config, subopts);
substruct_write_ptr(co.data, new_optstruct);
void *new_optstruct = NULL;
if (co.data) {
new_optstruct = m_config_alloc_struct(config, subopts);
substruct_write_ptr(co.data, new_optstruct);
}
const void *new_optstruct_def = substruct_read_ptr(co.default_data);
if (!new_optstruct_def)

View File

@ -73,6 +73,7 @@ typedef struct m_config {
// Create a new config object.
// talloc_parent: talloc parent context for the m_config allocation
// size: size of the optstruct (where option values are stored)
// size==0 creates a config object with no option struct allocated
// defaults: if not NULL, points to a struct of same type as optstruct, which
// contains default values for all options
// options: list of options. Each option defines a member of the optstruct
@ -86,6 +87,9 @@ struct m_config *m_config_new(void *talloc_parent, size_t size,
struct m_config *m_config_from_obj_desc(void *talloc_parent,
struct m_obj_desc *desc);
struct m_config *m_config_from_obj_desc_noalloc(void *talloc_parent,
struct m_obj_desc *desc);
int m_config_set_obj_params(struct m_config *conf, char **args);
// Initialize an object (VO/VF/...) in one go, including legacy handling.

View File

@ -2140,7 +2140,7 @@ static int parse_obj_settings(struct bstr opt, struct bstr *pstr,
}
if (_ret && desc.init_options) {
struct m_config *config = m_config_from_obj_desc(NULL, &desc);
struct m_config *config = m_config_from_obj_desc_noalloc(NULL, &desc);
bstr s = bstr0(desc.init_options);
m_obj_parse_sub_config(opt, str, &s, config,
M_SETOPT_CHECK_ONLY, &plist);
@ -2169,7 +2169,7 @@ static int parse_obj_settings(struct bstr opt, struct bstr *pstr,
} else {
struct m_config *config = NULL;
if (!skip)
config = m_config_from_obj_desc(NULL, &desc);
config = m_config_from_obj_desc_noalloc(NULL, &desc);
r = m_obj_parse_sub_config(opt, str, pstr, config,
M_SETOPT_CHECK_ONLY,
_ret ? &plist : NULL);