View of xos/drivers/tty_queue.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include "tty_queue_struct.h"
 
#define prev(pos) ((pos) == 0 ? TTY_BUFFER_SIZE - 1 : (pos) - 1)
#define next(pos) ((pos) == TTY_BUFFER_SIZE - 1 ? 0 : (pos) + 1)
 
#define is_empty(tty_queue) ((tty_queue)->end == (tty_queue)->start)
#define is_full(tty_queue)  (next((tty_queue)->end) == (tty_queue)->start)
 
#define is_in_bounds(tty_queue, pos) ((tty_queue)->end >= (tty_queue)->start ? (pos) >= (tty_queue)->start && (pos) < (tty_queue)->end : (pos) >= (tty_queue)->start || (pos) < (tty_queue)->end)
 
void tty_queue_init(struct tty_queue_struct *tty_queue)
{
  tty_queue->end = tty_queue->start = 0;
}
 
unsigned int tty_queue_get_end(const struct tty_queue_struct *tty_queue)
{
  return tty_queue->end;
}
 
int tty_queue_is_empty(const struct tty_queue_struct *tty_queue)
{
  return is_empty(tty_queue);
}
 
int tty_queue_is_full(const struct tty_queue_struct *tty_queue)
{
  return is_full(tty_queue);
}
 
/* Retourne 1 en cas de succes, 0 en cas d'echec (file vide). */
int tty_queue_get_last(const struct tty_queue_struct *tty_queue, char *c)
{
  if (is_empty(tty_queue))
    return 0;
  *c = tty_queue->buffer[prev(tty_queue->end)];
  return 1;
}
 
/* Retourne 1 en cas de succes, 0 en cas d'echec (position invalide).
 * La position est incrementee dans la file. */
int tty_queue_get(const struct tty_queue_struct *tty_queue, unsigned int *pos, char *c)
{
  *pos &= TTY_BUFFER_SIZE - 1;
  if (!is_in_bounds(tty_queue, *pos))
    return 0;
  *c = tty_queue->buffer[*pos];
  *pos = next(*pos);
  return 1;
}
 
/* Retourne 1 en cas de succes, 0 en cas d'echec (file pleine). */
int tty_queue_push(struct tty_queue_struct *tty_queue, char c)
{
  if (is_full(tty_queue))
    return 0;
  tty_queue->buffer[tty_queue->end] = c;
  tty_queue->end = next(tty_queue->end);
  return 1;
}
 
/* Retourne 1 en cas de succes, 0 en cas d'echec (file vide). */
int tty_queue_pop(struct tty_queue_struct *tty_queue, char *c)
{
  if (is_empty(tty_queue))
    return 0;
  *c = tty_queue->buffer[tty_queue->start];
  tty_queue->start = next(tty_queue->start);
  return 1;
}
 
void tty_queue_del(struct tty_queue_struct *tty_queue)
{
  if (is_empty(tty_queue))
    return;
  tty_queue->end = prev(tty_queue->end);
}
 
void tty_queue_clear(struct tty_queue_struct *tty_queue)
{
  tty_queue->start = tty_queue->end;
}