RTEMS CPU Kit with SuperCore  4.11.3
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  The license and distribution terms for this file may be
16  found in the file LICENSE in this distribution.
17 
18  This software with is provided ``as is'' and with NO WARRANTY.
19 
20  ------------------------------------------------------------------------
21 */
22 
23 #ifndef __CAPTUREBUFFER_H_
24 #define __CAPTUREBUFFER_H_
25 
26 #include <stdlib.h>
27 
28 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 typedef struct {
35  uint8_t *buffer;
36  size_t size;
37  volatile uint32_t count;
38  volatile uint32_t head;
39  volatile uint32_t tail;
40  volatile uint32_t end;
42 
43 static inline void rtems_capture_buffer_flush( rtems_capture_buffer_t* buffer )
44 {
45  buffer->end = buffer->size;
46  buffer->head = buffer->tail = 0;
47  buffer->count = 0;
48 }
49 
50 static inline void rtems_capture_buffer_create( rtems_capture_buffer_t* buffer, size_t size )
51 {
52  buffer->buffer = malloc(size);
53  buffer->size = size;
54  rtems_capture_buffer_flush( buffer );
55 }
56 
57 static inline void rtems_capture_buffer_destroy( rtems_capture_buffer_t* buffer )
58 {
59  rtems_capture_buffer_flush( buffer );
60  free( buffer->buffer);
61  buffer->buffer = NULL;
62 }
63 
64 static inline bool rtems_capture_buffer_is_empty( rtems_capture_buffer_t* buffer )
65 {
66  return( buffer->count == 0 );
67 }
68 
69 static inline bool rtems_capture_buffer_is_full( rtems_capture_buffer_t* buffer )
70 {
71  return (buffer->count == buffer->size);
72 }
73 
74 static inline bool rtems_capture_buffer_has_wrapped( rtems_capture_buffer_t* buffer )
75 {
76  if ( buffer->tail > buffer->head)
77  return true;
78 
79  return false;
80 }
81 
82 static inline void *rtems_capture_buffer_peek( rtems_capture_buffer_t* buffer, size_t *size )
83 {
84  if (rtems_capture_buffer_is_empty(buffer)) {
85  *size = 0;
86  return NULL;
87  }
88 
89  if ( buffer->tail > buffer->head)
90  *size = buffer->end - buffer->tail;
91  else
92  *size = buffer->head - buffer->tail;
93 
94  return &buffer->buffer[ buffer->tail ];
95 }
96 
97 void *rtems_capture_buffer_allocate( rtems_capture_buffer_t* buffer, size_t size );
98 
99 void *rtems_capture_buffer_free( rtems_capture_buffer_t* buffer, size_t size );
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif
Definition: capture_buffer.h:34