Browse code

dispatcher: variables declared at the beginning of blocks

Daniel-Constantin Mierla authored on 04/07/2020 15:50:54
Showing 1 changed files
... ...
@@ -196,11 +196,11 @@ int ds_hash_load_destroy(void)
196 196
 void ds_iter_set(ds_set_t *node, void (*ds_action_cb)(ds_set_t *node, int i, void *arg),
197 197
 		void *ds_action_arg)
198 198
 {
199
+	int i;
200
+
199 201
 	if(!node)
200 202
 		return;
201 203
 
202
-	int i;
203
-
204 204
 	for(i = 0; i < 2; ++i)
205 205
 		ds_iter_set(node->next[i], ds_action_cb, ds_action_arg);
206 206
 
... ...
@@ -340,7 +340,7 @@ int ds_set_attrs(ds_dest_t *dest, str *vattrs)
340 340
 		} else if(pit->name.len == 9
341 341
 				&& strncasecmp(pit->name.s, "ping_from", 9) == 0) {
342 342
 			dest->attrs.ping_from = pit->body;
343
-		} else if(pit->name.len == 7 
343
+		} else if(pit->name.len == 7
344 344
 				  && strncasecmp(pit->name.s, "obproxy", 7) == 0) {
345 345
 			dest->attrs.obproxy = pit->body;
346 346
 		}
... ...
@@ -557,11 +557,11 @@ err:
557 557
 /* for internal usage; arr must be arr[100] */
558 558
 void shuffle_uint100array(unsigned int *arr)
559 559
 {
560
-	if(arr == NULL)
561
-		return;
562 560
 	int k;
563 561
 	int j;
564 562
 	unsigned int t;
563
+	if(arr == NULL)
564
+		return;
565 565
 	for(j = 0; j < 100; j++) {
566 566
 		k = j + (kam_rand() % (100 - j));
567 567
 		t = arr[j];
... ...
@@ -879,7 +879,7 @@ next_line:
879 879
 	/* Update list - should it be sync'ed? */
880 880
 	_ds_list_nr = setn;
881 881
 	*crt_idx = *next_idx;
882
-	
882
+
883 883
 	ds_log_sets();
884 884
 	return 0;
885 885
 
... ...
@@ -1021,7 +1021,7 @@ int ds_load_db(void)
1021 1021
 				LM_ERR("too many db columns: %d\n", nrcols);
1022 1022
 				return -1;
1023 1023
 			}
1024
-			query_cols[nrcols++] = &pit->body; 
1024
+			query_cols[nrcols++] = &pit->body;
1025 1025
 		}
1026 1026
 	}
1027 1027
 
... ...
@@ -1366,7 +1366,6 @@ int ds_hash_ruri(struct sip_msg *msg, unsigned int *hash)
1366 1366
 	str key1;
1367 1367
 	str key2;
1368 1368
 
1369
-
1370 1369
 	if(msg == NULL || hash == NULL) {
1371 1370
 		LM_ERR("bad parameters\n");
1372 1371
 		return -1;
... ...
@@ -1621,7 +1620,7 @@ int ds_load_replace(struct sip_msg *msg, str *duid)
1621 1620
 	if(olddst == -1) {
1622 1621
 		LM_WARN("old destination address not found for [%d, %.*s]\n", set,
1623 1622
 				it->duid.len, it->duid.s);
1624
-	} 
1623
+	}
1625 1624
 	if(newdst == -1) {
1626 1625
 		/* new destination has not been found: has been removed meanwhile? */
1627 1626
 		ds_unlock_cell(_dsht_load, &msg->callid->body);
... ...
@@ -1632,7 +1631,7 @@ int ds_load_replace(struct sip_msg *msg, str *duid)
1632 1631
 
1633 1632
 	ds_unlock_cell(_dsht_load, &msg->callid->body);
1634 1633
 	ds_del_cell(_dsht_load, &msg->callid->body);
1635
-	
1634
+
1636 1635
 	if(olddst != -1)
1637 1636
 		DS_LOAD_DEC(idx, olddst);
1638 1637
 
... ...
@@ -2458,7 +2457,7 @@ int ds_add_dst(int group, str *address, int flags, str *attrs)
2458 2457
 
2459 2458
 	_ds_list_nr = setn;
2460 2459
 	*crt_idx = *next_idx;
2461
-	
2460
+
2462 2461
 	ds_log_sets();
2463 2462
 	return 0;
2464 2463
 
... ...
@@ -2513,7 +2512,7 @@ int ds_remove_dst(int group, str *address)
2513 2512
 
2514 2513
 	_ds_list_nr = setn;
2515 2514
 	*crt_idx = *next_idx;
2516
-	
2515
+
2517 2516
 	ds_log_sets();
2518 2517
 	return 0;
2519 2518
 
... ...
@@ -2599,10 +2598,12 @@ static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int l
2599 2598
 	/* standard deviation using oneline algorithm */
2600 2599
 	/* https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm */
2601 2600
 	if (latency_stats->count > 1) {
2602
-		float delta = latency - latency_stats->average;
2601
+		float delta;
2602
+		float delta2;
2603
+		delta = latency - latency_stats->average;
2603 2604
 		latency_stats->average += delta/latency_stats->count;
2604
-		float delta2 = latency - latency_stats->average;
2605
-		latency_stats->m2 += delta*delta2;
2605
+		delta2 = latency - latency_stats->average;
2606
+		latency_stats->m2 += ((double)delta)*delta2;
2606 2607
 		latency_stats->stdev = sqrt(latency_stats->m2 / (latency_stats->count-1));
2607 2608
 	}
2608 2609
 	/* exponentialy weighted moving average */
... ...
@@ -2619,6 +2620,9 @@ int ds_update_latency(int group, str *address, int code)
2619 2620
 	int i = 0;
2620 2621
 	int state = 0;
2621 2622
 	ds_set_t *idx = NULL;
2623
+	int apply_rweights = 0;
2624
+	int all_gw_congested = 1;
2625
+	int total_congestion_ms = 0;
2622 2626
 
2623 2627
 	if(_ds_list == NULL || _ds_list_nr <= 0) {
2624 2628
 		LM_ERR("the list is null\n");
... ...
@@ -2630,26 +2634,25 @@ int ds_update_latency(int group, str *address, int code)
2630 2634
 		LM_ERR("destination set [%d] not found\n", group);
2631 2635
 		return -1;
2632 2636
 	}
2633
-	int apply_rweights = 0;
2634
-	int all_gw_congested = 1;
2635
-	int total_congestion_ms = 0;
2636 2637
 	lock_get(&idx->lock);
2637 2638
 	while (i < idx->nr) {
2638 2639
 		ds_dest_t *ds_dest = &idx->dlist[i];
2639 2640
 		ds_latency_stats_t *latency_stats = &ds_dest->latency_stats;
2640 2641
 		if (ds_dest->uri.len == address->len
2641 2642
 				&& strncasecmp(ds_dest->uri.s, address->s, address->len) == 0) {
2643
+			struct timeval now;
2644
+			int latency_ms;
2645
+			int congestion_ms;
2642 2646
 			/* Destination address found, this is the gateway that was pinged. */
2643 2647
 			state = ds_dest->flags;
2644 2648
 			if (code == 408 && latency_stats->timeout < UINT32_MAX)
2645 2649
 				latency_stats->timeout++;
2646
-			struct timeval now;
2647 2650
 			gettimeofday(&now, NULL);
2648
-			int latency_ms = (now.tv_sec - latency_stats->start.tv_sec)*1000
2651
+			latency_ms = (now.tv_sec - latency_stats->start.tv_sec)*1000
2649 2652
 		            + (now.tv_usec - latency_stats->start.tv_usec)/1000;
2650 2653
 			latency_stats_update(latency_stats, latency_ms);
2651 2654
 
2652
-			int congestion_ms = latency_stats->estimate - latency_stats->average;
2655
+			congestion_ms = latency_stats->estimate - latency_stats->average;
2653 2656
 			if (congestion_ms < 0) congestion_ms = 0;
2654 2657
 			total_congestion_ms += congestion_ms;
2655 2658
 
... ...
@@ -2672,10 +2675,12 @@ int ds_update_latency(int group, str *address, int code)
2672 2675
 			}
2673 2676
 		} else {
2674 2677
 			/* Another gateway in the set, we verify if it is congested. */
2675
-			int congestion_ms = latency_stats->estimate - latency_stats->average;
2678
+			int congestion_ms;
2679
+			int active_weight;
2680
+			congestion_ms = latency_stats->estimate - latency_stats->average;
2676 2681
 			if (congestion_ms < 0) congestion_ms = 0;
2677 2682
 			total_congestion_ms += congestion_ms;
2678
-			int active_weight = ds_dest->attrs.weight - congestion_ms;
2683
+			active_weight = ds_dest->attrs.weight - congestion_ms;
2679 2684
 			if (active_weight > 0) all_gw_congested = 0;
2680 2685
 		}
2681 2686
 		if (!ds_dest->attrs.congestion_control) all_gw_congested = 0;
... ...
@@ -2686,11 +2691,13 @@ int ds_update_latency(int group, str *address, int code)
2686 2691
 	if (all_gw_congested) {
2687 2692
 		i = 0;
2688 2693
 		while (i < idx->nr) {
2694
+			int congestion_ms;
2695
+			int active_weight;
2689 2696
 			ds_dest_t *ds_dest = &idx->dlist[i];
2690 2697
 			ds_latency_stats_t *latency_stats = &ds_dest->latency_stats;
2691
-			int congestion_ms = latency_stats->estimate - latency_stats->average;
2698
+			congestion_ms = latency_stats->estimate - latency_stats->average;
2692 2699
 			/* We multiply by 2^4 to keep enough precision */
2693
-			int active_weight = (total_congestion_ms << 4) / congestion_ms;
2700
+			active_weight = (total_congestion_ms << 4) / congestion_ms;
2694 2701
 			if (ds_dest->attrs.rweight != active_weight) {
2695 2702
 				apply_rweights = 1;
2696 2703
 				ds_dest->attrs.rweight = active_weight;