Browse code

minor simplification: some memcpy()s are replaced with assignment

Miklos Tirpak authored on 18/02/2008 10:40:21
Showing 4 changed files
... ...
@@ -262,7 +262,6 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, str *var_name,
262 262
 	char		*old_string = NULL;
263 263
 	char		**replaced = NULL;
264 264
 	cfg_child_cb_t	*child_cb = NULL;
265
-	int		i;
266 265
 
267 266
 	/* verify the context even if we do not need it now
268 267
 	to make sure that a cfg driver has called the function
... ...
@@ -335,8 +334,7 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, str *var_name,
335 334
 	/* set the new value */
336 335
 	switch (CFG_VAR_TYPE(var)) {
337 336
 	case CFG_VAR_INT:
338
-		i = (int)(long)v;
339
-		memcpy(p, &i, sizeof(int));
337
+		*(int *)p = (int)(long)v;
340 338
 		break;
341 339
 
342 340
 	case CFG_VAR_STRING:
... ...
@@ -344,20 +342,20 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, str *var_name,
344 342
 		s.s = v;
345 343
 		s.len = (s.s) ? strlen(s.s) : 0;
346 344
 		if (cfg_clone_str(&s, &s)) goto error;
347
-		memcpy(&old_string, p, sizeof(char *));
348
-		memcpy(p, &s.s, sizeof(char *));
345
+		old_string = *(char **)p;
346
+		*(char **)p = s.s;
349 347
 		break;
350 348
 
351 349
 	case CFG_VAR_STR:
352 350
 		/* clone the string to shm mem */
353 351
 		s = *(str *)v;
354 352
 		if (cfg_clone_str(&s, &s)) goto error;
355
-		memcpy(&old_string, p, sizeof(char *));
353
+		old_string = *(char **)p;
356 354
 		memcpy(p, &s, sizeof(str));
357 355
 		break;
358 356
 
359 357
 	case CFG_VAR_POINTER:
360
-		memcpy(p, &v, sizeof(void *));
358
+		*(void **)p = v;
361 359
 		break;
362 360
 
363 361
 	}
... ...
@@ -489,7 +487,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
489 487
 	char		*temp_handle;
490 488
 	int		temp_handle_created;
491 489
 	cfg_changed_var_t	*changed = NULL;
492
-	int		i, size;
490
+	int		size;
493 491
 	str		s;
494 492
 
495 493
 	if (!cfg_shmized)
... ...
@@ -585,8 +583,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
585 583
 	switch (CFG_VAR_TYPE(var)) {
586 584
 
587 585
 	case CFG_VAR_INT:
588
-		i = (int)(long)v;
589
-		memcpy(changed->new_val, &i, sizeof(int));
586
+		*(int *)changed->new_val = (int)(long)v;
590 587
 		break;
591 588
 
592 589
 	case CFG_VAR_STRING:
... ...
@@ -594,7 +591,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
594 591
 		s.s = v;
595 592
 		s.len = (s.s) ? strlen(s.s) : 0;
596 593
 		if (cfg_clone_str(&s, &s)) goto error;
597
-		memcpy(changed->new_val, &s.s, sizeof(char *));
594
+		*(char **)changed->new_val = s.s;
598 595
 		break;
599 596
 
600 597
 	case CFG_VAR_STR:
... ...
@@ -605,7 +602,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
605 602
 		break;
606 603
 
607 604
 	case CFG_VAR_POINTER:
608
-		memcpy(changed->new_val, &v, sizeof(void *));
605
+		*(void **)changed->new_val = v;
609 606
 		break;
610 607
 
611 608
 	}
... ...
@@ -770,7 +767,7 @@ int cfg_commit(cfg_ctx_t *ctx)
770 767
 
771 768
 		if ((CFG_VAR_TYPE(changed->var) == CFG_VAR_STRING)
772 769
 		|| (CFG_VAR_TYPE(changed->var) == CFG_VAR_STR)) {
773
-			memcpy(&(replaced[replaced_num]), p, sizeof(char *));
770
+			replaced[replaced_num] = *(char **)p;
774 771
 			if (replaced[replaced_num])
775 772
 				replaced_num++;
776 773
 			/* else do not increase replaced_num, because
... ...
@@ -821,7 +818,6 @@ error0:
821 818
 int cfg_rollback(cfg_ctx_t *ctx)
822 819
 {
823 820
 	cfg_changed_var_t	*changed, *changed2;
824
-	char	*new_string;
825 821
 
826 822
 	if (!ctx) {
827 823
 		LOG(L_ERR, "ERROR: cfg_rollback(): context is undefined\n");
... ...
@@ -846,8 +842,8 @@ int cfg_rollback(cfg_ctx_t *ctx)
846 842
 
847 843
 		if ((CFG_VAR_TYPE(changed->var) == CFG_VAR_STRING)
848 844
 		|| (CFG_VAR_TYPE(changed->var) == CFG_VAR_STR)) {
849
-			memcpy(&new_string, changed->new_val, sizeof(char *));
850
-			if (new_string) shm_free(new_string);
845
+			if (*(char **)(changed->new_val))
846
+				shm_free(*(char **)(changed->new_val));
851 847
 		}
852 848
 		shm_free(changed);
853 849
 	}
... ...
@@ -873,8 +869,6 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
873 869
 	void		*p;
874 870
 	static str	s;	/* we need the value even
875 871
 				after the function returns */
876
-	int		i;
877
-	char		*ch;
878 872
 
879 873
 	/* verify the context even if we do not need it now
880 874
 	to make sure that a cfg driver has called the function
... ...
@@ -902,13 +896,11 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
902 896
 
903 897
 	switch (CFG_VAR_TYPE(var)) {
904 898
 	case CFG_VAR_INT:
905
-		memcpy(&i, p, sizeof(int));
906
-		*val = (void *)(long)i;
899
+		*val = (void *)(long)*(int *)p;
907 900
 		break;
908 901
 
909 902
 	case CFG_VAR_STRING:
910
-		memcpy(&ch, p, sizeof(char *));
911
-		*val = (void *)ch;
903
+		*val = (void *)*(char **)p;
912 904
 		break;
913 905
 
914 906
 	case CFG_VAR_STR:
... ...
@@ -917,7 +909,7 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, str *var_name,
917 909
 		break;
918 910
 
919 911
 	case CFG_VAR_POINTER:
920
-		memcpy(val, &p, sizeof(void *));
912
+		*val = *(void **)p;
921 913
 		break;
922 914
 
923 915
 	}
... ...
@@ -999,8 +991,6 @@ int cfg_diff_next(void **h,
999 991
 	void	*p;
1000 992
 	static str	old_s, new_s;	/* we need the value even
1001 993
 					after the function returns */
1002
-	int		i;
1003
-	char		*ch;
1004 994
 
1005 995
 	changed = (cfg_changed_var_t *)(*h);
1006 996
 	if (changed == NULL) return 0;
... ...
@@ -1017,17 +1007,13 @@ int cfg_diff_next(void **h,
1017 1007
 
1018 1008
 	switch (CFG_VAR_TYPE(changed->var)) {
1019 1009
 	case CFG_VAR_INT:
1020
-		memcpy(&i, p, sizeof(int));
1021
-		*old_val = (void *)(long)i;
1022
-		memcpy(&i, changed->new_val, sizeof(int));
1023
-		*new_val = (void *)(long)i;
1010
+		*old_val = (void *)(long)*(int *)p;
1011
+		*new_val = (void *)(long)*(int *)changed->new_val;
1024 1012
 		break;
1025 1013
 
1026 1014
 	case CFG_VAR_STRING:
1027
-		memcpy(&ch, p, sizeof(char *));
1028
-		*old_val = (void *)ch;
1029
-		memcpy(&ch, changed->new_val, sizeof(char *));
1030
-		*new_val = (void *)ch;
1015
+		*old_val = (void *)*(char **)p;
1016
+		*new_val = (void *)*(char **)changed->new_val;
1031 1017
 		break;
1032 1018
 
1033 1019
 	case CFG_VAR_STR:
... ...
@@ -1038,8 +1024,8 @@ int cfg_diff_next(void **h,
1038 1024
 		break;
1039 1025
 
1040 1026
 	case CFG_VAR_POINTER:
1041
-		memcpy(old_val, &p, sizeof(void *));
1042
-		memcpy(new_val, &changed->new_val, sizeof(void *));
1027
+		*old_val = *(void **)p;
1028
+		*new_val = *(void **)changed->new_val;
1043 1029
 		break;
1044 1030
 
1045 1031
 	}
... ...
@@ -185,7 +185,7 @@ int cfg_script_fixup(cfg_group_t *group, unsigned char *block)
185 185
 			offset = ROUND_INT(offset);
186 186
 			mapping[i].offset = offset;
187 187
 
188
-			memcpy(block + offset, &script_var->val.i, sizeof(int));
188
+			*(int *)(block + offset) = script_var->val.i;
189 189
 
190 190
 			offset += sizeof(int);
191 191
 			break;
... ...
@@ -88,14 +88,14 @@ int select_cfg_var(str *res, select_t *s, struct sip_msg *msg)
88 88
 
89 89
 	switch (CFG_VAR_TYPE(var)) {
90 90
 	case CFG_VAR_INT:
91
-		memcpy(&i, p, sizeof(int));
91
+		i = *(int *)p;
92 92
 		res->len = snprintf(buf, sizeof(buf)-1, "%d", i);
93 93
 		buf[res->len] = '\0';
94 94
 		res->s = buf;
95 95
 		break;
96 96
 
97 97
 	case CFG_VAR_STRING:
98
-		memcpy(&res->s, p, sizeof(char *));
98
+		res->s = *(char **)p;
99 99
 		res->len = (res->s) ? strlen(res->s) : 0;
100 100
 		break;
101 101
 
... ...
@@ -137,7 +137,7 @@ static int cfg_shmize_strings(cfg_group_t *group)
137 137
 		if (mapping[i].flag & cfg_var_shmized) continue;
138 138
 
139 139
 		if (CFG_VAR_TYPE(&mapping[i]) == CFG_VAR_STRING) {
140
-			memcpy(&s.s, group->vars + mapping[i].offset, sizeof(char *));
140
+			s.s = *(char **)(group->vars + mapping[i].offset);
141 141
 			if (!s.s) continue;
142 142
 			s.len = strlen(s.s);
143 143
 
... ...
@@ -149,7 +149,7 @@ static int cfg_shmize_strings(cfg_group_t *group)
149 149
 			continue;
150 150
 		}
151 151
 		if (cfg_clone_str(&s, &s)) return -1;
152
-		memcpy(group->vars + mapping[i].offset, &s.s, sizeof(char *));
152
+		*(char **)(group->vars + mapping[i].offset) = s.s;
153 153
 		mapping[i].flag |= cfg_var_shmized;
154 154
 	}
155 155
 
... ...
@@ -243,9 +243,7 @@ static void cfg_destory_groups(unsigned char *block)
243 243
 				(CFG_VAR_TYPE(&mapping[i]) == CFG_VAR_STR)) &&
244 244
 					mapping[i].flag & cfg_var_shmized) {
245 245
 
246
-						memcpy(	&old_string,
247
-							block + group->offset + mapping[i].offset,
248
-							sizeof(char *));
246
+						old_string = *(char **)(block + group->offset + mapping[i].offset);
249 247
 						if (old_string) shm_free(old_string);
250 248
 				}
251 249