Skip to content

Commit d1a3fe2

Browse files
david-laightdavem330
authored andcommitted
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>
1 parent b9420e1 commit d1a3fe2

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

‎include/net/sctp/command.h‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ typedef struct {
203203

204204
typedef struct {
205205
sctp_cmd_t cmds[SCTP_MAX_NUM_COMMANDS];
206-
__u8 next_free_slot;
207-
__u8 next_cmd;
206+
sctp_cmd_t *last_used_slot;
207+
sctp_cmd_t *next_cmd;
208208
} sctp_cmd_seq_t;
209209

210210

@@ -213,8 +213,9 @@ typedef struct {
213213
*/
214214
static inline int sctp_init_cmd_seq(sctp_cmd_seq_t *seq)
215215
{
216-
seq->next_free_slot = 0;
217-
seq->next_cmd = 0;
216+
/* cmds[] is filled backwards to simplify the overflow BUG() check */
217+
seq->last_used_slot = seq->cmds + SCTP_MAX_NUM_COMMANDS;
218+
seq->next_cmd = seq->last_used_slot;
218219
return 1; /* We always succeed. */
219220
}
220221

@@ -227,23 +228,24 @@ static inline int sctp_init_cmd_seq(sctp_cmd_seq_t *seq)
227228
static inline void sctp_add_cmd_sf(sctp_cmd_seq_t *seq, sctp_verb_t verb,
228229
sctp_arg_t obj)
229230
{
230-
BUG_ON(seq->next_free_slot >= SCTP_MAX_NUM_COMMANDS);
231+
sctp_cmd_t *cmd = seq->last_used_slot - 1;
231232

232-
seq->cmds[seq->next_free_slot].verb = verb;
233-
seq->cmds[seq->next_free_slot++].obj = obj;
233+
BUG_ON(cmd < seq->cmds);
234+
235+
cmd->verb = verb;
236+
cmd->obj = obj;
237+
seq->last_used_slot = cmd;
234238
}
235239

236240
/* Return the next command structure in an sctp_cmd_seq.
237241
* Return NULL at the end of the sequence.
238242
*/
239243
static inline sctp_cmd_t *sctp_next_cmd(sctp_cmd_seq_t *seq)
240244
{
241-
sctp_cmd_t *retval = NULL;
242-
243-
if (seq->next_cmd < seq->next_free_slot)
244-
retval = &seq->cmds[seq->next_cmd++];
245+
if (seq->next_cmd <= seq->last_used_slot)
246+
return NULL;
245247

246-
return retval;
248+
return --seq->next_cmd;
247249
}
248250

249251
#endif /* __net_sctp_command_h__ */

0 commit comments

Comments
 (0)