xref: /aosp_15_r20/external/openthread/third_party/tcplp/lib/cbuf.h (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2018, Sam Kumar <[email protected]>
3*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2018, University of California, Berkeley
4*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker  *
6*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker  *
17*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker  */
29*cfb92d14SAndroid Build Coastguard Worker 
30*cfb92d14SAndroid Build Coastguard Worker #ifndef CBUF_H_
31*cfb92d14SAndroid Build Coastguard Worker #define CBUF_H_
32*cfb92d14SAndroid Build Coastguard Worker 
33*cfb92d14SAndroid Build Coastguard Worker /* CIRCULAR BUFFER */
34*cfb92d14SAndroid Build Coastguard Worker 
35*cfb92d14SAndroid Build Coastguard Worker #include <stdbool.h>
36*cfb92d14SAndroid Build Coastguard Worker #include <stdint.h>
37*cfb92d14SAndroid Build Coastguard Worker #include <stdlib.h>
38*cfb92d14SAndroid Build Coastguard Worker #include <sys/types.h>
39*cfb92d14SAndroid Build Coastguard Worker 
40*cfb92d14SAndroid Build Coastguard Worker struct otLinkedBuffer;
41*cfb92d14SAndroid Build Coastguard Worker 
42*cfb92d14SAndroid Build Coastguard Worker /* Represents a circular buffer. */
43*cfb92d14SAndroid Build Coastguard Worker struct cbufhead {
44*cfb92d14SAndroid Build Coastguard Worker     size_t r_index;
45*cfb92d14SAndroid Build Coastguard Worker     size_t used;
46*cfb92d14SAndroid Build Coastguard Worker     size_t size;
47*cfb92d14SAndroid Build Coastguard Worker     uint8_t* buf;
48*cfb92d14SAndroid Build Coastguard Worker };
49*cfb92d14SAndroid Build Coastguard Worker 
50*cfb92d14SAndroid Build Coastguard Worker /* Initializes a circular buffer (chdr is the header storing metadata). */
51*cfb92d14SAndroid Build Coastguard Worker void cbuf_init(struct cbufhead* chdr, uint8_t* buf, size_t len);
52*cfb92d14SAndroid Build Coastguard Worker 
53*cfb92d14SAndroid Build Coastguard Worker /* Function type for copying data into or out of a circular buffer. */
54*cfb92d14SAndroid Build Coastguard Worker typedef void(*cbuf_copier_t)(void*, size_t, const void*, size_t, size_t);
55*cfb92d14SAndroid Build Coastguard Worker 
56*cfb92d14SAndroid Build Coastguard Worker /* Instantiations of cbuf_copier_t, for copying data between circular buffers and regular buffers. */
57*cfb92d14SAndroid Build Coastguard Worker void cbuf_copy_into_buffer(void* buffer, size_t buffer_offset, const void* arr, size_t arr_offset, size_t num_bytes);
58*cfb92d14SAndroid Build Coastguard Worker void cbuf_copy_from_buffer(void* arr, size_t arr_offset, const void* buffer, size_t buffer_offset, size_t num_bytes);
59*cfb92d14SAndroid Build Coastguard Worker 
60*cfb92d14SAndroid Build Coastguard Worker /* Instantiations of cbuf_copier_t, for copying data between circular buffers and otMessages. */
61*cfb92d14SAndroid Build Coastguard Worker void cbuf_copy_into_message(void* buffer, size_t buffer_offset, const void* arr, size_t arr_offset, size_t num_bytes);
62*cfb92d14SAndroid Build Coastguard Worker void cbuf_copy_from_message(void* arr, size_t arr_offset, const void* buffer, size_t buffer_offset, size_t num_bytes);
63*cfb92d14SAndroid Build Coastguard Worker 
64*cfb92d14SAndroid Build Coastguard Worker /* Writes data to the back of the circular buffer using the specified copier. */
65*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_write(struct cbufhead* chdr, const void* data, size_t data_offset, size_t data_len, cbuf_copier_t copy_from);
66*cfb92d14SAndroid Build Coastguard Worker 
67*cfb92d14SAndroid Build Coastguard Worker /* Reads data from the front of the circular buffer using the specified copier. */
68*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_read(struct cbufhead* chdr, void* data, size_t data_offset, size_t numbytes, int pop, cbuf_copier_t copy_into);
69*cfb92d14SAndroid Build Coastguard Worker 
70*cfb92d14SAndroid Build Coastguard Worker /* Reads data at the specified offset, in bytes, from the front of the circular buffer using the specified copier. */
71*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_read_offset(struct cbufhead* chdr, void* data, size_t data_offset, size_t numbytes, size_t offset, cbuf_copier_t copy_into);
72*cfb92d14SAndroid Build Coastguard Worker 
73*cfb92d14SAndroid Build Coastguard Worker /* Drops bytes from the front of the circular buffer. */
74*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_pop(struct cbufhead* chdr, size_t numbytes);
75*cfb92d14SAndroid Build Coastguard Worker 
76*cfb92d14SAndroid Build Coastguard Worker /* Returns the number of bytes available for reading from the circular buffer. */
77*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_used_space(struct cbufhead* chdr);
78*cfb92d14SAndroid Build Coastguard Worker 
79*cfb92d14SAndroid Build Coastguard Worker /* Returns the number of bytes that can be written to the circular buffer before it is full. */
80*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_free_space(struct cbufhead* chdr);
81*cfb92d14SAndroid Build Coastguard Worker 
82*cfb92d14SAndroid Build Coastguard Worker /* Returns the total capacity of the circular buffer, in bytes. */
83*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_size(struct cbufhead* chdr);
84*cfb92d14SAndroid Build Coastguard Worker 
85*cfb92d14SAndroid Build Coastguard Worker /* Returns true if the circular buffer is empty, and false if it is not empty. */
86*cfb92d14SAndroid Build Coastguard Worker bool cbuf_empty(struct cbufhead* chdr);
87*cfb92d14SAndroid Build Coastguard Worker 
88*cfb92d14SAndroid Build Coastguard Worker /* Rotates the circular buffer's data so that the "used" portion begins at the beginning of the buffer. */
89*cfb92d14SAndroid Build Coastguard Worker void cbuf_contiguify(struct cbufhead* chdr, uint8_t* bitmap);
90*cfb92d14SAndroid Build Coastguard Worker 
91*cfb92d14SAndroid Build Coastguard Worker /* Populates the provided otLinkedBuffers to reference the data currently in the circular buffer. */
92*cfb92d14SAndroid Build Coastguard Worker void cbuf_reference(const struct cbufhead* chdr, struct otLinkedBuffer* first, struct otLinkedBuffer* second);
93*cfb92d14SAndroid Build Coastguard Worker 
94*cfb92d14SAndroid Build Coastguard Worker /* Writes DATA at the end of the circular buffer without making it available for
95*cfb92d14SAndroid Build Coastguard Worker    reading. This data is said to be "out-of-sequence". OFFSET is position at
96*cfb92d14SAndroid Build Coastguard Worker    which to write these bytes, relative to the position where cbuf_write would
97*cfb92d14SAndroid Build Coastguard Worker    write them. Each bit in the BITMAP corresponds to a byte in the circular
98*cfb92d14SAndroid Build Coastguard Worker    buffer; the bits corresponding to the bytes containing the newly written
99*cfb92d14SAndroid Build Coastguard Worker    data are set. The index of the first byte written is stored into FIRSTINDEX,
100*cfb92d14SAndroid Build Coastguard Worker    if it is not NULL. */
101*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_reass_write(struct cbufhead* chdr, size_t offset, const void* data, size_t data_offset, size_t numbytes, uint8_t* bitmap, size_t* firstindex, cbuf_copier_t copy_from);
102*cfb92d14SAndroid Build Coastguard Worker 
103*cfb92d14SAndroid Build Coastguard Worker /* Makes NUMBYTES out-of-seqence bytes available for reading in the circular
104*cfb92d14SAndroid Build Coastguard Worker    buffer. No data is copied; the out-of-sequence bytes made available are the
105*cfb92d14SAndroid Build Coastguard Worker    bytes currently at the position where cbuf_write would write them. The bytes
106*cfb92d14SAndroid Build Coastguard Worker    are taken from the unused space of the buffer, and can be set using
107*cfb92d14SAndroid Build Coastguard Worker    cbuf_reass_write. */
108*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_reass_merge(struct cbufhead* chdr, size_t numbytes, uint8_t* bitmap);
109*cfb92d14SAndroid Build Coastguard Worker 
110*cfb92d14SAndroid Build Coastguard Worker /* Counts the number of contiguous out-of-sequence bytes at the specified
111*cfb92d14SAndroid Build Coastguard Worker    OFFSET, until the count reaches the specified LIMIT. Note that, for a given,
112*cfb92d14SAndroid Build Coastguard Worker    limit, this function might overcount the length of the continuous
113*cfb92d14SAndroid Build Coastguard Worker    out-of-sequence bytes and return a greater number; the caller is assumed to
114*cfb92d14SAndroid Build Coastguard Worker    handle this appropriately (i.e., treating the limit not as a hard upper
115*cfb92d14SAndroid Build Coastguard Worker    bound on the return value, but rather as, "I don't care if more bits than
116*cfb92d14SAndroid Build Coastguard Worker    this are set"). Just because the function returns something more than
117*cfb92d14SAndroid Build Coastguard Worker    LIMIT, it doesn't necessarily mean that more than LIMIT bits are actually
118*cfb92d14SAndroid Build Coastguard Worker    set. Note that LIMIT should never be set to a value greater than the number
119*cfb92d14SAndroid Build Coastguard Worker    of bytes in the circular buffer. */
120*cfb92d14SAndroid Build Coastguard Worker size_t cbuf_reass_count_set(struct cbufhead* chdr, size_t offset, uint8_t* bitmap, size_t limit);
121*cfb92d14SAndroid Build Coastguard Worker 
122*cfb92d14SAndroid Build Coastguard Worker /* Returns a true value iff INDEX is the index of a byte within OFFSET bytes
123*cfb92d14SAndroid Build Coastguard Worker    past the end of the buffer. */
124*cfb92d14SAndroid Build Coastguard Worker int cbuf_reass_within_offset(struct cbufhead* chdr, size_t offset, size_t index);
125*cfb92d14SAndroid Build Coastguard Worker 
126*cfb92d14SAndroid Build Coastguard Worker #endif
127