From 6bc77ccf4b39e543571d5b7bb45b96dc5d2c99ac Mon Sep 17 00:00:00 2001 From: Michael Walle Date: Thu, 21 Jun 2018 14:59:24 +0200 Subject: [PATCH] Replace memcmp() with pid_eq() and cid_eq() Consistenly use the pid_eq() and cid_eq() helper functions. Move the functions into utils.h (making them inline functions) because they are used in multiple source files. Signed-off-by: Michael Walle --- clock.c | 13 ++++--------- phc2sys.c | 5 ++--- port.c | 18 +++++++----------- port_private.h | 1 - tc.c | 2 +- util.h | 24 ++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 25 deletions(-) diff --git a/clock.c b/clock.c index 1edbb37..7bbb848 100644 --- a/clock.c +++ b/clock.c @@ -131,11 +131,6 @@ static void handle_state_decision_event(struct clock *c); static int clock_resize_pollfd(struct clock *c, int new_nports); static void clock_remove_port(struct clock *c, struct port *p); -static int cid_eq(struct ClockIdentity *a, struct ClockIdentity *b) -{ - return 0 == memcmp(a, b, sizeof(*a)); -} - static void remove_subscriber(struct clock_subscriber *s) { LIST_REMOVE(s, list); @@ -157,8 +152,8 @@ static void clock_update_subscription(struct clock *c, struct ptp_message *req, } LIST_FOREACH(s, &c->subscribers, list) { - if (!memcmp(&s->targetPortIdentity, &req->header.sourcePortIdentity, - sizeof(struct PortIdentity))) { + if (pid_eq(&s->targetPortIdentity, + &req->header.sourcePortIdentity)) { /* Found, update the transport address and event * mask. */ if (!remove) { @@ -196,8 +191,8 @@ static void clock_get_subscription(struct clock *c, struct ptp_message *req, struct timespec now; LIST_FOREACH(s, &c->subscribers, list) { - if (!memcmp(&s->targetPortIdentity, &req->header.sourcePortIdentity, - sizeof(struct PortIdentity))) { + if (pid_eq(&s->targetPortIdentity, + &req->header.sourcePortIdentity)) { memcpy(bitmask, s->events, EVENT_BITMASK_CNT); clock_gettime(CLOCK_MONOTONIC, &now); if (s->expiration < now.tv_sec) diff --git a/phc2sys.c b/phc2sys.c index dc0cfc5..3f2a0f8 100644 --- a/phc2sys.c +++ b/phc2sys.c @@ -807,9 +807,8 @@ static int check_clock_identity(struct node *node, struct ptp_message *msg) { if (!node->clock_identity_set) return 1; - return !memcmp(&node->clock_identity, - &msg->header.sourcePortIdentity.clockIdentity, - sizeof(struct ClockIdentity)); + return cid_eq(&node->clock_identity, + &msg->header.sourcePortIdentity.clockIdentity); } static int is_msg_mgt(struct ptp_message *msg) diff --git a/port.c b/port.c index ddb064a..64c2fcf 100644 --- a/port.c +++ b/port.c @@ -147,11 +147,6 @@ static int msg_source_equal(struct ptp_message *m1, struct foreign_clock *fc) return 0 == memcmp(id1, id2, sizeof(*id1)); } -int pid_eq(struct PortIdentity *a, struct PortIdentity *b) -{ - return 0 == memcmp(a, b, sizeof(*a)); -} - int source_pid_eq(struct ptp_message *m1, struct ptp_message *m2) { return pid_eq(&m1->header.sourcePortIdentity, @@ -418,7 +413,7 @@ static int net_sync_resp_append(struct port *p, struct ptp_message *m) pid = dad->pds.parentPortIdentity.clockIdentity; paddr = (struct PortAddress *)buf; - if (best && memcmp(&cid, &pid, sizeof(cid))) { + if (best && !cid_eq(&cid, &pid)) { /* Extract the parent's protocol address. */ paddr->networkProtocol = transport_type(best->trp); paddr->addressLength = @@ -559,8 +554,9 @@ static int path_trace_ignore(struct port *p, struct ptp_message *m) cnt = path_length(ptt); cid = clock_identity(p->clock); for (i = 0; i < cnt; i++) { - if (0 == memcmp(&ptt->cid[i], &cid, sizeof(cid))) + if (cid_eq(&ptt->cid[i], &cid)) { return 1; + } } } return 0; @@ -681,7 +677,7 @@ static int port_ignore(struct port *p, struct ptp_message *m) c1 = clock_identity(p->clock); c2 = m->header.sourcePortIdentity.clockIdentity; - if (0 == memcmp(&c1, &c2, sizeof(c1))) { + if (cid_eq(&c1, &c2)) { return 1; } return 0; @@ -724,7 +720,7 @@ static int port_sync_incapable(struct port *p) } cid = clock_identity(p->clock); pid = clock_parent_identity(p->clock); - if (!memcmp(&cid, &pid.clockIdentity, sizeof(cid))) { + if (cid_eq(&cid, &pid.clockIdentity)) { /* * We are the GM, but without gmCapable set. */ @@ -1854,7 +1850,7 @@ void process_follow_up(struct port *p, struct ptp_message *m) break; } master = clock_parent_identity(p->clock); - if (memcmp(&master, &m->header.sourcePortIdentity, sizeof(master))) { + if (!pid_eq(&master, &m->header.sourcePortIdentity)) { return; } @@ -2146,7 +2142,7 @@ void process_sync(struct port *p, struct ptp_message *m) break; } master = clock_parent_identity(p->clock); - if (memcmp(&master, &m->header.sourcePortIdentity, sizeof(master))) { + if (!pid_eq(&master, &m->header.sourcePortIdentity)) { return; } diff --git a/port_private.h b/port_private.h index 80b97cd..78d726d 100644 --- a/port_private.h +++ b/port_private.h @@ -164,7 +164,6 @@ int process_pdelay_req(struct port *p, struct ptp_message *m); int process_pdelay_resp(struct port *p, struct ptp_message *m); void process_pdelay_resp_fup(struct port *p, struct ptp_message *m); void process_sync(struct port *p, struct ptp_message *m); -int pid_eq(struct PortIdentity *a, struct PortIdentity *b); int source_pid_eq(struct ptp_message *m1, struct ptp_message *m2); void ts_add(tmv_t *ts, Integer64 correction); diff --git a/tc.c b/tc.c index 147bc5f..8b71451 100644 --- a/tc.c +++ b/tc.c @@ -490,7 +490,7 @@ int tc_ignore(struct port *p, struct ptp_message *m) c1 = clock_identity(p->clock); c2 = m->header.sourcePortIdentity.clockIdentity; - if (0 == memcmp(&c1, &c2, sizeof(c1))) { + if (cid_eq(&c1, &c2)) { return 1; } return 0; diff --git a/util.h b/util.h index 41fbdb2..256a799 100644 --- a/util.h +++ b/util.h @@ -59,6 +59,18 @@ char *bin2str_impl(Octet *data, int len, char *buf, int buf_len); */ char *cid2str(struct ClockIdentity *id); +/** + * Compare two clock identities for equality. + * + * @param a First clock identity. + * @param b Second clock identity. + * @return 1 if identities are equal, 0 otherwise. + */ +static inline int cid_eq(struct ClockIdentity *a, struct ClockIdentity *b) +{ + return memcmp(a, b, sizeof(*a)) == 0; +} + /** * Counts the number of occurrences of a given character. * @param str String to evaluate. @@ -80,6 +92,18 @@ char *pid2str(struct PortIdentity *id); char *portaddr2str(struct PortAddress *addr); +/** + * Compare two port identities for equality. + * + * @param a First port identity. + * @param b Second port identity. + * @return 1 if identities are equal, 0 otherwise. + */ +static inline int pid_eq(struct PortIdentity *a, struct PortIdentity *b) +{ + return memcmp(a, b, sizeof(*a)) == 0; +} + /** * Scan a string containing a MAC address and convert it into binary form. *