aboutsummaryrefslogtreecommitdiff
path: root/src/Fifo.cpp
blob: c5f0853edfce5e1de797a157aaa5606651b4220c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include "Fifo.h"

Fifo::Fifo ()
{
  astate = 0;			// not allocated
  buffer = NULL;
  totsize = 0;
  datasize = 0;
  start = 0;
  pthread_mutex_init (&mut, 0);
}

Fifo::Fifo (unsigned int size)
{
  buffer = new char[size];
  if (buffer != NULL)
    {
      astate = 1;
      totsize = size;
      datasize = 0;
      start = 0;
    }
  else
    {
      astate = 0;
      totsize = 0;
      datasize = 0;
      start = 0;
    }
  pthread_mutex_init (&mut, 0);
}

Fifo::~Fifo ()
{
  pthread_mutex_lock (&mut);
  if (astate)
    delete buffer;
  pthread_mutex_unlock (&mut);
}

void
Fifo::Flush ()
{
  pthread_mutex_lock (&mut);
  astate = 1;
  //totsize = size;
  datasize = 0;
  start = 0;
  pthread_mutex_unlock (&mut);
  //for (int x =0; x < sizeof(buffer); x++) {
  //  buffer[x] = 0;
  //}
}

int
Fifo::ReAlloc (unsigned int size)
{
  pthread_mutex_lock (&mut);
  if (astate)
    delete buffer;
  buffer = new char[size];
  if (buffer != NULL)
    {
      astate = 1;
      totsize = size;
      datasize = 0;
      start = 0;
      pthread_mutex_unlock (&mut);
    }
  else
    {
      astate = 0;
      totsize = 0;
      datasize = 0;
      start = 0;
      pthread_mutex_unlock (&mut);
      return -1;
    }
  return 0;
}

void *
Fifo::Read (void *buf, unsigned int &len)
{
  pthread_mutex_lock (&mut);
  if (len > datasize)
    len = datasize;
  unsigned int rest;
  if (len > (totsize - start))
    rest = len - (totsize - start);
  else
    rest = 0;
  unsigned int first = len - rest;
  memcpy (buf, buffer + start, first);
  memcpy ((char *) buf + first, buffer, rest);
  datasize -= len;
  start += len;
  if (start >= totsize)
    start = rest;
  //if (datasize == 0) printf("in fifo READ, data is zero\n");
  pthread_mutex_unlock (&mut);
  return buf;
}

int
Fifo::Write (void *buf, unsigned int len)
{
  pthread_mutex_lock (&mut);
  unsigned int end;
  end = start + datasize;
  if (end > totsize)
    end = end - totsize;
  if (len > (totsize - datasize))
    {
      pthread_mutex_unlock (&mut);
      return -1;
    }
  unsigned int rest;
  if ((len + end) > totsize)
    rest = (len + end) - totsize;
  else
    rest = 0;
  unsigned int first = len - rest;
  memcpy (buffer + end, buf, first);
  memcpy (buffer, (char *) buf + first, rest);

  datasize += len;
  //if (datasize == 0) printf("in fifo WRITE, data is zero\n");
  pthread_mutex_unlock (&mut);
  return len;
}

unsigned int
Fifo::FreeSpace (void)
{				// do we need locks here?
  int x;
  pthread_mutex_lock (&mut);
  x = totsize - datasize;
  pthread_mutex_unlock (&mut);
  return x;
}

unsigned int
Fifo::UsedSpace (void)
{
  int x;
  pthread_mutex_lock (&mut);
  x = datasize;
  pthread_mutex_unlock (&mut);
  return x;
}