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 <[email protected]>
---
 clock.c   |  2 +-
 msg.c     | 16 +++++++++++++---
 msg.h     | 11 +++++++----
 phc2sys.c |  2 +-
 pmc.c     |  2 +-
 5 files changed, 23 insertions(+), 10 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 e408ea4..f9c4261 100644
--- a/msg.c
+++ b/msg.c
@@ -309,7 +309,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) {
@@ -497,7 +496,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;
 }
 
 void msg_tlv_recycle(struct ptp_message *msg)
@@ -508,7 +519,6 @@ void msg_tlv_recycle(struct ptp_message *msg)
                TAILQ_REMOVE(&msg->tlv_list, extra, list);
                tlv_extra_recycle(extra);
        }
-       msg->tlv_count = 0;
 }
 
 
diff --git a/msg.h b/msg.h
index 6a3509b..58ad37a 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;
 };
 
 /**
@@ -286,6 +282,13 @@ void msg_tlv_attach(struct ptp_message *msg, struct 
tlv_extra *extra);
  */
 void msg_tlv_recycle(struct ptp_message *msg);
 
+/*
+ * 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 6f23167..94a0c2b 100644
--- a/pmc.c
+++ b/pmc.c
@@ -160,7 +160,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, action_string[action]);
-       if (msg->tlv_count != 1) {
+       if (msg_tlv_count(msg) != 1) {
                goto out;
        }
        extra = TAILQ_FIRST(&msg->tlv_list);
-- 
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linuxptp-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to