Skip to content

Apply foreach macro refactorings #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,7 @@ static int get_other_site(struct booth_site **node)
if (!booth_conf)
return 0;

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

for (i = 0; i < booth_conf->site_count; i++) {
n = booth_conf->site + i;
FOREACH_NODE(i, n) {
if ((n->type == SITE || any_type) &&
strncmp(n->addr_string, site, sizeof(n->addr_string)) == 0) {
*node = n;
Expand All @@ -1037,8 +1035,7 @@ int find_site_by_id(uint32_t site_id, struct booth_site **node)
if (!booth_conf)
return 0;

for (i = 0; i < booth_conf->site_count; i++) {
n = booth_conf->site + i;
FOREACH_NODE(i, n) {
if (n->site_id == site_id) {
*node = n;
return 1;
Expand Down
4 changes: 2 additions & 2 deletions src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ void wait_child(int sig)
int i, status;
struct ticket_config *tk;

/* use waitpid(2) and not wait(2) in order not to interfear
/* use waitpid(2) and not wait(2) in order not to interfere
* with popen(2)/pclose(2) and system(2) used in pacemaker.c
*/
foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
if (tk_test.path && tk_test.pid > 0 &&
(tk_test.progstate == EXTPROG_RUNNING ||
tk_test.progstate == EXTPROG_IGNORE) &&
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static int format_peers(char **pdata, unsigned int *len)
return -ENOMEM;

cp = data;
foreach_node(i, s) {
FOREACH_NODE(i, s) {
if (s == local)
continue;
strftime(time_str, sizeof(time_str), "%F %T",
Expand Down
10 changes: 5 additions & 5 deletions src/raft.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ inline static void clear_election(struct ticket_config *tk)

tk_log_debug("clear election");
tk->votes_received = 0;
foreach_node(i, site)
FOREACH_NODE(i, site) {
tk->votes_for[site->index] = NULL;
}
}


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

for(i=0; i<booth_conf->site_count; i++) {
for (i = 0; i < booth_conf->site_count; i++) {
v = tk->votes_for[i];
if (!v)
continue;
count[v->index]++;
max_votes = max(max_votes, count[v->index]);
}

for(i=0; i<booth_conf->site_count; i++) {
for (i = 0; i < booth_conf->site_count; i++) {
if (count[i] == max_votes)
max_cnt++;
}
Expand All @@ -183,8 +184,7 @@ static struct booth_site *majority_votes(struct ticket_config *tk)
struct booth_site *v;
int count[MAX_NODES] = { 0, };


for(i=0; i<booth_conf->site_count; i++) {
for (i = 0; i < booth_conf->site_count; i++) {
v = tk->votes_for[i];
if (!v || v == no_leader)
continue;
Expand Down
34 changes: 17 additions & 17 deletions src/ticket.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ int check_max_len_valid(const char *s, int max)

int find_ticket_by_name(const char *ticket, struct ticket_config **found)
{
struct ticket_config *tk;
int i;

if (found)
*found = NULL;

for (i = 0; i < booth_conf->ticket_count; i++) {
if (!strncmp(booth_conf->ticket[i].name, ticket,
sizeof(booth_conf->ticket[i].name))) {
FOREACH_TICKET(i, tk) {
if (!strncmp(tk->name, ticket, sizeof(tk->name))) {
if (found)
*found = booth_conf->ticket + i;
*found = tk;
return 1;
}
}
Expand Down Expand Up @@ -392,6 +392,7 @@ int do_revoke_ticket(struct ticket_config *tk)
int list_ticket(char **pdata, unsigned int *len)
{
struct ticket_config *tk;
struct booth_site *site;
char timeout_str[64];
char pending_str[64];
char *data, *cp;
Expand All @@ -405,7 +406,7 @@ int list_ticket(char **pdata, unsigned int *len)

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

foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
multiple_grant_warning_length = number_sites_marked_as_granted(tk);

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

cp = data;
foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
if ((!is_manual(tk)) && is_time_set(&tk->term_expires)) {
/* Manual tickets doesn't have term_expires defined */
ts = wall_ts(&tk->term_expires);
Expand Down Expand Up @@ -467,7 +468,7 @@ int list_ticket(char **pdata, unsigned int *len)
}
}

foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
multiple_grant_warning_length = number_sites_marked_as_granted(tk);

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

for(site_index=0; site_index<booth_conf->site_count; ++site_index) {
FOREACH_NODE(site_index, site) {
if (tk->sites_where_granted[site_index] > 0) {
cp += snprintf(cp,
alloc - (cp - data),
"%s",
site_string(&(booth_conf->site[site_index])));
site_string(site));

if (--multiple_grant_warning_length > 0) {
cp += snprintf(cp,
Expand Down Expand Up @@ -632,7 +633,7 @@ int setup_ticket(void)
struct ticket_config *tk;
int i;

foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
reset_ticket(tk);

if (local->type == SITE) {
Expand Down Expand Up @@ -859,8 +860,7 @@ static void log_lost_servers(struct ticket_config *tk)
*/
return;

for (i = 0; i < booth_conf->site_count; i++) {
n = booth_conf->site + i;
FOREACH_NODE(i, n) {
if (!(tk->acks_received & n->bitmask)) {
tk_log_warn("%s %s didn't acknowledge our %s, "
"will retry %d times",
Expand All @@ -880,8 +880,7 @@ static void resend_msg(struct ticket_config *tk)
if (!(tk->acks_received ^ local->bitmask)) {
ticket_broadcast(tk, tk->last_request, 0, RLT_SUCCESS, 0);
} else {
for (i = 0; i < booth_conf->site_count; i++) {
n = booth_conf->site + i;
FOREACH_NODE(i, n) {
if (!(tk->acks_received & n->bitmask)) {
n->resend_cnt++;
tk_log_debug("resending %s to %s",
Expand Down Expand Up @@ -1145,7 +1144,7 @@ void process_tickets(void)
int i;
timetype last_cron;

foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
if (!has_extprog_exited(tk) &&
is_time_set(&tk->next_cron) && !is_past(&tk->next_cron))
continue;
Expand All @@ -1169,7 +1168,7 @@ void tickets_log_info(void)
int i;
time_t ts;

foreach_ticket(i, tk) {
FOREACH_TICKET(i, tk) {
ts = wall_ts(&tk->term_expires);
tk_log_info("state '%s' "
"term %d "
Expand Down Expand Up @@ -1365,8 +1364,9 @@ int is_manual(struct ticket_config *tk)
int number_sites_marked_as_granted(struct ticket_config *tk)
{
int i, result = 0;
struct booth_site *ignored __attribute__((unused));

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

Expand Down
11 changes: 9 additions & 2 deletions src/ticket.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ extern int TIME_RES;
#define DEFAULT_RETRIES 10


#define foreach_ticket(i_,t_) for(i_=0; (t_=booth_conf->ticket+i_, i_<booth_conf->ticket_count); i_++)
#define foreach_node(i_,n_) for(i_=0; (n_=booth_conf->site+i_, i_<booth_conf->site_count); i_++)
#define FOREACH_TICKET(i_, t_) \
for (i_ = 0; \
(t_ = booth_conf->ticket + i_, i_ < booth_conf->ticket_count); \
i_++)

#define FOREACH_NODE(i_, n_) \
for (i_ = 0; \
(n_ = booth_conf->site + i_, i_ < booth_conf->site_count); \
i_++)

#define set_leader(tk, who) do { \
if (who == NULL) { \
Expand Down
5 changes: 2 additions & 3 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ static int find_address(unsigned char ipaddr[BOOTH_IPADDR_LEN],
/* One bit left to check means ignore 7 lowest bits. */
mask = ~( (1 << (8 - bits_left)) -1);

for (i = 0; i < booth_conf->site_count; i++) {
node = booth_conf->site + i;
FOREACH_NODE(i, node) {
if (family != node->family)
continue;
n_a = node_to_addr_pointer(node);
Expand Down Expand Up @@ -896,7 +895,7 @@ static int booth_udp_broadcast_auth(void *buf, int len)
return rv;

rvs = 0;
foreach_node(i, site) {
FOREACH_NODE(i, site) {
if (site != local) {
rv = booth_udp_send(site, buf, len);
if (!rvs)
Expand Down