[asterisk-commits] dlee: branch dlee/stasis-app r382615 - in /team/dlee/stasis-app: apps/ includ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Mar 7 11:39:31 CST 2013


Author: dlee
Date: Thu Mar  7 11:39:28 2013
New Revision: 382615

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382615
Log:
Addressed reveiw feedback.

 * Simplified app and control allocations
 * Preferred ast_str_hash to ast_hashtab_hash_string
 * Fixed a memory leak

Modified:
    team/dlee/stasis-app/apps/app_stasis.c
    team/dlee/stasis-app/include/asterisk/app_stasis.h
    team/dlee/stasis-app/res/res_stasis_websocket.c

Modified: team/dlee/stasis-app/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/app_stasis.c?view=diff&rev=382615&r1=382614&r2=382615
==============================================================================
--- team/dlee/stasis-app/apps/app_stasis.c (original)
+++ team/dlee/stasis-app/apps/app_stasis.c Thu Mar  7 11:39:28 2013
@@ -36,9 +36,9 @@
 #include "asterisk/app_stasis.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/channel.h"
-#include "asterisk/hashtab.h"
 #include "asterisk/module.h"
 #include "asterisk/stasis.h"
+#include "asterisk/strings.h"
 
 /*** DOCUMENTATION
 	<application name="Stasis" language="en_US">
@@ -99,43 +99,34 @@
 }
 
 struct stasis_app {
-	/*! Name of the Stasis application */
-	char *name;
 	/*! Callback function for this application. */
 	stasis_app_cb handler;
 	/*! Opaque data to hand to callback function. */
 	void *data;
+	/*! Name of the Stasis application */
+	char name[];
 };
-
-/*! Destructor for \ref stasis_app. */
-static void app_dtor(void *obj)
-{
-	struct stasis_app *app = obj;
-	ast_free(app->name);
-}
 
 /*! Constructor for \ref stasis_app. */
 static struct stasis_app *app_create(const char *name, stasis_app_cb handler, void *data)
 {
-	RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
+	struct stasis_app *app;
+	size_t size;
 
 	ast_assert(name != NULL);
 	ast_assert(handler != NULL);
 
-	app = ao2_alloc_options(sizeof(*app), app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
+	size = sizeof(*app) + strlen(name) + 1;
+	app = ao2_alloc_options(size, NULL, AO2_ALLOC_OPT_LOCK_MUTEX);
 
 	if (!app) {
 		return NULL;
 	}
 
-	if (!(app->name = ast_strdup(name))) {
-		return NULL;
-	}
-
+	strncpy(app->name, name, size - sizeof(*app));
 	app->handler = handler;
 	app->data = data;
 
-	ao2_ref(app, +1);
 	return app;
 }
 
@@ -145,7 +136,7 @@
 	const struct stasis_app *app = obj;
 	const char *name = flags & OBJ_KEY ? obj : app->name;
 
-	return ast_hashtab_hash_string(name);
+	return ast_str_hash(name);
 }
 
 /*! AO2 comparison function for \ref stasis_app */
@@ -173,35 +164,27 @@
 }
 
 struct stasis_app_control {
-	/*! Uniqueid of the associated channel */
-	char *channel_uniqueid;
 	/*!
 	 * When set, /c app_stasis should exit and continue in the dialplan.
 	 */
 	int continue_to_dialplan:1;
+	/*! Uniqueid of the associated channel */
+	char channel_uniqueid[];
 };
 
-static void app_control_dtor(void *obj)
-{
-	struct stasis_app_control *control = obj;
-	ast_free(control->channel_uniqueid);
-}
-
 static struct stasis_app_control *control_create(const char *uniqueid)
 {
-	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
-
-	control = ao2_alloc(sizeof(*control), app_control_dtor);
+	struct stasis_app_control *control;
+	size_t size;
+
+	size = sizeof(*control) + strlen(uniqueid) + 1;
+	control = ao2_alloc(size, NULL);
 	if (!control) {
 		return NULL;
 	}
 
-	control->channel_uniqueid = ast_strdup(uniqueid);
-	if (!control->channel_uniqueid) {
-		return NULL;
-	}
-
-	ao2_ref(control, +1);
+	strncpy(control->channel_uniqueid, uniqueid, size - sizeof(*control));
+
 	return control;
 }
 
@@ -241,7 +224,9 @@
 	control->continue_to_dialplan = 1;
 }
 
-struct ast_json *stasis_app_event_create(const char *event_name, const struct ast_channel_snapshot *channel_info, const struct ast_json *extra_info) {
+struct ast_json *stasis_app_event_create(const char *event_name,
+					 const struct ast_channel_snapshot *channel_info,
+					 const struct ast_json *extra_info) {
 	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
 	int r;
 
@@ -356,11 +341,13 @@
 {
 	RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
 
-	if (control) {
-		controls = app_controls();
-		ao2_unlink_flags(controls, control, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
-		ao2_cleanup(control);
-	}
+	if (!control) {
+		return;
+	}
+
+	controls = app_controls();
+	ao2_unlink_flags(controls, control, OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
+	ao2_cleanup(control);
 }
 
 /*! /brief Stasis dialplan application callback */

Modified: team/dlee/stasis-app/include/asterisk/app_stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/app_stasis.h?view=diff&rev=382615&r1=382614&r2=382615
==============================================================================
--- team/dlee/stasis-app/include/asterisk/app_stasis.h (original)
+++ team/dlee/stasis-app/include/asterisk/app_stasis.h Thu Mar  7 11:39:28 2013
@@ -55,7 +55,7 @@
  * \brief Callback for Stasis application handler.
  *
  * The message given to the handler is a borrowed copy. If you want to keep a
- * reference to it, you should use \c ast_ref() to keep it around.
+ * reference to it, you should use \c ao2_ref() to keep it around.
  *
  * \param data Data ptr given when registered.
  * \param app_name Name of the application being dispatched to.
@@ -85,7 +85,7 @@
  * \brief Send a message to the given Stasis application.
  *
  * The message given to the handler is a borrowed copy. If you want to keep a
- * reference to it, you should use \c ast_ref() to keep it around.
+ * reference to it, you should use \c ao2_ref() to keep it around.
  *
  * \param app_name Name of the application to invoke.
  * \param message Message to send (borrowed reference)

Modified: team/dlee/stasis-app/res/res_stasis_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/res/res_stasis_websocket.c?view=diff&rev=382615&r1=382614&r2=382615
==============================================================================
--- team/dlee/stasis-app/res/res_stasis_websocket.c (original)
+++ team/dlee/stasis-app/res/res_stasis_websocket.c Thu Mar  7 11:39:28 2013
@@ -36,10 +36,10 @@
 
 #include "asterisk/app_stasis.h"
 #include "asterisk/astobj2.h"
-#include "asterisk/hashtab.h"
 #include "asterisk/http_websocket.h"
 #include "asterisk/json.h"
 #include "asterisk/module.h"
+#include "asterisk/strings.h"
 #include "asterisk/utils.h"
 
 /*! WebSocket protocol for Stasis */
@@ -109,7 +109,7 @@
 	const struct stasis_app *app = obj;
 	const char *name = flags & OBJ_KEY ? obj : app->name;
 
-	return ast_hashtab_hash_string(name);
+	return ast_str_hash(name);
 }
 
 /*! Comparison function for stasis_app */
@@ -273,6 +273,7 @@
 	i = ao2_iterator_init(stasis_session.stasis_apps, 0);
 	while ((app = ao2_iterator_next(&i))) {
 		stasis_app_unregister(app->name);
+		ao2_cleanup(app);
 	}
 	ao2_cleanup(stasis_session.stasis_apps);
 




More information about the asterisk-commits mailing list