aboutsummaryrefslogtreecommitdiff
path: root/dcb.c
blob: e2c1e4c11abd9cf5ece017ebe634f961ac6b439a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * MPX OS
 * Operating Systems - Fall 2013
 * Averill Morash, Ben Burwell
 */

#include "mpx.h"

/**
 * Returns the character at the front of the ring queue
 * associated with the dcb, or '\0' if the queue is empty.
 */
char dcb_dequeue(dcb *d) {
  char c;

  if (d->ring_count == 0) {
	return '\0';
  }

  c = d->ring[d->ring_front];

  if (d->ring_front == d->ring_size - 1) {
	d->ring_front = 0;
  } else {
	d->ring_front++;
  }

  d->ring_count--;

  return c;
}

/**
 * Adds the given character to the end of the ring queue
 * in the given dcb if there is room
 */
void dcb_enqueue(dcb *d, char c) {
  if (d->ring_count >= d->ring_size) {
	return;
  }

  if (d->ring_rear == d->ring_size - 1) {
	d->ring_rear = 0;
  } else {
	d->ring_rear++;
  }

  d->ring[d->ring_rear] = c;

  d->ring_count++;

  return;
}

/**
 * Set up a dcb's circular queue, and set the current_op to NO_OP
 */
void dcb_init(dcb *d) {
  d->ring_front = 0;
  d->ring_rear = -1;
  d->ring_size = INPUT_BUFFER_MAX;
  d->ring_count = 0;
  d->current_op = NO_OP;
  return;
}