| 1 |
/* mapped-ring-buffer.h |
| 2 |
* |
| 3 |
* Copyright 2020-2025 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* Subject to the terms and conditions of this license, each copyright holder |
| 17 |
* and contributor hereby grants to those receiving rights under this license |
| 18 |
* a perpetual, worldwide, non-exclusive, no-charge, royalty-free, |
| 19 |
* irrevocable (except for failure to satisfy the conditions of this license) |
| 20 |
* patent license to make, have made, use, offer to sell, sell, import, and |
| 21 |
* otherwise transfer this software, where such license applies only to those |
| 22 |
* patent claims, already acquired or hereafter acquired, licensable by such |
| 23 |
* copyright holder or contributor that are necessarily infringed by: |
| 24 |
* |
| 25 |
* (a) their Contribution(s) (the licensed copyrights of copyright holders |
| 26 |
* and non-copyrightable additions of contributors, in source or binary |
| 27 |
* form) alone; or |
| 28 |
* |
| 29 |
* (b) combination of their Contribution(s) with the work of authorship to |
| 30 |
* which such Contribution(s) was added by such copyright holder or |
| 31 |
* contributor, if, at the time the Contribution is added, such addition |
| 32 |
* causes such combination to be necessarily infringed. The patent license |
| 33 |
* shall not apply to any other combinations which include the |
| 34 |
* Contribution. |
| 35 |
* |
| 36 |
* Except as expressly stated above, no rights or licenses from any copyright |
| 37 |
* holder or contributor is granted under this license, whether expressly, by |
| 38 |
* implication, estoppel or otherwise. |
| 39 |
* |
| 40 |
* DISCLAIMER |
| 41 |
* |
| 42 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 43 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 44 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 45 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
| 46 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 47 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 48 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 49 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 50 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 51 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 52 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 53 |
* |
| 54 |
* SPDX-License-Identifier: BSD-2-Clause-Patent |
| 55 |
*/ |
| 56 |
|
| 57 |
#pragma once |
| 58 |
|
| 59 |
#include <stdbool.h> |
| 60 |
#include <stddef.h> |
| 61 |
|
| 62 |
#include "sysprof-macros.h" |
| 63 |
|
| 64 |
SYSPROF_BEGIN_DECLS |
| 65 |
|
| 66 |
typedef struct _MappedRingBuffer MappedRingBuffer; |
| 67 |
|
| 68 |
/** |
| 69 |
* MappedRingBufferCallback: |
| 70 |
* @data: a pointer into the mapped buffer containing the data frame |
| 71 |
* @length: (inout): the number of bytes to advance |
| 72 |
* @user_data: closure data provided to mapped_ring_buffer_drain() |
| 73 |
* |
| 74 |
* Functions matching this prototype will be called from the |
| 75 |
* mapped_ring_buffer_drain() function for each data frame read from the |
| 76 |
* underlying memory mapping. |
| 77 |
* |
| 78 |
* @length is initially set to the max bytes that @data could contain, but |
| 79 |
* should be set by the caller to the amount of bytes known to have been |
| 80 |
* consumed. This allows MappedRingBuffer to avoid storing length data or |
| 81 |
* framing information as that can come from the buffer content. |
| 82 |
* |
| 83 |
* The callback should shorten @length if it knows the frame is less than |
| 84 |
* what was provided. |
| 85 |
* |
| 86 |
* This function can also be used with mapped_ring_buffer_create_source() |
| 87 |
* to automatically drain the ring buffer as part of the main loop progress. |
| 88 |
* |
| 89 |
* Returns: %TRUE to coninue draining, otherwise %FALSE and draining stops |
| 90 |
*/ |
| 91 |
typedef bool (*MappedRingBufferCallback) (const void *data, |
| 92 |
size_t *length, |
| 93 |
void *user_data); |
| 94 |
|
| 95 |
SYSPROF_INTERNAL |
| 96 |
MappedRingBuffer *mapped_ring_buffer_new_reader (size_t buffer_size); |
| 97 |
SYSPROF_INTERNAL |
| 98 |
MappedRingBuffer *mapped_ring_buffer_new_readwrite (size_t buffer_size); |
| 99 |
SYSPROF_INTERNAL |
| 100 |
MappedRingBuffer *mapped_ring_buffer_new_writer (int fd); |
| 101 |
SYSPROF_INTERNAL |
| 102 |
int mapped_ring_buffer_get_fd (MappedRingBuffer *self); |
| 103 |
SYSPROF_INTERNAL |
| 104 |
MappedRingBuffer *mapped_ring_buffer_ref (MappedRingBuffer *self); |
| 105 |
SYSPROF_INTERNAL |
| 106 |
void mapped_ring_buffer_unref (MappedRingBuffer *self); |
| 107 |
SYSPROF_INTERNAL |
| 108 |
void mapped_ring_buffer_clear (MappedRingBuffer *self); |
| 109 |
SYSPROF_INTERNAL |
| 110 |
void *mapped_ring_buffer_allocate (MappedRingBuffer *self, |
| 111 |
size_t length); |
| 112 |
SYSPROF_INTERNAL |
| 113 |
void mapped_ring_buffer_advance (MappedRingBuffer *self, |
| 114 |
size_t length); |
| 115 |
SYSPROF_INTERNAL |
| 116 |
bool mapped_ring_buffer_drain (MappedRingBuffer *self, |
| 117 |
MappedRingBufferCallback callback, |
| 118 |
void *user_data); |
| 119 |
SYSPROF_INTERNAL |
| 120 |
bool mapped_ring_buffer_is_empty (MappedRingBuffer *self); |
| 121 |
|
| 122 |
SYSPROF_END_DECLS |