RTEMS 6.1-rc4
Loading...
Searching...
No Matches
capture_buffer.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
12/*
13 * COPYRIGHT (c) 2014.
14 * On-Line Applications Research Corporation (OAR).
15 *
16 * Copyright 2016 Chris Johns <chrisj@rtems.org>.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#ifndef __CAPTURE_BUFFER_H_
42#define __CAPTURE_BUFFER_H_
43
44#include <stdlib.h>
45
47#ifdef __cplusplus
48extern "C" {
49#endif
50
54typedef struct rtems_capture_buffer {
55 uint8_t* buffer;
56 size_t size;
57 size_t count;
58 size_t head;
59 size_t tail;
60 size_t end;
61 size_t max_rec;
63
64static inline void
65rtems_capture_buffer_flush (rtems_capture_buffer* buffer)
66{
67 buffer->end = buffer->size;
68 buffer->head = buffer->tail = 0;
69 buffer->count = 0;
70 buffer->max_rec = 0;
71}
72
73static inline void
74rtems_capture_buffer_create (rtems_capture_buffer* buffer, size_t size)
75{
76 buffer->buffer = malloc(size);
77 buffer->size = size;
78 rtems_capture_buffer_flush (buffer);
79}
80
81static inline void
82rtems_capture_buffer_destroy (rtems_capture_buffer* buffer)
83{
84 rtems_capture_buffer_flush (buffer);
85 free (buffer->buffer);
86 buffer->buffer = NULL;
87}
88
89static inline bool
90rtems_capture_buffer_is_empty (rtems_capture_buffer* buffer)
91{
92 return buffer->count == 0;
93}
94
95static inline bool
96rtems_capture_buffer_is_full (rtems_capture_buffer* buffer)
97{
98 return buffer->count == buffer->size;
99}
100
101static inline bool
102rtems_capture_buffer_has_wrapped (rtems_capture_buffer* buffer)
103{
104 if (buffer->tail > buffer->head)
105 return true;
106
107 return false;
108}
109
110static inline void*
111rtems_capture_buffer_peek (rtems_capture_buffer* buffer, size_t* size)
112{
113 if (rtems_capture_buffer_is_empty (buffer))
114 {
115 *size = 0;
116 return NULL;
117 }
118
119 if (buffer->tail > buffer->head)
120 *size = buffer->end - buffer->tail;
121 else
122 *size = buffer->head - buffer->tail;
123
124 return &buffer->buffer[buffer->tail];
125}
126
127void* rtems_capture_buffer_allocate (rtems_capture_buffer* buffer, size_t size);
128
129void* rtems_capture_buffer_free (rtems_capture_buffer* buffer, size_t size);
130
131#ifdef __cplusplus
132}
133#endif
134
135#endif
Definition: capture_buffer.h:54
size_t head
Definition: capture_buffer.h:58
size_t max_rec
Definition: capture_buffer.h:61
size_t tail
Definition: capture_buffer.h:59
size_t count
Definition: capture_buffer.h:57
size_t size
Definition: capture_buffer.h:56
uint8_t * buffer
Definition: capture_buffer.h:55
size_t end
Definition: capture_buffer.h:60