Skip to content

Commit dbf1b73

Browse files
authored
Merge pull request #145 from clumens/foreach
Apply foreach macro refactorings
2 parents 322fea0 + de2b6a2 commit dbf1b73

File tree

7 files changed

+39
-36
lines changed

7 files changed

+39
-36
lines changed

src/config.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,7 @@ static int get_other_site(struct booth_site **node)
986986
if (!booth_conf)
987987
return 0;
988988

989-
for (i = 0; i < booth_conf->site_count; i++) {
990-
n = booth_conf->site + i;
989+
FOREACH_NODE(i, n) {
991990
if (n != local && n->type == SITE) {
992991
if (!*node) {
993992
*node = n;
@@ -1012,8 +1011,7 @@ int find_site_by_name(char *site, struct booth_site **node, int any_type)
10121011
if (!strcmp(site, OTHER_SITE))
10131012
return get_other_site(node);
10141013

1015-
for (i = 0; i < booth_conf->site_count; i++) {
1016-
n = booth_conf->site + i;
1014+
FOREACH_NODE(i, n) {
10171015
if ((n->type == SITE || any_type) &&
10181016
strncmp(n->addr_string, site, sizeof(n->addr_string)) == 0) {
10191017
*node = n;
@@ -1037,8 +1035,7 @@ int find_site_by_id(uint32_t site_id, struct booth_site **node)
10371035
if (!booth_conf)
10381036
return 0;
10391037

1040-
for (i = 0; i < booth_conf->site_count; i++) {
1041-
n = booth_conf->site + i;
1038+
FOREACH_NODE(i, n) {
10421039
if (n->site_id == site_id) {
10431040
*node = n;
10441041
return 1;

src/handler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ void wait_child(int sig)
130130
int i, status;
131131
struct ticket_config *tk;
132132

133-
/* use waitpid(2) and not wait(2) in order not to interfear
133+
/* use waitpid(2) and not wait(2) in order not to interfere
134134
* with popen(2)/pclose(2) and system(2) used in pacemaker.c
135135
*/
136-
foreach_ticket(i, tk) {
136+
FOREACH_TICKET(i, tk) {
137137
if (tk_test.path && tk_test.pid > 0 &&
138138
(tk_test.progstate == EXTPROG_RUNNING ||
139139
tk_test.progstate == EXTPROG_IGNORE) &&

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static int format_peers(char **pdata, unsigned int *len)
233233
return -ENOMEM;
234234

235235
cp = data;
236-
foreach_node(i, s) {
236+
FOREACH_NODE(i, s) {
237237
if (s == local)
238238
continue;
239239
strftime(time_str, sizeof(time_str), "%F %T",

src/raft.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ inline static void clear_election(struct ticket_config *tk)
4040

4141
tk_log_debug("clear election");
4242
tk->votes_received = 0;
43-
foreach_node(i, site)
43+
FOREACH_NODE(i, site) {
4444
tk->votes_for[site->index] = NULL;
45+
}
4546
}
4647

4748

@@ -161,15 +162,15 @@ static int is_tie(struct ticket_config *tk)
161162
int count[MAX_NODES] = { 0, };
162163
int max_votes = 0, max_cnt = 0;
163164

164-
for(i=0; i<booth_conf->site_count; i++) {
165+
for (i = 0; i < booth_conf->site_count; i++) {
165166
v = tk->votes_for[i];
166167
if (!v)
167168
continue;
168169
count[v->index]++;
169170
max_votes = max(max_votes, count[v->index]);
170171
}
171172

172-
for(i=0; i<booth_conf->site_count; i++) {
173+
for (i = 0; i < booth_conf->site_count; i++) {
173174
if (count[i] == max_votes)
174175
max_cnt++;
175176
}
@@ -183,8 +184,7 @@ static struct booth_site *majority_votes(struct ticket_config *tk)
183184
struct booth_site *v;
184185
int count[MAX_NODES] = { 0, };
185186

186-
187-
for(i=0; i<booth_conf->site_count; i++) {
187+
for (i = 0; i < booth_conf->site_count; i++) {
188188
v = tk->votes_for[i];
189189
if (!v || v == no_leader)
190190
continue;

src/ticket.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ int check_max_len_valid(const char *s, int max)
5959

6060
int find_ticket_by_name(const char *ticket, struct ticket_config **found)
6161
{
62+
struct ticket_config *tk;
6263
int i;
6364

6465
if (found)
6566
*found = NULL;
6667

67-
for (i = 0; i < booth_conf->ticket_count; i++) {
68-
if (!strncmp(booth_conf->ticket[i].name, ticket,
69-
sizeof(booth_conf->ticket[i].name))) {
68+
FOREACH_TICKET(i, tk) {
69+
if (!strncmp(tk->name, ticket, sizeof(tk->name))) {
7070
if (found)
71-
*found = booth_conf->ticket + i;
71+
*found = tk;
7272
return 1;
7373
}
7474
}
@@ -392,6 +392,7 @@ int do_revoke_ticket(struct ticket_config *tk)
392392
int list_ticket(char **pdata, unsigned int *len)
393393
{
394394
struct ticket_config *tk;
395+
struct booth_site *site;
395396
char timeout_str[64];
396397
char pending_str[64];
397398
char *data, *cp;
@@ -405,7 +406,7 @@ int list_ticket(char **pdata, unsigned int *len)
405406

406407
alloc = booth_conf->ticket_count * (BOOTH_NAME_LEN * 2 + 128 + 16);
407408

408-
foreach_ticket(i, tk) {
409+
FOREACH_TICKET(i, tk) {
409410
multiple_grant_warning_length = number_sites_marked_as_granted(tk);
410411

411412
if (multiple_grant_warning_length > 1) {
@@ -419,7 +420,7 @@ int list_ticket(char **pdata, unsigned int *len)
419420
return -ENOMEM;
420421

421422
cp = data;
422-
foreach_ticket(i, tk) {
423+
FOREACH_TICKET(i, tk) {
423424
if ((!is_manual(tk)) && is_time_set(&tk->term_expires)) {
424425
/* Manual tickets doesn't have term_expires defined */
425426
ts = wall_ts(&tk->term_expires);
@@ -467,7 +468,7 @@ int list_ticket(char **pdata, unsigned int *len)
467468
}
468469
}
469470

470-
foreach_ticket(i, tk) {
471+
FOREACH_TICKET(i, tk) {
471472
multiple_grant_warning_length = number_sites_marked_as_granted(tk);
472473

473474
if (multiple_grant_warning_length > 1) {
@@ -476,12 +477,12 @@ int list_ticket(char **pdata, unsigned int *len)
476477
"\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters
477478
tk->name);
478479

479-
for(site_index=0; site_index<booth_conf->site_count; ++site_index) {
480+
FOREACH_NODE(site_index, site) {
480481
if (tk->sites_where_granted[site_index] > 0) {
481482
cp += snprintf(cp,
482483
alloc - (cp - data),
483484
"%s",
484-
site_string(&(booth_conf->site[site_index])));
485+
site_string(site));
485486

486487
if (--multiple_grant_warning_length > 0) {
487488
cp += snprintf(cp,
@@ -632,7 +633,7 @@ int setup_ticket(void)
632633
struct ticket_config *tk;
633634
int i;
634635

635-
foreach_ticket(i, tk) {
636+
FOREACH_TICKET(i, tk) {
636637
reset_ticket(tk);
637638

638639
if (local->type == SITE) {
@@ -859,8 +860,7 @@ static void log_lost_servers(struct ticket_config *tk)
859860
*/
860861
return;
861862

862-
for (i = 0; i < booth_conf->site_count; i++) {
863-
n = booth_conf->site + i;
863+
FOREACH_NODE(i, n) {
864864
if (!(tk->acks_received & n->bitmask)) {
865865
tk_log_warn("%s %s didn't acknowledge our %s, "
866866
"will retry %d times",
@@ -880,8 +880,7 @@ static void resend_msg(struct ticket_config *tk)
880880
if (!(tk->acks_received ^ local->bitmask)) {
881881
ticket_broadcast(tk, tk->last_request, 0, RLT_SUCCESS, 0);
882882
} else {
883-
for (i = 0; i < booth_conf->site_count; i++) {
884-
n = booth_conf->site + i;
883+
FOREACH_NODE(i, n) {
885884
if (!(tk->acks_received & n->bitmask)) {
886885
n->resend_cnt++;
887886
tk_log_debug("resending %s to %s",
@@ -1145,7 +1144,7 @@ void process_tickets(void)
11451144
int i;
11461145
timetype last_cron;
11471146

1148-
foreach_ticket(i, tk) {
1147+
FOREACH_TICKET(i, tk) {
11491148
if (!has_extprog_exited(tk) &&
11501149
is_time_set(&tk->next_cron) && !is_past(&tk->next_cron))
11511150
continue;
@@ -1169,7 +1168,7 @@ void tickets_log_info(void)
11691168
int i;
11701169
time_t ts;
11711170

1172-
foreach_ticket(i, tk) {
1171+
FOREACH_TICKET(i, tk) {
11731172
ts = wall_ts(&tk->term_expires);
11741173
tk_log_info("state '%s' "
11751174
"term %d "
@@ -1365,8 +1364,9 @@ int is_manual(struct ticket_config *tk)
13651364
int number_sites_marked_as_granted(struct ticket_config *tk)
13661365
{
13671366
int i, result = 0;
1367+
struct booth_site *ignored __attribute__((unused));
13681368

1369-
for(i=0; i<booth_conf->site_count; ++i) {
1369+
FOREACH_NODE(i, ignored) {
13701370
result += tk->sites_where_granted[i];
13711371
}
13721372

src/ticket.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ extern int TIME_RES;
3535
#define DEFAULT_RETRIES 10
3636

3737

38-
#define foreach_ticket(i_,t_) for(i_=0; (t_=booth_conf->ticket+i_, i_<booth_conf->ticket_count); i_++)
39-
#define foreach_node(i_,n_) for(i_=0; (n_=booth_conf->site+i_, i_<booth_conf->site_count); i_++)
38+
#define FOREACH_TICKET(i_, t_) \
39+
for (i_ = 0; \
40+
(t_ = booth_conf->ticket + i_, i_ < booth_conf->ticket_count); \
41+
i_++)
42+
43+
#define FOREACH_NODE(i_, n_) \
44+
for (i_ = 0; \
45+
(n_ = booth_conf->site + i_, i_ < booth_conf->site_count); \
46+
i_++)
4047

4148
#define set_leader(tk, who) do { \
4249
if (who == NULL) { \

src/transport.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ static int find_address(unsigned char ipaddr[BOOTH_IPADDR_LEN],
9898
/* One bit left to check means ignore 7 lowest bits. */
9999
mask = ~( (1 << (8 - bits_left)) -1);
100100

101-
for (i = 0; i < booth_conf->site_count; i++) {
102-
node = booth_conf->site + i;
101+
FOREACH_NODE(i, node) {
103102
if (family != node->family)
104103
continue;
105104
n_a = node_to_addr_pointer(node);
@@ -896,7 +895,7 @@ static int booth_udp_broadcast_auth(void *buf, int len)
896895
return rv;
897896

898897
rvs = 0;
899-
foreach_node(i, site) {
898+
FOREACH_NODE(i, site) {
900899
if (site != local) {
901900
rv = booth_udp_send(site, buf, len);
902901
if (!rvs)

0 commit comments

Comments
 (0)