/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/RingBuffer.c

  • Committer: Gustav Hartvigsson
  • Date: 2016-09-07 20:42:04 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20160907204204-s97ifo5zh3csfrlc
* Added Better comments to SRingBuffer
* Fixed indentation
* Added skeleton for a basic console/terminal manipulation... Nothing fancy like ncurs..

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "RingBuffer.h"
2
2
 
3
 
 
4
 
#define err_on_full(S) {\
 
3
/* These are just to reduce code redundancy.
 
4
 */
 
5
#define err_on_full(S, E) {\
5
6
  if (self->len >= S->size) {\
6
 
    *err = TRUE;\
 
7
    E = TRUE;\
7
8
    s_err_print ("Buffer full. Needs to be reallocated.\n"\
8
9
                 "    (This is not done automatically. Returning.)");\
9
10
    return;\
10
11
  }\
11
12
}
12
13
 
13
 
#define err_seems_full() {\
 
14
#define err_seems_full(E) {\
14
15
  s_err_print ("Array seems to be full... Returning.");\
15
 
  *err = TRUE;\
 
16
  E = TRUE;\
16
17
  return;\
17
18
}
18
19
 
19
 
#define nothing_to_ret() {\
20
 
  s_warn_print ("Ring buffer is empty, nothing to return");\
21
 
  *err = TRUE;\
22
 
  return 0;\
 
20
#define err_on_empty(S, E) {\
 
21
  if (s_ring_buffer_is_empty (S)) {\
 
22
    s_warn_print ("Ring buffer is empty, nothing to return");\
 
23
    E = TRUE;\
 
24
    return 0;\
 
25
  }\
23
26
}
24
27
 
 
28
 
25
29
struct SRingBuffer {
26
 
  size_t size;
27
 
  size_t start;
28
 
  size_t end;
29
 
  size_t len;
30
 
  
31
 
  sbyte * array;
 
30
  size_t size;   /* The number of elemets in the array */
 
31
  size_t len;    /* The "length" of the buffered data */
 
32
  
 
33
  size_t start;  /* The index of the "first" element in the buffer in the array
 
34
                  */
 
35
  size_t end;    /* The index of the "last" element in the buffer in the array
 
36
                  */
 
37
  
 
38
  sbyte * array; /* The array that is used to implement the buffer */
32
39
}
33
40
 
34
41
 
54
61
 
55
62
sboolean
56
63
s_ring_buffer_is_empty (SRingBuffer * self) {
 
64
  /* If the data data two pointers are the same and the the length is zero,
 
65
   * the array is most likely empty... */
57
66
  return ((self->start == self->end) && self->len == 0 );
58
67
}
59
68
 
62
71
                    sbyte data,
63
72
                    sboolean * err) {
64
73
  *err = FALSE;
65
 
  err_on_full (self);
66
 
  
 
74
  err_on_full (self, *err);
 
75
  /* The buffer should only have three different states so this should do.
 
76
   *    (exept there are five states... Idiot...)
 
77
   *
 
78
   * The first state here is the normal state where the end is after the start.
 
79
   *
 
80
   * The second state is when the end pointer is at the end of the array.
 
81
   *   This has two substates, one where the array is full (the start pointer
 
82
   *   is zero and the end pointer points to the last lement of the array), the
 
83
   *   second state is when the stat pointer does not point to zero and is thus
 
84
   *   free to use so we set the end pointer to zero.
 
85
   *
 
86
   * The third state is when the array loops back on itself (see headerfile).
 
87
   *   This has two substates, one where the next element that should be set is
 
88
   *   actially the start element, thus the array is full. The second substates
 
89
   *   is when that is not true and we can use that slot.
 
90
   */
67
91
  if (self->end <= self->size) {
68
92
    self->end = self->end + 1;
69
93
  } else if (self->end == self->size) {
70
94
    if (self->start == 0) {
71
 
      err_seems_full ();
 
95
      err_seems_full (*err);
72
96
    } else {
73
97
      self->end = 0;
74
98
    }
75
99
  } else if (self->end < self->start) {
76
100
    if ((self->end + 1) == self->start ) {
77
 
      err_seems_full ();
 
101
      err_seems_full (*err);
78
102
    } else {
79
103
      self->end = self->end + 1;
80
104
    }
81
105
  } else {
 
106
    /* This is an unknown state... some bad shit has gone down if you end up
 
107
    * here */
82
108
    s_err_print ("Reaching a place that should not be reached. Returning");
83
109
    *err = TRUE;
84
110
    return;
93
119
                          sbyte data,
94
120
                          sboolean * err) {
95
121
  *err = FALSE;
96
 
  err_on_full (self);
 
122
  err_on_full (self, *err);
97
123
  
98
 
  if ((self->start - 1) < 0) {
 
124
  /* This is pretty much the same as s_ring_buffer_push (), save for the states
 
125
   * beeing in a different order order is different to make it actially work as
 
126
   * inteded, I think.
 
127
   *
 
128
   * LOL.
 
129
   */
 
130
  if (self->start == 0) {
99
131
    if (self->end == self->size) {
100
 
      err_seems_full ();
 
132
      err_seems_full (*err);
101
133
    } else {
102
134
      self->start = self->size;
103
135
    }
104
136
  } else if (self->start <= self-end) {
105
137
    self->start = self->start - 1;
106
 
  } else  if (self->start > self->end) {
 
138
  } else if (self->start > self->end) {
107
139
    if ((self->start - 1) == self->end ) {
108
 
      err_seems_full ();
 
140
      err_seems_full (*err);
109
141
    } else {
110
142
      self->start = self->start - 1;
111
143
    }
120
152
 
121
153
/*
122
154
 * When doing poping we do not override the data in the buffer.
123
 
 * This to alleviate some prosessing time form usage of the data structure.
 
155
 * This to alleviate some processing time form usage of the data structure.
124
156
 */
125
157
sbyte
126
158
s_ring_buffer_pop (SRingBuffer * self, sboolean * err) {
127
 
  if (s_ring_buffer_is_empty (self)) {
128
 
    nothing_to_ret ();
129
 
  }
130
 
  *err = FALSE;
 
159
  err_on_empty (self, *err);
131
160
  sbyte ret_val = self->array[self->end];
132
161
  self->len = self->len - 1;
133
162
  self->end = self->end - 1;
137
166
 
138
167
sbyte
139
168
s_ring_buffer_pop_front (SRingBuffer * self, sboolean * err) {
140
 
  if (s_ring_buffer_is_empty (self)) {
141
 
    nothing_to_ret ();
142
 
  }
 
169
  err_on_empty (self, *err);
143
170
  *err = FALSE;
144
171
  sbyte ret_val = self->array[self->start];
145
172
  self->start = self->start + 1;
150
177
 
151
178
sbyte
152
179
s_ring_buffer_peek (SRingBuffer * self, sboolean * err) {
153
 
  if (s_ring_buffer_is_empty (self)) {
154
 
    nothing_to_ret ();
155
 
  }
 
180
  err_on_empty (self, *err);
156
181
  *err = FALSE;
157
182
  return self->array[self->end];
158
183
}
159
184
 
160
185
sbyte
161
186
s_ring_buffer_peek_front (SRingBuffer * self, sboolean * err) {
162
 
  if (s_ring_buffer_is_empty (self)) {
163
 
    nothing_to_ret ();
164
 
  }
 
187
  err_on_empty (self, *err);
165
188
  *err = FALSE;
166
189
  return self->array[self->start];
167
190
}
188
211
s_ring_buffer_for_each (SRingBuffer * self,
189
212
                        ForEachFunc func,
190
213
                        spointer user_data,
191
 
                        sboolean * err);
 
214
                        sboolean * err) {
 
215
  err_on_empty (self, *err);
 
216
  
 
217
}
192
218