n_tty: Use separate head and tail indices for echo_buf

Instead of using a single index to track the current echo_buf position,
use a head index when adding to the buffer and a tail index when
consuming from the buffer. Allow these head and tail indices to wrap
at max representable value; perform modulo reduction via helper
functions when accessing the buffer.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Peter Hurley 2013-06-15 10:04:22 -04:00 коммит произвёл Greg Kroah-Hartman
Родитель ae56f33041
Коммит addaebccf6
1 изменённых файлов: 35 добавлений и 53 удалений

Просмотреть файл

@ -108,7 +108,8 @@ struct n_tty_data {
/* protected by echo_lock */ /* protected by echo_lock */
unsigned char *echo_buf; unsigned char *echo_buf;
unsigned int echo_pos; size_t echo_head;
size_t echo_tail;
unsigned int echo_cnt; unsigned int echo_cnt;
/* protected by output lock */ /* protected by output lock */
@ -135,6 +136,16 @@ static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)]; return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
} }
static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
{
return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
}
static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
{
return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
}
static inline int tty_put_user(struct tty_struct *tty, unsigned char x, static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
unsigned char __user *ptr) unsigned char __user *ptr)
{ {
@ -325,7 +336,7 @@ static void reset_buffer_flags(struct n_tty_data *ldata)
ldata->read_head = ldata->canon_head = ldata->read_tail = 0; ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
mutex_lock(&ldata->echo_lock); mutex_lock(&ldata->echo_lock);
ldata->echo_pos = ldata->echo_cnt = 0; ldata->echo_head = ldata->echo_tail = ldata->echo_cnt = 0;
mutex_unlock(&ldata->echo_lock); mutex_unlock(&ldata->echo_lock);
ldata->erasing = 0; ldata->erasing = 0;
@ -640,8 +651,8 @@ static void process_echoes(struct tty_struct *tty)
{ {
struct n_tty_data *ldata = tty->disc_data; struct n_tty_data *ldata = tty->disc_data;
int space, nr; int space, nr;
size_t tail;
unsigned char c; unsigned char c;
unsigned char *cp, *buf_end;
if (!ldata->echo_cnt) if (!ldata->echo_cnt)
return; return;
@ -651,14 +662,12 @@ static void process_echoes(struct tty_struct *tty)
space = tty_write_room(tty); space = tty_write_room(tty);
buf_end = ldata->echo_buf + N_TTY_BUF_SIZE; tail = ldata->echo_tail;
cp = ldata->echo_buf + ldata->echo_pos;
nr = ldata->echo_cnt; nr = ldata->echo_cnt;
while (nr > 0) { while (nr > 0) {
c = *cp; c = echo_buf(ldata, tail);
if (c == ECHO_OP_START) { if (c == ECHO_OP_START) {
unsigned char op; unsigned char op;
unsigned char *opp;
int no_space_left = 0; int no_space_left = 0;
/* /*
@ -666,18 +675,13 @@ static void process_echoes(struct tty_struct *tty)
* operation, get the next byte, which is either the * operation, get the next byte, which is either the
* op code or a control character value. * op code or a control character value.
*/ */
opp = cp + 1; op = echo_buf(ldata, tail + 1);
if (opp == buf_end)
opp -= N_TTY_BUF_SIZE;
op = *opp;
switch (op) { switch (op) {
unsigned int num_chars, num_bs; unsigned int num_chars, num_bs;
case ECHO_OP_ERASE_TAB: case ECHO_OP_ERASE_TAB:
if (++opp == buf_end) num_chars = echo_buf(ldata, tail + 2);
opp -= N_TTY_BUF_SIZE;
num_chars = *opp;
/* /*
* Determine how many columns to go back * Determine how many columns to go back
@ -703,20 +707,20 @@ static void process_echoes(struct tty_struct *tty)
if (ldata->column > 0) if (ldata->column > 0)
ldata->column--; ldata->column--;
} }
cp += 3; tail += 3;
nr -= 3; nr -= 3;
break; break;
case ECHO_OP_SET_CANON_COL: case ECHO_OP_SET_CANON_COL:
ldata->canon_column = ldata->column; ldata->canon_column = ldata->column;
cp += 2; tail += 2;
nr -= 2; nr -= 2;
break; break;
case ECHO_OP_MOVE_BACK_COL: case ECHO_OP_MOVE_BACK_COL:
if (ldata->column > 0) if (ldata->column > 0)
ldata->column--; ldata->column--;
cp += 2; tail += 2;
nr -= 2; nr -= 2;
break; break;
@ -729,7 +733,7 @@ static void process_echoes(struct tty_struct *tty)
tty_put_char(tty, ECHO_OP_START); tty_put_char(tty, ECHO_OP_START);
ldata->column++; ldata->column++;
space--; space--;
cp += 2; tail += 2;
nr -= 2; nr -= 2;
break; break;
@ -751,7 +755,7 @@ static void process_echoes(struct tty_struct *tty)
tty_put_char(tty, op ^ 0100); tty_put_char(tty, op ^ 0100);
ldata->column += 2; ldata->column += 2;
space -= 2; space -= 2;
cp += 2; tail += 2;
nr -= 2; nr -= 2;
} }
@ -769,24 +773,13 @@ static void process_echoes(struct tty_struct *tty)
tty_put_char(tty, c); tty_put_char(tty, c);
space -= 1; space -= 1;
} }
cp += 1; tail += 1;
nr -= 1; nr -= 1;
} }
/* When end of circular buffer reached, wrap around */
if (cp >= buf_end)
cp -= N_TTY_BUF_SIZE;
} }
if (nr == 0) { ldata->echo_tail = tail;
ldata->echo_pos = 0; ldata->echo_cnt = nr;
ldata->echo_cnt = 0;
} else {
int num_processed = ldata->echo_cnt - nr;
ldata->echo_pos += num_processed;
ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
ldata->echo_cnt = nr;
}
mutex_unlock(&ldata->echo_lock); mutex_unlock(&ldata->echo_lock);
mutex_unlock(&ldata->output_lock); mutex_unlock(&ldata->output_lock);
@ -807,37 +800,26 @@ static void process_echoes(struct tty_struct *tty)
static void add_echo_byte(unsigned char c, struct n_tty_data *ldata) static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
{ {
int new_byte_pos;
if (ldata->echo_cnt == N_TTY_BUF_SIZE) { if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
/* Circular buffer is already at capacity */ size_t head = ldata->echo_head;
new_byte_pos = ldata->echo_pos;
/* /*
* Since the buffer start position needs to be advanced, * Since the buffer start position needs to be advanced,
* be sure to step by a whole operation byte group. * be sure to step by a whole operation byte group.
*/ */
if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) { if (echo_buf(ldata, head) == ECHO_OP_START) {
if (ldata->echo_buf[(ldata->echo_pos + 1) & if (echo_buf(ldata, head + 1) == ECHO_OP_ERASE_TAB) {
(N_TTY_BUF_SIZE - 1)] == ldata->echo_tail += 3;
ECHO_OP_ERASE_TAB) {
ldata->echo_pos += 3;
ldata->echo_cnt -= 2; ldata->echo_cnt -= 2;
} else { } else {
ldata->echo_pos += 2; ldata->echo_tail += 2;
ldata->echo_cnt -= 1; ldata->echo_cnt -= 1;
} }
} else { } else
ldata->echo_pos++; ldata->echo_tail++;
} } else
ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
} else {
new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
new_byte_pos &= N_TTY_BUF_SIZE - 1;
ldata->echo_cnt++; ldata->echo_cnt++;
}
ldata->echo_buf[new_byte_pos] = c; *echo_buf_addr(ldata, ldata->echo_head++) = c;
} }
/** /**