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 LBUF_H_
31*cfb92d14SAndroid Build Coastguard Worker #define LBUF_H_
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker #include <stddef.h>
34*cfb92d14SAndroid Build Coastguard Worker #include <stdint.h>
35*cfb92d14SAndroid Build Coastguard Worker
36*cfb92d14SAndroid Build Coastguard Worker struct otLinkedBuffer;
37*cfb92d14SAndroid Build Coastguard Worker
38*cfb92d14SAndroid Build Coastguard Worker /* LINKED BUFFER */
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker struct lbufhead {
41*cfb92d14SAndroid Build Coastguard Worker struct otLinkedBuffer* head;
42*cfb92d14SAndroid Build Coastguard Worker struct otLinkedBuffer* tail;
43*cfb92d14SAndroid Build Coastguard Worker size_t offset;
44*cfb92d14SAndroid Build Coastguard Worker size_t length;
45*cfb92d14SAndroid Build Coastguard Worker };
46*cfb92d14SAndroid Build Coastguard Worker
47*cfb92d14SAndroid Build Coastguard Worker /* Initializes a linked buffer. */
48*cfb92d14SAndroid Build Coastguard Worker void lbuf_init(struct lbufhead* buffer);
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Worker /* Returns the contents of the buffer as a linked buffer chain, or NULL if the buffer has
51*cfb92d14SAndroid Build Coastguard Worker no head. */
lbuf_head(struct lbufhead * buffer)52*cfb92d14SAndroid Build Coastguard Worker static inline struct otLinkedBuffer* lbuf_head(struct lbufhead* buffer) {
53*cfb92d14SAndroid Build Coastguard Worker return buffer->head;
54*cfb92d14SAndroid Build Coastguard Worker }
55*cfb92d14SAndroid Build Coastguard Worker
56*cfb92d14SAndroid Build Coastguard Worker /* Adds the contents of NEWENTRY to the buffer by appending it to the end of
57*cfb92d14SAndroid Build Coastguard Worker the current chain. */
58*cfb92d14SAndroid Build Coastguard Worker void lbuf_append(struct lbufhead* buffer, struct otLinkedBuffer* newentry);
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Worker /* Extends the last entry of the buffer by the specified number of bytes. */
61*cfb92d14SAndroid Build Coastguard Worker void lbuf_extend(struct lbufhead* buffer, size_t numbytes);
62*cfb92d14SAndroid Build Coastguard Worker
63*cfb92d14SAndroid Build Coastguard Worker /* Removes the first NUMBYTES bytes from the buffer, and returns the number of
64*cfb92d14SAndroid Build Coastguard Worker bytes removed (which is fewer than NUMBYTES if there were fewer than
65*cfb92d14SAndroid Build Coastguard Worker NUMBYTES bytes in the buffer to begin with). *NTRAVERSED is incremented once
66*cfb92d14SAndroid Build Coastguard Worker for each entry in the buffer that is no longer referenced and can be
67*cfb92d14SAndroid Build Coastguard Worker reclaimed. */
68*cfb92d14SAndroid Build Coastguard Worker size_t lbuf_pop(struct lbufhead* buffer, size_t numbytes, uint32_t* ntraversed);
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Worker /* Given a range of indices, specified by an OFFSET from the start and a
71*cfb92d14SAndroid Build Coastguard Worker length NUMBYTES, this function locates the chain of linked buffer entries
72*cfb92d14SAndroid Build Coastguard Worker that reference the corresponding bytes.
73*cfb92d14SAndroid Build Coastguard Worker A pointer to the first entry in the range is stored into FIRST, and the
74*cfb92d14SAndroid Build Coastguard Worker number of bytes in the entry before the start of the range is stored into
75*cfb92d14SAndroid Build Coastguard Worker FIRSTOFFSET. A pointer to the last entry in the range is stored into LAST,
76*cfb92d14SAndroid Build Coastguard Worker and the number of bytes in that entry after the end of the range is stored
77*cfb92d14SAndroid Build Coastguard Worker into LASTEXTRA.
78*cfb92d14SAndroid Build Coastguard Worker Returns 0 on success and 1 on failure. On failure, FIRST, LAST, FIRSTOFFSET,
79*cfb92d14SAndroid Build Coastguard Worker and LASTEXTRA are not set. The only failure condition is when there are not
80*cfb92d14SAndroid Build Coastguard Worker enough bytes in the buffer to do the full traversal. */
81*cfb92d14SAndroid Build Coastguard Worker int lbuf_getrange(struct lbufhead* buffer, size_t offset, size_t numbytes,
82*cfb92d14SAndroid Build Coastguard Worker struct otLinkedBuffer** first, size_t* firstoffset,
83*cfb92d14SAndroid Build Coastguard Worker struct otLinkedBuffer** last, size_t* lastextra);
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker /* Returns the total number of bytes stored in the buffer. */
86*cfb92d14SAndroid Build Coastguard Worker size_t lbuf_used_space(const struct lbufhead* buffer);
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker #endif
89