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 RTEMS Performance Monitoring and Measurement Framework.
0007  *
0008  * This is the Target Interface Command Line Interface. You need
0009  * to start the RTEMS monitor.
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 2014.  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include <rtems.h>
0042 #include <rtems/score/assert.h>
0043 #include "capture_buffer.h"
0044 
0045 void*
0046 rtems_capture_buffer_allocate (rtems_capture_buffer* buffer, size_t size)
0047 {
0048   void* ptr = NULL;
0049 
0050   if ((buffer->count + size) <= buffer->end)
0051   {
0052     size_t end;
0053 
0054     /*
0055      * Determine if the end of free space is marked with the end of buffer
0056      * space, or the head of allocated space.
0057      *
0058      * |...|head| freespace |tail| ...| end
0059      *
0060      * tail|.....|head| freespace| end
0061      */
0062     if (buffer->tail > buffer->head)
0063     {
0064       end = buffer->tail;
0065     } else
0066     {
0067       end = buffer->end;
0068     }
0069 
0070     /*
0071      * Can we allocate it easily?
0072      */
0073     if ((buffer->head + size) <= end)
0074     {
0075       ptr = &buffer->buffer[buffer->head];
0076       buffer->head += size;
0077       buffer->count = buffer->count + size;
0078       if (buffer->max_rec < size)
0079         buffer->max_rec = size;
0080     }
0081     else
0082     {
0083       /*
0084        * We have to consider wrapping around to the front of the buffer
0085        *
0086        * If there is no room at the end of the buffer and we have we already
0087        * wrapped then we can't allocate and if there is room at the front of
0088        * the buffer.
0089        */
0090       if ((end != buffer->tail) && (buffer->tail >= size))
0091       {
0092         /*
0093          * Change the end pointer to the last used byte, so a read will wrap
0094          * when out of data
0095          */
0096         buffer->end = buffer->head;
0097 
0098         /*
0099          * Now return the buffer
0100          */
0101         ptr = buffer->buffer;
0102         buffer->head = size;
0103         buffer->count = buffer->count + size;
0104         if (buffer->max_rec < size)
0105           buffer->max_rec = size;
0106       }
0107     }
0108   }
0109 
0110   return ptr;
0111 }
0112 
0113 void*
0114 rtems_capture_buffer_free (rtems_capture_buffer* buffer, size_t size)
0115 {
0116   void*  ptr;
0117   size_t next;
0118   size_t buff_size;
0119 
0120   if (size == 0)
0121     return NULL;
0122 
0123   ptr = rtems_capture_buffer_peek (buffer, &buff_size);
0124   next = buffer->tail + size;
0125 
0126   /*
0127    * Check if we are freeing space past the end of the buffer
0128    */
0129   _Assert (! rtems_capture_buffer_is_empty( buffer));
0130   _Assert (!((buffer->tail > buffer->head) && (next > buffer->end)));
0131   _Assert (!((buffer->tail < buffer->head) && (next > buffer->head)));
0132 
0133   buffer->count = buffer->count - size;
0134 
0135   if (next == buffer->end)
0136   {
0137     buffer->end = buffer->size;
0138     buffer->tail = 0;
0139   } else
0140   {
0141     buffer->tail = next;
0142   }
0143 
0144   return ptr;
0145 }