[svn-commits] twilson: branch twilson/config_work r361903 - in /team/twilson/config_work: a...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Apr 10 18:16:18 CDT 2012


Author: twilson
Date: Tue Apr 10 18:16:14 2012
New Revision: 361903

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=361903
Log:
A little cleanup

Modified:
    team/twilson/config_work/apps/app_skel.c
    team/twilson/config_work/include/asterisk/astobj2.h
    team/twilson/config_work/include/asterisk/config_options.h
    team/twilson/config_work/main/astobj2.c
    team/twilson/config_work/main/config_options.c

Modified: team/twilson/config_work/apps/app_skel.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/apps/app_skel.c?view=diff&rev=361903&r1=361902&r2=361903
==============================================================================
--- team/twilson/config_work/apps/app_skel.c (original)
+++ team/twilson/config_work/apps/app_skel.c Tue Apr 10 18:16:14 2012
@@ -189,13 +189,6 @@
 	return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
 }
 
-static void ao2_cleanup(void *obj)
-{
-	if (obj) {
-		ao2_ref(obj, -1);
-	}
-}
-
 static void apply_config(struct skel_global_config *new_global, struct ao2_container *new_pvt_container, struct ao2_container *new_cfg_container)
 {
 	void *old;

Modified: team/twilson/config_work/include/asterisk/astobj2.h
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/include/asterisk/astobj2.h?view=diff&rev=361903&r1=361902&r2=361903
==============================================================================
--- team/twilson/config_work/include/asterisk/astobj2.h (original)
+++ team/twilson/config_work/include/asterisk/astobj2.h Tue Apr 10 18:16:14 2012
@@ -1458,4 +1458,10 @@
 /* extra functions */
 void ao2_bt(void);	/* backtrace */
 
+/*! gcc __attribute__(cleanup()) functions
+ * \note they must be able to handle NULL parameters because most of the
+ * allocation/find functions can fail and we don't want to try to tear
+ * down a NULL */
+void ao2_cleanup(void *obj);
+void ao2_iterator_cleanup(struct ao2_iterator *iter);
 #endif /* _ASTERISK_ASTOBJ2_H */

Modified: team/twilson/config_work/include/asterisk/config_options.h
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/include/asterisk/config_options.h?view=diff&rev=361903&r1=361902&r2=361903
==============================================================================
--- team/twilson/config_work/include/asterisk/config_options.h (original)
+++ team/twilson/config_work/include/asterisk/config_options.h Tue Apr 10 18:16:14 2012
@@ -1,5 +1,32 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * Mark Spencer <markster at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief Configuration Option-handling
+ * \author Terry Wilson <twilson at digium.com>
+ */
+
 #ifndef _ASTERISK_CONFIG_OPTIONS_H
 #define _ASTERISK_CONFIG_OPTIONS_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
 
 #include "asterisk/config.h"
 #include "asterisk/astobj2.h"
@@ -40,6 +67,25 @@
 int ast_config_parse_category_options(struct ao2_container *container, struct ast_config *cfg, const char *cat, void *obj);
 int ast_config_option_set_defaults(struct ao2_container *container, const char *context, void *obj);
 
+#define ast_config_option_register(container, name, op, context, default_val, opt_type, struct_type, flags, ...) {\
+	struct ast_config_option *__opt; \
+	__opt = opt_type == OPT_STRINGFIELD_T ? \
+		__ast_config_option_build(name, op, context, default_val, opt_type, NULL, flags, ARGMAP(offsetof, struct_type, __VA_ARGS__, __field_mgr_pool, __field_mgr)) : \
+		__ast_config_option_build(name, op, context, default_val, opt_type, NULL, flags, ARGMAP(offsetof, struct_type, __VA_ARGS__)); \
+	if (__opt) { \
+		ao2_link(container, __opt); \
+		ao2_ref(__opt, -1); \
+	} \
+}
+
+#define ast_config_option_register_custom(container, name, op, context, default_val, opt_type, handler, flags) { \
+	struct ast_config_option *__opt = ast_config_option_build(name, op, context, default_val, opt_type, handler, flags); \
+	if (__opt) { \
+		ao2_link(container, __opt); \
+		ao2_ref(__opt, -1); \
+	} \
+}
+
 #define PASTE(arg1, arg2)  PASTE1(arg1, arg2)
 #define PASTE1(arg1, arg2) arg1##arg2
 
@@ -70,23 +116,8 @@
 #define ARGMAP_(N, func, type, x, ...) PASTE(ARGMAP_, N)(func, type, N, x, __VA_ARGS__)
 #define ARGMAP(func, type, x, ...) ARGMAP_(VA_NARGS(x, ##__VA_ARGS__), func, type, x, __VA_ARGS__)
 
-#define ast_config_option_register(container, name, op, context, default_val, opt_type, struct_type, flags, ...) {\
-	struct ast_config_option *__opt; \
-	__opt = opt_type == OPT_STRINGFIELD_T ? \
-		__ast_config_option_build(name, op, context, default_val, opt_type, NULL, flags, ARGMAP(offsetof, struct_type, __VA_ARGS__, __field_mgr_pool, __field_mgr)) : \
-		__ast_config_option_build(name, op, context, default_val, opt_type, NULL, flags, ARGMAP(offsetof, struct_type, __VA_ARGS__)); \
-	if (__opt) { \
-		ao2_link(container, __opt); \
-		ao2_ref(__opt, -1); \
-	} \
+#if defined(__cplusplus) || defined(c_plusplus)
 }
-
-#define ast_config_option_register_custom(container, name, op, context, default_val, opt_type, handler, flags) { \
-	struct ast_config_option *__opt = ast_config_option_build(name, op, context, default_val, opt_type, handler, flags); \
-	if (__opt) { \
-		ao2_link(container, __opt); \
-		ao2_ref(__opt, -1); \
-	} \
-}
+#endif
 
 #endif /* _ASTERISK_CONFIG_OPTIONS_H */

Modified: team/twilson/config_work/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/main/astobj2.c?view=diff&rev=361903&r1=361902&r2=361903
==============================================================================
--- team/twilson/config_work/main/astobj2.c (original)
+++ team/twilson/config_work/main/astobj2.c Tue Apr 10 18:16:14 2012
@@ -1517,6 +1517,20 @@
 	return clone;
 }
 
+void ao2_cleanup(void *obj)
+{
+	if (obj) {
+		ao2_ref(obj, -1);
+	}
+}
+
+void ao2_iterator_cleanup(struct ao2_iterator *iter)
+{
+	if (iter) {
+		ao2_iterator_destroy(iter);
+	}
+}
+
 #ifdef AO2_DEBUG
 static int print_cb(void *obj, void *arg, int flag)
 {

Modified: team/twilson/config_work/main/config_options.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/config_work/main/config_options.c?view=diff&rev=361903&r1=361902&r2=361903
==============================================================================
--- team/twilson/config_work/main/config_options.c (original)
+++ team/twilson/config_work/main/config_options.c Tue Apr 10 18:16:14 2012
@@ -1,3 +1,26 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * Mark Spencer <markster at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief Configuration Option-handling
+ * \author Terry Wilson <twilson at digium.com>
+ */
+
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
@@ -9,6 +32,12 @@
 #include "asterisk/stringfields.h"
 #include "asterisk/acl.h"
 #include "asterisk/frame.h"
+
+#ifdef LOW_MEMORY
+#define CONFIG_OPT_BUCKETS 5
+#else
+#define CONFIG_OPT_BUCKETS 53
+#endif /* LOW_MEMORY */
 
 struct ast_config_option {
 	const char *name;
@@ -32,22 +61,25 @@
 	}
 }
 
-static int (*ast_config_option_default_handlers[])(const struct ast_config_option *opt, struct ast_variable *var, void *obj) = {
-	ast_config_option_int_fn,         /* OPT_INT_T */
-	ast_config_option_uint_fn,        /* OPT_UINT_T */
-	ast_config_option_double_fn,      /* OPT_DOUBLE_T */
-	ast_config_option_sockaddr_fn,    /* OPT_SOCKADDR_T */
-	ast_config_option_stringfield_fn, /* OPT_STRINGFIELD_T */
-	ast_config_option_bool_fn,        /* OPT_BOOL_T */
-	ast_config_option_acl_fn,         /* OPT_ACL_T */
-	ast_config_option_codec_fn,       /* OPT_CODEC_T */
-};
-
-/*
-struct ast_config_option *__ast_config_option_build(const char *name, enum ast_config_option_context_op context_allow,
-	const char *context, const char *default_val, enum ast_config_option_type type, size_t field_offset,
-	ast_config_option_handler handler, unsigned int flags, size_t field_mgr_ofst, size_t field_mgr_pool_ofst)
-*/
+static ast_config_option_handler ast_config_option_default_handler(enum ast_config_option_type type)
+{
+	switch(type) {
+	case OPT_ACL_T: return ast_config_option_acl_fn;
+	case OPT_BOOL_T: return ast_config_option_bool_fn;
+	case OPT_CODEC_T: return ast_config_option_codec_fn;
+	case OPT_DOUBLE_T: return ast_config_option_double_fn;
+	case OPT_INT_T: return ast_config_option_int_fn;
+	case OPT_SOCKADDR_T: return ast_config_option_sockaddr_fn;
+	case OPT_STRINGFIELD_T: return ast_config_option_stringfield_fn;
+	case OPT_UINT_T: return ast_config_option_uint_fn;
+	}
+
+	return NULL;
+}
+
+/*! \brief build a config option
+ * \note this should probably only be called by one of the ast_config_option_register* macros
+ */
 struct ast_config_option *__ast_config_option_build(const char *name, enum ast_config_option_context_op context_allow,
 	const char *context, const char *default_val, enum ast_config_option_type type, ast_config_option_handler handler, unsigned int flags, size_t argc, ...)
 {
@@ -86,7 +118,7 @@
 	opt->flags = flags;
 	opt->argc = argc;
 
-	if (!opt->handler && !(opt->handler = ast_config_option_default_handlers[opt->type])) {
+	if (!opt->handler && !(opt->handler = ast_config_option_default_handler(opt->type))) {
 		ao2_ref(opt, -1);
 		return NULL;
 	};
@@ -94,6 +126,12 @@
 	return opt;
 }
 
+/*! \brief Test if the recorded context of each option matches the generated regex of the other
+ * \param opt1 the first option
+ * \param opt2 the second option
+ * \retval 0 both options have regexes that match each other's recorded context setting
+ * \retval -1 the options have regexes that don't match each other's recorded context setting
+ */
 static int test_opt_contexts(struct ast_config_option *opt1, struct ast_config_option *opt2)
 {
 	return regexec(opt1->context_regex, opt2->context, 0, NULL, 0) ||
@@ -117,7 +155,7 @@
 {
 	const char *name = arg, *context = data;
 	struct ast_config_option *match = obj;
-	/* Continue if we don't match on name, or if NOT regex_matches XOR regex_should_match */
+	/* Continue if we don't match on name, or if NOT (regex_matches XOR regex_should_match) */
 	return strcasecmp(name, match->name) || !regexec(match->context_regex, context, 0, NULL, 0) == !match->context_allow ? 0 : CMP_MATCH | CMP_STOP; 
 }
 
@@ -126,7 +164,6 @@
 	return ao2_callback_data(container, OBJ_KEY, find_option_cb, (void *) name, (void *) cat);
 }
 
-#define CONFIG_OPT_BUCKETS 53
 struct ao2_container *ast_config_option_container_new(void)
 {
 	return ao2_container_alloc(CONFIG_OPT_BUCKETS, config_opt_hash, config_opt_cmp);
@@ -158,6 +195,9 @@
 	return 0;
 }
 
+
+/*! \brief match for anything where the context passes (or fails if !context_allow) the context regex
+ */
 static int match_option_by_context(void *obj, void *arg, int flags)
 {
 	struct ast_config_option *match = obj;
@@ -168,34 +208,34 @@
 
 int ast_config_option_set_defaults(struct ao2_container *container, const char *context, void *obj)
 {
-	struct ao2_iterator *iter;
+	RAII_VAR(struct ao2_iterator *, iter, NULL, ao2_iterator_cleanup);
 	struct ast_config_option *opt;
 
-	iter = ao2_callback(container, OBJ_MULTIPLE, match_option_by_context, (void *) context);
+	if (!(iter = ao2_callback(container, OBJ_MULTIPLE, match_option_by_context, (void *) context))) {
+		return -1;
+	}
+
 	while ((opt = ao2_iterator_next(iter))) {
-		struct ast_variable *var;
+		RAII_VAR(struct ast_variable *, var, NULL, ast_variables_destroy);
+
 		if (ast_strlen_zero(opt->default_val)) {
 			continue;
 		}
 		if (!(var = ast_variable_new(opt->name, opt->default_val, ""))) {
 			ao2_ref(opt, -1);
-			ao2_iterator_destroy(iter);
 			return -1;
 		}
 		if (opt->handler(opt, var, obj)) {
 			ao2_ref(opt, -1);
-			ao2_iterator_destroy(iter);
-			ast_variables_destroy(var);
-			return -1;
-		}
-		ast_variables_destroy(var);
-	}
-	ao2_iterator_destroy(iter);
-
-	return 0;
-}
-
-/* default option handlers */
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+/* default config option handlers */
+
 int ast_config_option_int_fn(const struct ast_config_option *opt, struct ast_variable *var, void *obj) {
 	int *field = (int *)(obj + opt->args[0]);
 	return ast_parse_arg(var->value, PARSE_INT32 | opt->flags, field);
@@ -205,10 +245,12 @@
 	unsigned int *field = (unsigned int *)(obj + opt->args[0]);
 	return ast_parse_arg(var->value, PARSE_UINT32 | opt->flags, field);
 }
+
 int ast_config_option_double_fn(const struct ast_config_option *opt, struct ast_variable *var, void *obj) {
 	double *field = (double *)(obj + opt->args[0]);
 	return ast_parse_arg(var->value, PARSE_DOUBLE | opt->flags, field);
 }
+
 int ast_config_option_acl_fn(const struct ast_config_option *opt, struct ast_variable *var, void *obj) {
 	struct ast_ha **ha = (struct ast_ha **)(obj + opt->args[0]);
 	int error = 0;
@@ -226,7 +268,7 @@
 /* opt->args[0] = ast_string_field,  opt->args[1] = field_mgr_pool, opt->args[2] = field_mgri */
 int ast_config_option_stringfield_fn(const struct ast_config_option *opt, struct ast_variable *var, void *obj)
 {
-	const char **field = (const char **)(obj + opt->args[0]);
+	ast_string_field *field = (const char **)(obj + opt->args[0]);
 	struct ast_string_field_pool **pool = (struct ast_string_field_pool **)(obj + opt->args[1]);
 	struct ast_string_field_mgr *mgr = (struct ast_string_field_mgr *)(obj + opt->args[2]);
 	ast_string_field_ptr_set_by_fields(*pool, *mgr, field, var->value);




More information about the svn-commits mailing list