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 <michael@walle.cc>
master
Michael Walle 2018-07-10 11:31:45 +02:00 committed by Richard Cochran
parent 427f1b3981
commit 8f484e8312
5 changed files with 23 additions and 9 deletions

View File

@ -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;

15
msg.c
View File

@ -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)

11
msg.h
View File

@ -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.

View File

@ -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)

2
pmc.c
View File

@ -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);