From 8f484e8312fa8c73e85dfa46ad7850ecc4446075 Mon Sep 17 00:00:00 2001 From: Michael Walle Date: Tue, 10 Jul 2018 11:31:45 +0200 Subject: [PATCH] msg: Drop msg->tlv_count. The field is redundant with the length tlv_list. Replace it with a function msg_tlv_count() instead. This iterates over the tlv_list. The computational overhead should be small, because the lists are very short and the tlv_count is only used in management paths (yet). Signed-off-by: Michael Walle --- clock.c | 2 +- msg.c | 15 +++++++++++++-- msg.h | 11 +++++++---- phc2sys.c | 2 +- pmc.c | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/clock.c b/clock.c index 2400b2f..faf2dea 100644 --- a/clock.c +++ b/clock.c @@ -1319,7 +1319,7 @@ int clock_manage(struct clock *c, struct port *p, struct ptp_message *msg) if (!cid_eq(tcid, &wildcard) && !cid_eq(tcid, &c->dds.clockIdentity)) { return changed; } - if (msg->tlv_count != 1) { + if (msg_tlv_count(msg) != 1) { return changed; } mgt = (struct management_tlv *) msg->management.suffix; diff --git a/msg.c b/msg.c index d5b651f..d1a657c 100644 --- a/msg.c +++ b/msg.c @@ -318,7 +318,6 @@ struct ptp_message *msg_duplicate(struct ptp_message *msg, int cnt) memcpy(dup, msg, sizeof(*dup)); dup->refcnt = 1; TAILQ_INIT(&dup->tlv_list); - dup->tlv_count = 0; err = msg_post_recv(dup, cnt); if (err) { @@ -506,7 +505,19 @@ struct tlv_extra *msg_tlv_append(struct ptp_message *msg, int length) void msg_tlv_attach(struct ptp_message *msg, struct tlv_extra *extra) { TAILQ_INSERT_TAIL(&msg->tlv_list, extra, list); - msg->tlv_count++; +} + +int msg_tlv_count(struct ptp_message *msg) +{ + int count = 0; + struct tlv_extra *extra; + + for (extra = TAILQ_FIRST(&msg->tlv_list); + extra != NULL; + extra = TAILQ_NEXT(extra, list)) + count++; + + return count; } const char *msg_type_string(int type) diff --git a/msg.h b/msg.h index 58a916c..03fe1fd 100644 --- a/msg.h +++ b/msg.h @@ -227,10 +227,6 @@ struct ptp_message { * pointers to the appended TLVs. */ TAILQ_HEAD(tlv_list, tlv_extra) tlv_list; - /** - * Contains the number of TLVs in the suffix. - */ - int tlv_count; }; /** @@ -279,6 +275,13 @@ struct tlv_extra *msg_tlv_append(struct ptp_message *msg, int length); */ void msg_tlv_attach(struct ptp_message *msg, struct tlv_extra *extra); +/* + * Return the number of TLVs attached to a message. + * @param msg A message obtained using @ref msg_allocate(). + * @return The number of attached TLVs. + */ +int msg_tlv_count(struct ptp_message *msg); + /** * Obtain the transportSpecific field from a message. * @param m Message to test. diff --git a/phc2sys.c b/phc2sys.c index 66e34d6..21cae75 100644 --- a/phc2sys.c +++ b/phc2sys.c @@ -819,7 +819,7 @@ static int is_msg_mgt(struct ptp_message *msg) return 0; if (management_action(msg) != RESPONSE) return 0; - if (msg->tlv_count != 1) + if (msg_tlv_count(msg) != 1) return 0; tlv = (struct TLV *) msg->management.suffix; if (tlv->type == TLV_MANAGEMENT) diff --git a/pmc.c b/pmc.c index 35b1d96..440c905 100644 --- a/pmc.c +++ b/pmc.c @@ -80,7 +80,7 @@ static void pmc_show(struct ptp_message *msg, FILE *fp) fprintf(fp, "\t%s seq %hu %s ", pid2str(&msg->header.sourcePortIdentity), msg->header.sequenceId, pmc_action_string(action)); - if (msg->tlv_count != 1) { + if (msg_tlv_count(msg) != 1) { goto out; } extra = TAILQ_FIRST(&msg->tlv_list);