[asterisk-commits] eliel: branch group/data_api_gsoc2009 r201895 - in /team/group/data_api_gsoc2...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jun 19 08:41:03 CDT 2009


Author: eliel
Date: Fri Jun 19 08:40:59 2009
New Revision: 201895

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=201895
Log:
Move the code to 90 columns.
Add two needed functions (ast_data_create and ast_data_free)

Modified:
    team/group/data_api_gsoc2009/include/asterisk/data.h
    team/group/data_api_gsoc2009/main/data.c

Modified: team/group/data_api_gsoc2009/include/asterisk/data.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/include/asterisk/data.h?view=diff&rev=201895&r1=201894&r2=201895
==============================================================================
--- team/group/data_api_gsoc2009/include/asterisk/data.h (original)
+++ team/group/data_api_gsoc2009/include/asterisk/data.h Fri Jun 19 08:40:59 2009
@@ -208,11 +208,19 @@
 void ast_data_free(struct ast_data *res);
 
 /*!
+ * \brief Allocate a 'data' node structure.
+ * \param[in] name The root node name.
+ * \retval NULL on error.
+ * \retval non-NULL The allocated structure.
+ */
+struct ast_data *ast_data_create(const char *name);
+
+/*!
  * \brief Initialize an iterator.
  * \param[in] tree The returned tree by a call to ast_data_get.
  * \param[in] elements Which elements to iterate through.
  * \retval NULL on error.
- * \retval A dinamically allocated iterator structure.
+ * \retval non-NULL A dinamically allocated iterator structure.
  */
 struct ast_data_iterator *ast_data_iterator_init(struct ast_data *tree, const char *elements);
 
@@ -227,7 +235,7 @@
  * \brief Get the next node of the tree.
  * \param[in] iterator The iterator structure returned by ast_data_iterator_start.
  * \retval NULL when no more nodes to return.
- * \retval A node of the ast_data tree.
+ * \retval non-NULL A node of the ast_data tree.
  * \see ast_data_iterator_start, ast_data_iterator_stop
  */
 struct ast_data *ast_data_iterator_next(struct ast_data_iterator *iterator);

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=201895&r1=201894&r2=201895
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Fri Jun 19 08:40:59 2009
@@ -36,13 +36,17 @@
 /*! \brief The data tree to be returned by the callbacks and 
            managed by functions local to this file. */
 struct ast_data {
+	/*! \brief The type of the node content. */
 	enum ast_data_type type;
+	/*! \brief The node content. */
 	union {
 		unsigned int boolean:1;
 		int integer;
 		char *string;
 	} payload;
+	/*! \brief The list of nodes inside this node. */
 	struct ao2_container *children;
+	/*! \brief The name of the node. */
 	char name[0];
 };
 
@@ -151,7 +155,8 @@
  * \retval NULL on error.
  * \retval The allocated data node structure.
  */
-static struct data_node_provider *data_node_provider_new(const char *name, const struct ast_data_handler *handler, const char *registrar)
+static struct data_node_provider *data_node_provider_new(const char *name,
+	const struct ast_data_handler *handler, const char *registrar)
 {
 	struct data_node_provider *node;
 	size_t namelen;
@@ -212,7 +217,8 @@
  * \retval The node found.
  * \note Remember to decrement the ref count of the returned node after using it.
  */
-static struct data_node_provider *data_find_child(struct ao2_container *parent, const char *name, const char *registrar)
+static struct data_node_provider *data_find_child(struct ao2_container *parent,
+	const char *name, const char *registrar)
 {
 	struct data_node_provider *find_node, *found;
 
@@ -230,8 +236,9 @@
 	if (found && found->registrar && registrar) {
 		if (strcmp(found->registrar, registrar)) {
 			/* if the name doesn't match, do not return this node. */
-			ast_log(LOG_DEBUG, "Registrar doesn't match, node was registered by '%s' "
-				"and we are searching for '%s'\n", found->registrar, registrar);
+			ast_log(LOG_DEBUG, "Registrar doesn't match, node was registered"
+				" by '%s' and we are searching for '%s'\n",
+				found->registrar, registrar);
 			ao2_ref(found, -1);
 			return NULL;
 		}
@@ -250,7 +257,8 @@
  * \retval 0 on success.
  * \see data_node_provider_create
  */
-static int data_node_provider_release(struct ao2_container *parent, const char *path, const char *registrar)
+static int data_node_provider_release(struct ao2_container *parent, const char *path,
+	const char *registrar)
 {
 	char *node_name, *rpath;
 	struct data_node_provider *child;
@@ -293,7 +301,8 @@
  * \param[in] registrar
  * \see __ast_data_unregister
  */
-static void data_node_provider_release_all(struct ao2_container *parent, const char *registrar)
+static void data_node_provider_release_all(struct ao2_container *parent,
+	const char *registrar)
 {
 	struct ao2_iterator i;
 	struct data_node_provider *node;
@@ -328,7 +337,8 @@
  * \retval The created node.
  * \see data_node_provider_release
  */
-static struct data_node_provider *data_node_provider_create(struct ao2_container *parent, const char *path, const char *registrar)
+static struct data_node_provider *data_node_provider_create(struct ao2_container *parent,
+	const char *path, const char *registrar)
 {
 	char *rpath, *node_name;
 	struct data_node_provider *child, *ret = NULL;
@@ -361,7 +371,8 @@
 	return ret ? ret : child;
 }
 
-int __ast_data_register(const char *path, const struct ast_data_handler *handler, const char *registrar)
+int __ast_data_register(const char *path, const struct ast_data_handler *handler,
+	const char *registrar)
 {
 	struct data_node_provider *node;
 
@@ -374,14 +385,16 @@
 
 	node = data_node_provider_create(root_data, path, registrar);
 	if (!node) {
-		ast_log(LOG_ERROR, "Unable to create the specified path (%s) for '%s'.\n", path, registrar);
+		ast_log(LOG_ERROR, "Unable to create the specified path (%s) "
+			"for '%s'.\n", path, registrar);
 		data_unlock();
 		return -1;
 	}
 
 	if (ao2_container_count(node->children) || node->handler) {
 		ast_log(LOG_ERROR, "The node '%s' was already registered. "
-			"We were unable to register '%s' for registrar '%s'.\n", node->name, path, registrar);
+			"We were unable to register '%s' for registrar '%s'.\n",
+			node->name, path, registrar);
 		ao2_ref(node, -1);
 		data_unlock();
 		return -1;
@@ -397,12 +410,14 @@
 	return 0;
 }
 
-int __ast_data_register_multiple(const struct ast_data_entry *data_entries, size_t entries, const char *registrar)
+int __ast_data_register_multiple(const struct ast_data_entry *data_entries,
+	size_t entries, const char *registrar)
 {
 	int i, res;
 
 	for (i = 0; i < entries; i++) {
-		res = __ast_data_register(data_entries[i].path, data_entries[i].handler, registrar);
+		res = __ast_data_register(data_entries[i].path, data_entries[i].handler,
+				registrar);
 		if (res) {
 			/* unregister all the already registered nodes, and make this action atomic. */
 			while ((--i) >= 0) {
@@ -428,15 +443,64 @@
 	data_unlock();
 
 	if (path && ret) {
-		ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n", path, registrar);
+		ast_log(LOG_ERROR, "Unable to unregister '%s' for '%s'\n",
+			path, registrar);
 	}
 
 	return ret;
 }
 
-void ast_data_free(struct ast_data *res)
-{
-	/* TODO: implement this. */
+/*!
+ * \internal
+ * \brief Release the memory allocated by a call to ao2_alloc.
+ */
+static void data_node_destructor(void *obj)
+{
+	struct ast_data *node = obj;
+
+	if (node->type == AST_DATA_STRING) {
+		/* If there is a string in the node, release the
+		 * used memory to store it. */
+		ast_free(node->payload.string);
+	}
+}
+
+struct ast_data *ast_data_create(const char *name)
+{
+	struct ast_data *res;
+	size_t namelen;
+
+	if (!name) {
+		ast_log(LOG_ERROR, "You need to pass a root node name in order to "
+			"create a data structure\n");
+		return NULL;
+	}
+
+	namelen = strlen(name) + 1;
+
+	res = ao2_alloc(sizeof(*res) + namelen, data_node_destructor);
+	if (!res) {
+		ast_log(LOG_ERROR, "Unable to allocate a node structure "
+			"named '%s'.\n", name);
+		return NULL;
+	}
+
+	return res;
+}
+
+void ast_data_free(struct ast_data *root)
+{
+	struct ao2_iterator i;
+	struct ast_data *node;
+
+	/* Free internal nodes too. */
+	i = ao2_iterator_init(root->children, 0);
+	while ((node = ao2_iterator_next(&i))) {
+		ast_data_free(node);
+		ao2_unlink(root->children, node);
+	}
+
+	ao2_ref(root, -1);
 }
 
 struct ast_data *ast_data_get(const struct ast_data_query *query)




More information about the asterisk-commits mailing list