HP85 GPIB Disk Emulator  1.0
HP85GPIBDiskEmulator
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
queue.c
Go to the documentation of this file.
1 
23 #include "user_config.h"
24 
25 #ifdef AVR
26 #include <stdlib.h>
27 #endif
28 
29 #include "queue.h"
30 
36 queue_t *queue_new(size_t size)
37 {
38  queue_t *q = safecalloc( sizeof(queue_t),1);
39  if(!q)
40  return(NULL);
41  q->buf = safecalloc(size+1,1);
42  if(!q->buf)
43  {
44  safefree(q);
45  return(NULL);
46  }
47  q->in = 0;
48  q->out = 0;
49  q->bytes = 0;
50  q->size = size;
51  q->flags = 0;
52  return(q);
53 }
54 
55 
62 {
63  if(!q)
64  return;
65  if(q->buf)
66  {
67  safefree(q->buf);
68 // This clear help prevents a freed pointer from being used by mistake
69 // can be removed in production
70  q->buf = NULL;
71  q->in = 0;
72  q->out = 0;
73  q->bytes = 0;
74  q->size = 0;
75  q->flags = 0;
76  }
77  safefree(q);
78 }
79 
80 
87 {
88  if(!q)
89  return;
90  q->in = 0;
91  q->out = 0;
92  q->bytes = 0;
93  q->flags = 0;
94 }
95 
96 
102 size_t queue_used(queue_t *q)
103 {
104  if(!q || !q->buf)
105  return(0);
106  return(q->bytes);
107 }
108 
109 
116 {
117  if(!q || !q->buf)
118  return(1);
119  if(!q->bytes)
120  return(1);
121  return(0);
122 }
123 
124 
131 {
132  if(!q || !q->buf)
133  return(0);
134  return(q->size - q->bytes);
135 }
136 
137 
143 size_t queue_full(queue_t *q)
144 {
145  if(!q || !q->buf)
146  return(0);
147  return(queue_space(q) ? 0 : 1);
148 }
149 
150 
161 size_t queue_push_buffer(queue_t *q, uint8_t *src, size_t size)
162 {
163  size_t bytes = 0;
164 
165  if(!q || !q->buf)
166  return(0);
167 
168  while(size && queue_space(q) )
169  {
170  if(q->in >= q->size)
171  q->in = 0;
172  q->buf[q->in++] = *src++;
173  ++q->bytes;
174  --size;
175  ++bytes;
176  }
177  return(bytes);
178 }
179 
180 
191 size_t queue_pop_buffer(queue_t *q, uint8_t *dst, size_t size)
192 {
193  size_t bytes = 0;
194 
195  if(!q || !q->buf)
196  return(0);
197 
198  while(size && q->bytes)
199  {
200  *dst++ = q->buf[q->out++];
201  if(q->out >= q->size)
202  q->out = 0;
203  --q->bytes;
204  --size;
205  ++bytes;
206  }
207  return(bytes);
208 }
209 
210 
221 int queue_pushc(queue_t *q, uint8_t c)
222 {
223  if(!q || !q->buf)
224  return(0);
225 
226  if(q->bytes >= q->size)
227  {
228  q->flags |= QUEUE_OVERRUN;
229  return(0);
230  }
231  if(q->in >= q->size)
232  q->in = 0;
233 
234  if(c == '\n') // st EOL flasg when we see one
235  q->flags |= QUEUE_EOL;
236  q->buf[q->in++] = c;
237  ++q->bytes;
238  return(1);
239 }
240 
241 
251 {
252  uint8_t c;
253  if(!q || !q->buf)
254  return(0);
255 
256  if(q->bytes)
257  {
258  c = q->buf[q->out];
259  if(c == '\n') // reset EOL flag after a read
260  q->flags &= ~QUEUE_EOL;
261  if(++q->out >= q->size)
262  q->out = 0;
263  q->bytes--;
264  return(c);
265  }
266  return(0);
267 }
queue_t::buf
char * buf
Definition: queue.h:37
QUEUE_OVERRUN
#define QUEUE_OVERRUN
Definition: queue.h:31
queue_popc
int queue_popc(queue_t *q)
Remove a byte from the ring buffer Note: This function does not wait/block util there is data to meet...
Definition: queue.c:250
safecalloc
void * safecalloc(int size, int elements)
Safe Alloc - Display Error message if Calloc fails.
Definition: ram.c:122
dst
dst_t dst
DST start and stop in GMT epoch.
Definition: time.c:52
safefree
void safefree(void *p)
Safe free - Only free a pointer if it is in malloc memory range.
Definition: ram.c:158
queue_full
size_t queue_full(queue_t *q)
Is the ring buffer full ?
Definition: queue.c:143
queue_pushc
int queue_pushc(queue_t *q, uint8_t c)
Add a byte to the ring buffer Note: This function does not wait/block util there is enough free space...
Definition: queue.c:221
queue_used
size_t queue_used(queue_t *q)
Find the number of bytes used by the ring buffer.
Definition: queue.c:102
queue_t::flags
uint8_t flags
Definition: queue.h:38
queue_t::in
size_t in
Definition: queue.h:39
NULL
#define NULL
Definition: user_config.h:85
queue_flush
void queue_flush(queue_t *q)
Flush ring buffer.
Definition: queue.c:86
queue_del
void queue_del(queue_t *q)
Delete a ring buffer and free memory.
Definition: queue.c:61
queue_t
queue structure
Definition: queue.h:35
queue_t::out
size_t out
Definition: queue.h:40
queue.h
Ring buffer code.
queue_t::size
size_t size
Definition: queue.h:42
queue_new
queue_t * queue_new(size_t size)
Create a ring buffer of a given size.
Definition: queue.c:36
queue_pop_buffer
size_t queue_pop_buffer(queue_t *q, uint8_t *dst, size_t size)
Get a data buffer from the ring buffer. Note: This function does not wait/block until there is enough...
Definition: queue.c:191
QUEUE_EOL
#define QUEUE_EOL
Definition: queue.h:32
queue_t::bytes
size_t bytes
Definition: queue.h:41
queue_space
size_t queue_space(queue_t *q)
Find the amount of free space remaining in the ring buffer.
Definition: queue.c:130
queue_empty
size_t queue_empty(queue_t *q)
Is the ring buffer empty ?
Definition: queue.c:115
queue_push_buffer
size_t queue_push_buffer(queue_t *q, uint8_t *src, size_t size)
Add a data buffer to the ring buffer Note: This function does not wait/block util there is enough fre...
Definition: queue.c:161