RTEMS  5.1
capture_buffer.h
Go to the documentation of this file.
1 
9 /*
10  ------------------------------------------------------------------------
11 
12  COPYRIGHT (c) 2014.
13  On-Line Applications Research Corporation (OAR).
14 
15  Copyright 2016 Chris Johns <chrisj@rtems.org>.
16  All rights reserved.
17 
18  The license and distribution terms for this file may be
19  found in the file LICENSE in this distribution.
20 
21  This software with is provided ``as is'' and with NO WARRANTY.
22 
23  ------------------------------------------------------------------------
24 */
25 
26 #ifndef __CAPTURE_BUFFER_H_
27 #define __CAPTURE_BUFFER_H_
28 
29 #include <stdlib.h>
30 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 typedef struct rtems_capture_buffer {
40  uint8_t* buffer;
41  size_t size;
42  size_t count;
43  size_t head;
44  size_t tail;
45  size_t end;
46  size_t max_rec;
48 
49 static inline void
50 rtems_capture_buffer_flush (rtems_capture_buffer* buffer)
51 {
52  buffer->end = buffer->size;
53  buffer->head = buffer->tail = 0;
54  buffer->count = 0;
55  buffer->max_rec = 0;
56 }
57 
58 static inline void
59 rtems_capture_buffer_create (rtems_capture_buffer* buffer, size_t size)
60 {
61  buffer->buffer = malloc(size);
62  buffer->size = size;
63  rtems_capture_buffer_flush (buffer);
64 }
65 
66 static inline void
67 rtems_capture_buffer_destroy (rtems_capture_buffer* buffer)
68 {
69  rtems_capture_buffer_flush (buffer);
70  free (buffer->buffer);
71  buffer->buffer = NULL;
72 }
73 
74 static inline bool
75 rtems_capture_buffer_is_empty (rtems_capture_buffer* buffer)
76 {
77  return buffer->count == 0;
78 }
79 
80 static inline bool
81 rtems_capture_buffer_is_full (rtems_capture_buffer* buffer)
82 {
83  return buffer->count == buffer->size;
84 }
85 
86 static inline bool
87 rtems_capture_buffer_has_wrapped (rtems_capture_buffer* buffer)
88 {
89  if (buffer->tail > buffer->head)
90  return true;
91 
92  return false;
93 }
94 
95 static inline void*
96 rtems_capture_buffer_peek (rtems_capture_buffer* buffer, size_t* size)
97 {
98  if (rtems_capture_buffer_is_empty (buffer))
99  {
100  *size = 0;
101  return NULL;
102  }
103 
104  if (buffer->tail > buffer->head)
105  *size = buffer->end - buffer->tail;
106  else
107  *size = buffer->head - buffer->tail;
108 
109  return &buffer->buffer[buffer->tail];
110 }
111 
112 void* rtems_capture_buffer_allocate (rtems_capture_buffer* buffer, size_t size);
113 
114 void* rtems_capture_buffer_free (rtems_capture_buffer* buffer, size_t size);
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif
Definition: capture_buffer.h:39
size_t max_rec
Definition: capture_buffer.h:46
uint8_t * buffer
Definition: capture_buffer.h:40
size_t tail
Definition: capture_buffer.h:44
size_t head
Definition: capture_buffer.h:43
size_t size
Definition: capture_buffer.h:41
size_t end
Definition: capture_buffer.h:45
size_t count
Definition: capture_buffer.h:42
struct rtems_capture_buffer rtems_capture_buffer
unsigned size
Definition: tte.h:74
#define NULL
Requests a GPIO pin group configuration.
Definition: bestcomm_api.h:77