From bfc3dbae88dc70a832e82c5b44474fdf5a5e65a5 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 18 Sep 2012 16:08:17 +0200 Subject: options: simplify somewhat by introducing a union for option values The m_option_value union is supposed to contain a field for each possible option type (as long as it actually stores data). This helps avoiding silly temporary memory alocations. Using a pointer to an union and to a field of the union interchangeably should be allowed by standard C. --- m_property.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'm_property.c') diff --git a/m_property.c b/m_property.c index e75cbe43ac..ac68d86163 100644 --- a/m_property.c +++ b/m_property.c @@ -69,7 +69,7 @@ int m_property_do(const m_option_t *prop_list, const char *name, int action, void *arg, void *ctx) { const m_option_t *opt; - void *val; + union m_option_value val = {0}; int r; switch (action) { @@ -85,15 +85,11 @@ int m_property_do(const m_option_t *prop_list, const char *name, if ((r = do_action(prop_list, name, M_PROPERTY_GET_TYPE, &opt, ctx)) <= 0) return r; - val = calloc(1, opt->type->size); - if ((r = do_action(prop_list, name, M_PROPERTY_GET, val, ctx)) <= 0) { - free(val); + if ((r = do_action(prop_list, name, M_PROPERTY_GET, &val, ctx)) <= 0) return r; - } if (!arg) return M_PROPERTY_ERROR; - char *str = m_option_print(opt, val); - free(val); + char *str = m_option_print(opt, &val); *(char **)arg = str; return str != NULL; case M_PROPERTY_PARSE: @@ -107,14 +103,10 @@ int m_property_do(const m_option_t *prop_list, const char *name, return r; if (!arg) return M_PROPERTY_ERROR; - val = calloc(1, opt->type->size); - if ((r = m_option_parse(opt, bstr0(opt->name), bstr0(arg), val)) <= 0) { - free(val); + if ((r = m_option_parse(opt, bstr0(opt->name), bstr0(arg), &val)) <= 0) return r; - } - r = do_action(prop_list, name, M_PROPERTY_SET, val, ctx); - m_option_free(opt, val); - free(val); + r = do_action(prop_list, name, M_PROPERTY_SET, &val, ctx); + m_option_free(opt, &val); return r; } return do_action(prop_list, name, action, arg, ctx); -- cgit v1.2.3