Skip to content

Commit

Permalink
msg: Drop msg->tlv_count.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mwalle authored and richardcochran committed Jul 21, 2018
1 parent 427f1b3 commit 8f484e8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion phc2sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8f484e8

Please sign in to comment.