Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:18

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @brief Capture buffer
0007  *
0008  * This is a set of functions to control a variable length capture
0009  * record buffer.
0010  */
0011 
0012 /*
0013  * COPYRIGHT (c) 2014.
0014  * On-Line Applications Research Corporation (OAR).
0015  *
0016  * Copyright 2016 Chris Johns <chrisj@rtems.org>.
0017  * All rights reserved.
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions
0021  * are met:
0022  * 1. Redistributions of source code must retain the above copyright
0023  *    notice, this list of conditions and the following disclaimer.
0024  * 2. Redistributions in binary form must reproduce the above copyright
0025  *    notice, this list of conditions and the following disclaimer in the
0026  *    documentation and/or other materials provided with the distribution.
0027  *
0028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0038  * POSSIBILITY OF SUCH DAMAGE.
0039  */
0040 
0041 #ifndef __CAPTURE_BUFFER_H_
0042 #define __CAPTURE_BUFFER_H_
0043 
0044 #include <stdlib.h>
0045 
0046 /**@{*/
0047 #ifdef __cplusplus
0048 extern "C" {
0049 #endif
0050 
0051 /**
0052  * Capture buffer. There is one per CPU.
0053  */
0054 typedef struct rtems_capture_buffer {
0055   uint8_t* buffer;       /**< The per cpu buffer. */
0056   size_t   size;         /**< The size of the buffer in bytes. */
0057   size_t   count;        /**< The number of used bytes in the buffer. */
0058   size_t   head;         /**< First record. */
0059   size_t   tail;         /**< Head == Tail for empty. */
0060   size_t   end;          /**< Buffer current end, it may move in. */
0061   size_t   max_rec;      /**< The largest record in the buffer. */
0062 } rtems_capture_buffer;
0063 
0064 static inline void
0065 rtems_capture_buffer_flush (rtems_capture_buffer* buffer)
0066 {
0067   buffer->end = buffer->size;
0068   buffer->head = buffer->tail =  0;
0069   buffer->count = 0;
0070   buffer->max_rec = 0;
0071 }
0072 
0073 static inline void
0074 rtems_capture_buffer_create (rtems_capture_buffer* buffer, size_t size)
0075 {
0076   buffer->buffer = malloc(size);
0077   buffer->size = size;
0078   rtems_capture_buffer_flush (buffer);
0079 }
0080 
0081 static inline void
0082 rtems_capture_buffer_destroy (rtems_capture_buffer*  buffer)
0083 {
0084   rtems_capture_buffer_flush (buffer);
0085   free (buffer->buffer);
0086   buffer->buffer = NULL;
0087 }
0088 
0089 static inline bool
0090 rtems_capture_buffer_is_empty (rtems_capture_buffer* buffer)
0091 {
0092    return buffer->count == 0;
0093 }
0094 
0095 static inline bool
0096 rtems_capture_buffer_is_full (rtems_capture_buffer* buffer)
0097 {
0098    return buffer->count == buffer->size;
0099 }
0100 
0101 static inline bool
0102 rtems_capture_buffer_has_wrapped (rtems_capture_buffer* buffer)
0103 {
0104   if (buffer->tail > buffer->head)
0105     return true;
0106 
0107   return false;
0108 }
0109 
0110 static inline void*
0111 rtems_capture_buffer_peek (rtems_capture_buffer* buffer, size_t* size)
0112 {
0113   if (rtems_capture_buffer_is_empty (buffer))
0114   {
0115     *size = 0;
0116     return NULL;
0117   }
0118 
0119   if (buffer->tail > buffer->head)
0120     *size = buffer->end - buffer->tail;
0121   else
0122     *size = buffer->head - buffer->tail;
0123 
0124   return &buffer->buffer[buffer->tail];
0125 }
0126 
0127 void* rtems_capture_buffer_allocate (rtems_capture_buffer* buffer, size_t size);
0128 
0129 void* rtems_capture_buffer_free (rtems_capture_buffer* buffer, size_t size);
0130 
0131 #ifdef __cplusplus
0132 }
0133 #endif
0134 
0135 #endif