1 /*
2 * Copyright (c) 2018, Sam Kumar <samkumar@cs.berkeley.edu>
3 * Copyright (c) 2018, University of California, Berkeley
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the
14 * names of its contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef LBUF_H_
31 #define LBUF_H_
32
33 #include <stddef.h>
34 #include <stdint.h>
35
36 struct otLinkedBuffer;
37
38 /* LINKED BUFFER */
39
40 struct lbufhead {
41 struct otLinkedBuffer* head;
42 struct otLinkedBuffer* tail;
43 size_t offset;
44 size_t length;
45 };
46
47 /* Initializes a linked buffer. */
48 void lbuf_init(struct lbufhead* buffer);
49
50 /* Returns the contents of the buffer as a linked buffer chain, or NULL if the buffer has
51 no head. */
lbuf_head(struct lbufhead * buffer)52 static inline struct otLinkedBuffer* lbuf_head(struct lbufhead* buffer) {
53 return buffer->head;
54 }
55
56 /* Adds the contents of NEWENTRY to the buffer by appending it to the end of
57 the current chain. */
58 void lbuf_append(struct lbufhead* buffer, struct otLinkedBuffer* newentry);
59
60 /* Extends the last entry of the buffer by the specified number of bytes. */
61 void lbuf_extend(struct lbufhead* buffer, size_t numbytes);
62
63 /* Removes the first NUMBYTES bytes from the buffer, and returns the number of
64 bytes removed (which is fewer than NUMBYTES if there were fewer than
65 NUMBYTES bytes in the buffer to begin with). *NTRAVERSED is incremented once
66 for each entry in the buffer that is no longer referenced and can be
67 reclaimed. */
68 size_t lbuf_pop(struct lbufhead* buffer, size_t numbytes, uint32_t* ntraversed);
69
70 /* Given a range of indices, specified by an OFFSET from the start and a
71 length NUMBYTES, this function locates the chain of linked buffer entries
72 that reference the corresponding bytes.
73 A pointer to the first entry in the range is stored into FIRST, and the
74 number of bytes in the entry before the start of the range is stored into
75 FIRSTOFFSET. A pointer to the last entry in the range is stored into LAST,
76 and the number of bytes in that entry after the end of the range is stored
77 into LASTEXTRA.
78 Returns 0 on success and 1 on failure. On failure, FIRST, LAST, FIRSTOFFSET,
79 and LASTEXTRA are not set. The only failure condition is when there are not
80 enough bytes in the buffer to do the full traversal. */
81 int lbuf_getrange(struct lbufhead* buffer, size_t offset, size_t numbytes,
82 struct otLinkedBuffer** first, size_t* firstoffset,
83 struct otLinkedBuffer** last, size_t* lastextra);
84
85 /* Returns the total number of bytes stored in the buffer. */
86 size_t lbuf_used_space(const struct lbufhead* buffer);
87
88 #endif
89