net: sctp: Use pointers (not array indexes) to access sctp_cmd_seq_t.cmds[].

Using pointers into sctp_cmd_seq_t.cmds[] lets the compiler generate much
better code.
Use the last entry first to optimise the overflow check.

Signed-off-by: David Laight <david.laight@aculab.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
David Laight 2014-07-04 14:35:57 +00:00 committed by David S. Miller
parent b9420e1c87
commit d1a3fe26e9

View file

@ -203,8 +203,8 @@ typedef struct {
typedef struct { typedef struct {
sctp_cmd_t cmds[SCTP_MAX_NUM_COMMANDS]; sctp_cmd_t cmds[SCTP_MAX_NUM_COMMANDS];
__u8 next_free_slot; sctp_cmd_t *last_used_slot;
__u8 next_cmd; sctp_cmd_t *next_cmd;
} sctp_cmd_seq_t; } sctp_cmd_seq_t;
@ -213,8 +213,9 @@ typedef struct {
*/ */
static inline int sctp_init_cmd_seq(sctp_cmd_seq_t *seq) static inline int sctp_init_cmd_seq(sctp_cmd_seq_t *seq)
{ {
seq->next_free_slot = 0; /* cmds[] is filled backwards to simplify the overflow BUG() check */
seq->next_cmd = 0; seq->last_used_slot = seq->cmds + SCTP_MAX_NUM_COMMANDS;
seq->next_cmd = seq->last_used_slot;
return 1; /* We always succeed. */ return 1; /* We always succeed. */
} }
@ -227,10 +228,13 @@ static inline int sctp_init_cmd_seq(sctp_cmd_seq_t *seq)
static inline void sctp_add_cmd_sf(sctp_cmd_seq_t *seq, sctp_verb_t verb, static inline void sctp_add_cmd_sf(sctp_cmd_seq_t *seq, sctp_verb_t verb,
sctp_arg_t obj) sctp_arg_t obj)
{ {
BUG_ON(seq->next_free_slot >= SCTP_MAX_NUM_COMMANDS); sctp_cmd_t *cmd = seq->last_used_slot - 1;
seq->cmds[seq->next_free_slot].verb = verb; BUG_ON(cmd < seq->cmds);
seq->cmds[seq->next_free_slot++].obj = obj;
cmd->verb = verb;
cmd->obj = obj;
seq->last_used_slot = cmd;
} }
/* Return the next command structure in an sctp_cmd_seq. /* Return the next command structure in an sctp_cmd_seq.
@ -238,12 +242,10 @@ static inline void sctp_add_cmd_sf(sctp_cmd_seq_t *seq, sctp_verb_t verb,
*/ */
static inline sctp_cmd_t *sctp_next_cmd(sctp_cmd_seq_t *seq) static inline sctp_cmd_t *sctp_next_cmd(sctp_cmd_seq_t *seq)
{ {
sctp_cmd_t *retval = NULL; if (seq->next_cmd <= seq->last_used_slot)
return NULL;
if (seq->next_cmd < seq->next_free_slot) return --seq->next_cmd;
retval = &seq->cmds[seq->next_cmd++];
return retval;
} }
#endif /* __net_sctp_command_h__ */ #endif /* __net_sctp_command_h__ */