Browse code

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.

Miklos Tirpak authored on 07/09/2010 15:37:20
Showing 3 changed files
... ...
@@ -306,13 +306,14 @@ static int cfg_update_defaults(cfg_group_meta_t	*meta,
306 306
 int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
307 307
 			void *val, unsigned int val_type)
308 308
 {
309
+	int		i;
309 310
 	cfg_group_t	*group;
310 311
 	cfg_mapping_t	*var;
311 312
 	void		*p, *v;
312 313
 	cfg_block_t	*block = NULL;
313 314
 	str		s, s2;
314 315
 	char		*old_string = NULL;
315
-	char		**replaced = NULL;
316
+	void		**replaced = NULL;
316 317
 	cfg_child_cb_t	*child_cb = NULL;
317 318
 	cfg_group_inst_t	*group_inst, *new_array = NULL;
318 319
 	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
523 524
 				new_array = CFG_GROUP_META(block, group)->array;
524 525
 		}
525 526
 
526
-		if (old_string) {
527
+		if (old_string || new_array) {
527 528
 			/* prepare the array of the replaced strings,
529
+			and replaced group instances,
528 530
 			they will be freed when the old block is freed */
529
-			replaced = (char **)shm_malloc(sizeof(char *)*2);
531
+			replaced = (void **)shm_malloc(sizeof(void *)
532
+					* ((old_string?1:0) + (new_array?1:0) + 1));
530 533
 			if (!replaced) {
531 534
 				LOG(L_ERR, "ERROR: cfg_set_now(): not enough shm memory\n");
532 535
 				goto error;
533 536
 			}
534
-			replaced[0] = old_string;
535
-			replaced[1] = NULL;
537
+			i = 0;
538
+			if (old_string) {
539
+				replaced[i] = old_string;
540
+				i++;
541
+			}
542
+			if (new_array) {	
543
+				replaced[i] = CFG_GROUP_META(*cfg_global, group)->array;
544
+				i++;
545
+			}
546
+			replaced[i] = NULL;
536 547
 		}
537 548
 		/* replace the global config with the new one */
538 549
 		if (block) cfg_install_global(block, replaced, child_cb, child_cb);
... ...
@@ -845,7 +856,7 @@ int cfg_commit(cfg_ctx_t *ctx)
845 856
 	int	replaced_num = 0;
846 857
 	cfg_changed_var_t	*changed, *changed2;
847 858
 	cfg_block_t	*block;
848
-	char	**replaced = NULL;
859
+	void	**replaced = NULL;
849 860
 	cfg_child_cb_t	*child_cb;
850 861
 	cfg_child_cb_t	*child_cb_first = NULL;
851 862
 	cfg_child_cb_t	*child_cb_last = NULL;
... ...
@@ -899,8 +910,8 @@ int cfg_commit(cfg_ctx_t *ctx)
899 910
 
900 911
 	if (replaced_num) {
901 912
 		/* allocate memory for the replaced string array */
902
-		size = sizeof(char *)*(replaced_num + 1);
903
-		replaced = (char **)shm_malloc(size);
913
+		size = sizeof(void *)*(replaced_num + 1);
914
+		replaced = (void **)shm_malloc(size);
904 915
 		if (!replaced) {
905 916
 			LOG(L_ERR, "ERROR: cfg_commit(): not enough shm memory\n");
906 917
 			goto error;
... ...
@@ -645,7 +645,7 @@ void cfg_install_child_cb(cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last)
645 645
  * cb_first and cb_last define a linked list of per-child process
646 646
  * callbacks. This list is added to the global linked list.
647 647
  */
648
-void cfg_install_global(cfg_block_t *block, char **replaced,
648
+void cfg_install_global(cfg_block_t *block, void **replaced,
649 649
 			cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last)
650 650
 {
651 651
 	cfg_block_t* old_cfg;
... ...
@@ -122,7 +122,8 @@ typedef struct _cfg_block {
122 122
 	atomic_t	refcnt;		/*!< reference counter,
123 123
 					the block is automatically deleted
124 124
 					when it reaches 0 */
125
-	char		**replaced;	/*!< set of the strings that must be freed
125
+	void		**replaced;	/*!< set of the strings and other memory segments
126
+					that must be freed
126 127
 					together with the block. The content depends
127 128
 					on the block that replaces this one */
128 129
 	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);
394 395
  * cb_first and cb_last define a linked list of per-child process
395 396
  * callbacks. This list is added to the global linked list.
396 397
  */
397
-void cfg_install_global(cfg_block_t *block, char **replaced,
398
+void cfg_install_global(cfg_block_t *block, void **replaced,
398 399
 			cfg_child_cb_t *cb_first, cfg_child_cb_t *cb_last);
399 400
 
400 401
 /* creates a structure for a per-child process callback */