[sr-dev] git:tirpi/cfg_framework_multivalue: cfg framework: free the memory of the old group instance

Miklos Tirpak miklos at iptel.org
Tue Sep 14 16:46:29 CEST 2010


Module: sip-router
Branch: tirpi/cfg_framework_multivalue
Commit: 27580846d9c01de2846601a9ef1bfac0210fecd6
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=27580846d9c01de2846601a9ef1bfac0210fecd6

Author: Miklos Tirpak <miklos at iptel.org>
Committer: Miklos Tirpak <miklos at iptel.org>
Date:   Tue Sep  7 17:37:20 2010 +0200

cfg framework: free the memory of the old group instance

The memory area of the old group instances is freed when the
additional array is replaced.

---

 cfg/cfg_ctx.c    |   27 +++++++++++++++++++--------
 cfg/cfg_struct.c |    2 +-
 cfg/cfg_struct.h |    5 +++--
 3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/cfg/cfg_ctx.c b/cfg/cfg_ctx.c
index 57e6670..cc64a0d 100644
--- a/cfg/cfg_ctx.c
+++ b/cfg/cfg_ctx.c
@@ -306,13 +306,14 @@ static int cfg_update_defaults(cfg_group_meta_t	*meta,
 int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
 			void *val, unsigned int val_type)
 {
+	int		i;
 	cfg_group_t	*group;
 	cfg_mapping_t	*var;
 	void		*p, *v;
 	cfg_block_t	*block = NULL;
 	str		s, s2;
 	char		*old_string = NULL;
-	char		**replaced = NULL;
+	void		**replaced = NULL;
 	cfg_child_cb_t	*child_cb = NULL;
 	cfg_group_inst_t	*group_inst, *new_array = NULL;
 	unsigned char		*var_block;
@@ -523,16 +524,26 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *va
 				new_array = CFG_GROUP_META(block, group)->array;
 		}
 
-		if (old_string) {
+		if (old_string || new_array) {
 			/* prepare the array of the replaced strings,
+			and replaced group instances,
 			they will be freed when the old block is freed */
-			replaced = (char **)shm_malloc(sizeof(char *)*2);
+			replaced = (void **)shm_malloc(sizeof(void *)
+					* ((old_string?1:0) + (new_array?1:0) + 1));
 			if (!replaced) {
 				LOG(L_ERR, "ERROR: cfg_set_now(): not enough shm memory\n");
 				goto error;
 			}
-			replaced[0] = old_string;
-			replaced[1] = NULL;
+			i = 0;
+			if (old_string) {
+				replaced[i] = old_string;
+				i++;
+			}
+			if (new_array) {	
+				replaced[i] = CFG_GROUP_META(*cfg_global, group)->array;
+				i++;
+			}
+			replaced[i] = NULL;
 		}
 		/* replace the global config with the new one */
 		if (block) cfg_install_global(block, replaced, child_cb, child_cb);
@@ -845,7 +856,7 @@ int cfg_commit(cfg_ctx_t *ctx)
 	int	replaced_num = 0;
 	cfg_changed_var_t	*changed, *changed2;
 	cfg_block_t	*block;
-	char	**replaced = NULL;
+	void	**replaced = NULL;
 	cfg_child_cb_t	*child_cb;
 	cfg_child_cb_t	*child_cb_first = NULL;
 	cfg_child_cb_t	*child_cb_last = NULL;
@@ -899,8 +910,8 @@ int cfg_commit(cfg_ctx_t *ctx)
 
 	if (replaced_num) {
 		/* allocate memory for the replaced string array */
-		size = sizeof(char *)*(replaced_num + 1);
-		replaced = (char **)shm_malloc(size);
+		size = sizeof(void *)*(replaced_num + 1);
+		replaced = (void **)shm_malloc(size);
 		if (!replaced) {
 			LOG(L_ERR, "ERROR: cfg_commit(): not enough shm memory\n");
 			goto error;
diff --git a/cfg/cfg_struct.c b/cfg/cfg_struct.c
index 13605ae..3e92080 100644
--- a/cfg/cfg_struct.c
+++ b/cfg/cfg_struct.c
@@ -645,7 +645,7 @@ void cfg_install_child_cb(cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last)
  * cb_first and cb_last define a linked list of per-child process
  * callbacks. This list is added to the global linked list.
  */
-void cfg_install_global(cfg_block_t *block, char **replaced,
+void cfg_install_global(cfg_block_t *block, void **replaced,
 			cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last)
 {
 	cfg_block_t* old_cfg;
diff --git a/cfg/cfg_struct.h b/cfg/cfg_struct.h
index 18ffbc7..58cd810 100644
--- a/cfg/cfg_struct.h
+++ b/cfg/cfg_struct.h
@@ -122,7 +122,8 @@ typedef struct _cfg_block {
 	atomic_t	refcnt;		/*!< reference counter,
 					the block is automatically deleted
 					when it reaches 0 */
-	char		**replaced;	/*!< set of the strings that must be freed
+	void		**replaced;	/*!< set of the strings and other memory segments
+					that must be freed
 					together with the block. The content depends
 					on the block that replaces this one */
 	unsigned char	vars[1];	/*!< blob that contains the values */
@@ -394,7 +395,7 @@ void cfg_install_child_cb(cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last);
  * cb_first and cb_last define a linked list of per-child process
  * callbacks. This list is added to the global linked list.
  */
-void cfg_install_global(cfg_block_t *block, char **replaced,
+void cfg_install_global(cfg_block_t *block, void **replaced,
 			cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last);
 
 /* creates a structure for a per-child process callback */




More information about the sr-dev mailing list