/src/openthread/third_party/tcplp/lib/lbuf.c
Line | Count | Source |
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 | | /* LINKED BUFFER */ |
31 | | |
32 | | #include "lbuf.h" |
33 | | #include <string.h> |
34 | | #include <openthread/tcp.h> |
35 | | |
36 | 1 | void lbuf_init(struct lbufhead* buffer) { |
37 | 1 | memset(buffer, 0x00, sizeof(struct lbufhead)); |
38 | 1 | } |
39 | | |
40 | 0 | void lbuf_append(struct lbufhead* buffer, otLinkedBuffer* newentry) { |
41 | 0 | otLinkedBuffer* tail = buffer->tail; |
42 | 0 | if (tail == NULL) { |
43 | 0 | buffer->head = newentry; |
44 | 0 | buffer->tail = newentry; |
45 | 0 | buffer->offset = 0; |
46 | 0 | buffer->length = newentry->mLength; |
47 | 0 | newentry->mNext = NULL; |
48 | 0 | } else { |
49 | 0 | tail->mNext = newentry; |
50 | 0 | buffer->tail = newentry; |
51 | 0 | buffer->length += newentry->mLength; |
52 | 0 | newentry->mNext = NULL; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 0 | void lbuf_extend(struct lbufhead* buffer, size_t numbytes) { |
57 | 0 | buffer->tail->mLength += numbytes; |
58 | 0 | buffer->length += numbytes; |
59 | 0 | } |
60 | | |
61 | 0 | size_t lbuf_pop(struct lbufhead* buffer, size_t numbytes, uint32_t* ntraversed) { |
62 | 0 | otLinkedBuffer* curr = buffer->head; |
63 | 0 | size_t bytesleft = numbytes; |
64 | 0 | size_t curroffset = buffer->offset; |
65 | 0 | if (curr == NULL) { |
66 | 0 | return 0; |
67 | 0 | } |
68 | 0 | while (bytesleft >= curr->mLength - curroffset) { |
69 | 0 | ++*ntraversed; |
70 | 0 | bytesleft -= (curr->mLength - curroffset); |
71 | 0 | buffer->length -= (curr->mLength - curroffset); |
72 | 0 | if (buffer->tail == curr) { |
73 | 0 | buffer->head = NULL; |
74 | 0 | buffer->tail = NULL; |
75 | 0 | buffer->offset = 0; |
76 | 0 | return numbytes - bytesleft; |
77 | 0 | } |
78 | 0 | curr = curr->mNext; |
79 | 0 | curroffset = 0; |
80 | 0 | } |
81 | | /* Handle the last entry. */ |
82 | 0 | buffer->head = curr; |
83 | 0 | buffer->offset = curroffset + bytesleft; |
84 | 0 | buffer->length -= bytesleft; |
85 | 0 | return numbytes; |
86 | 0 | } |
87 | | |
88 | | int lbuf_getrange(struct lbufhead* buffer, size_t offset, size_t numbytes, |
89 | | otLinkedBuffer** first, size_t* firstoffset, |
90 | 0 | otLinkedBuffer** last, size_t* lastextra) { |
91 | 0 | otLinkedBuffer* curr = buffer->head; |
92 | 0 | size_t offsetleft = offset + buffer->offset; |
93 | 0 | size_t bytesleft = numbytes; |
94 | 0 | if (buffer->length < offset + numbytes) { |
95 | 0 | return 1; // out of range |
96 | 0 | } |
97 | 0 | while (offsetleft > 0 && offsetleft >= curr->mLength) { |
98 | 0 | offsetleft -= curr->mLength; |
99 | 0 | curr = curr->mNext; |
100 | 0 | } |
101 | 0 | *first = curr; |
102 | 0 | *firstoffset = offsetleft; |
103 | 0 | bytesleft += offsetleft; |
104 | 0 | while (bytesleft > 0 && bytesleft > curr->mLength) { |
105 | 0 | bytesleft -= curr->mLength; |
106 | 0 | curr = curr->mNext; |
107 | 0 | } |
108 | 0 | *last = curr; |
109 | 0 | *lastextra = curr->mLength - bytesleft; |
110 | 0 | return 0; |
111 | 0 | } |
112 | | |
113 | 0 | size_t lbuf_used_space(const struct lbufhead* buffer) { |
114 | 0 | return buffer->length; |
115 | 0 | } |