[Asterisk-code-review] datastore: Add automatic module references. (asterisk[master])

Joshua Colp asteriskteam at digium.com
Thu Jan 4 06:53:28 CST 2018


Joshua Colp has submitted this change and it was merged. ( https://gerrit.asterisk.org/7769 )

Change subject: datastore: Add automatic module references.
......................................................................

datastore: Add automatic module references.

Add a reference to the calling module when it is active to protect
access to datastore->info.  Remove module references done by
func_periodic_hook as the datastore now handles it.

ASTERISK-25128 #close

Change-Id: I8357a3711e77591d0d1dd8ab4211a7eedd782c89
---
M funcs/func_periodic_hook.c
M include/asterisk/datastore.h
M main/datastore.c
3 files changed, 17 insertions(+), 8 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
  Kevin Harwell: Looks good to me, approved



diff --git a/funcs/func_periodic_hook.c b/funcs/func_periodic_hook.c
index 0ab3d6b..958da97 100644
--- a/funcs/func_periodic_hook.c
+++ b/funcs/func_periodic_hook.c
@@ -137,8 +137,6 @@
 	ast_free(state->context);
 	ast_free(state->exten);
 	ast_free(state);
-
-	ast_module_unref(ast_module_info->self);
 }
 
 static const struct ast_datastore_info hook_datastore = {
@@ -305,7 +303,7 @@
 	if (!(datastore = ast_datastore_alloc(&hook_datastore, uid))) {
 		return -1;
 	}
-	ast_module_ref(ast_module_info->self);
+
 	if (!(state = hook_state_alloc(context, exten, interval, hook_id))) {
 		ast_datastore_free(datastore);
 		return -1;
diff --git a/include/asterisk/datastore.h b/include/asterisk/datastore.h
index 85e9c68..8d6516b 100644
--- a/include/asterisk/datastore.h
+++ b/include/asterisk/datastore.h
@@ -69,6 +69,7 @@
 	const char *uid;			/*!< Unique data store identifier */
 	void *data;				/*!< Contained data */
 	const struct ast_datastore_info *info;	/*!< Data store type information */
+	struct ast_module *mod;			/*!< Module referenced by this datastore */
 	unsigned int inheritance;		/*!< Number of levels this item will continue to be inherited */
 	AST_LIST_ENTRY(ast_datastore) entry; 	/*!< Used for easy linking */
 };
@@ -77,13 +78,16 @@
  * \brief Create a data store object
  * \param[in] info information describing the data store object
  * \param[in] uid unique identifer
+ * \param[in] mod The module to hold until this datastore is freed.
  * \param file, line, function
  * \version 1.6.1 moved here and renamed from ast_channel_datastore_alloc
  */
-struct ast_datastore * attribute_malloc __ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
-							      const char *file, int line, const char *function);
+struct ast_datastore * attribute_malloc __ast_datastore_alloc(
+	const struct ast_datastore_info *info, const char *uid, struct ast_module *mod,
+	const char *file, int line, const char *function);
 
-#define ast_datastore_alloc(info, uid) __ast_datastore_alloc(info, uid, __FILE__, __LINE__, __PRETTY_FUNCTION__)
+#define ast_datastore_alloc(info, uid) \
+	__ast_datastore_alloc(info, uid, AST_MODULE_SELF, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
 /*!
  * \brief Free a data store object
diff --git a/main/datastore.c b/main/datastore.c
index 1170e24..a12bbdf 100644
--- a/main/datastore.c
+++ b/main/datastore.c
@@ -31,12 +31,14 @@
 #include "asterisk/utils.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/uuid.h"
+#include "asterisk/module.h"
 
 /*! \brief Number of buckets for datastore container */
 #define DATASTORE_BUCKETS 53
 
-struct ast_datastore *__ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
-					    const char *file, int line, const char *function)
+struct ast_datastore *__ast_datastore_alloc(
+	const struct ast_datastore_info *info, const char *uid, struct ast_module *mod,
+	const char *file, int line, const char *function)
 {
 	struct ast_datastore *datastore = NULL;
 
@@ -50,11 +52,14 @@
 	}
 
 	datastore->info = info;
+	datastore->mod = mod;
 
 	if (!ast_strlen_zero(uid) && !(datastore->uid = ast_strdup(uid))) {
 		ast_free(datastore);
 		datastore = NULL;
 	}
+
+	ast_module_ref(mod);
 
 	return datastore;
 }
@@ -75,6 +80,8 @@
 		datastore->uid = NULL;
 	}
 
+	ast_module_unref(datastore->mod);
+
 	/* Finally free memory used by ourselves */
 	ast_free(datastore);
 

-- 
To view, visit https://gerrit.asterisk.org/7769
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I8357a3711e77591d0d1dd8ab4211a7eedd782c89
Gerrit-Change-Number: 7769
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180104/b50ba0fb/attachment.html>


More information about the asterisk-code-review mailing list