|
| 1 | +/* |
| 2 | +
|
| 3 | +The MIT License (MIT) |
| 4 | +
|
| 5 | +Copyright (c) 2016 Wrymouth Innovation Ltd |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | +copy of this software and associated documentation files (the "Software"), |
| 9 | +to deal in the Software without restriction, including without limitation |
| 10 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | +and/or sell copies of the Software, and to permit persons to whom the |
| 12 | +Software is furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included |
| 15 | +in all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | +OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include <sys/types.h> |
| 28 | + |
| 29 | +#include "buffer.h" |
| 30 | + |
| 31 | +typedef struct buffer |
| 32 | +{ |
| 33 | + char *buf; |
| 34 | + ssize_t size; |
| 35 | + ssize_t hwm; |
| 36 | + ssize_t ridx; |
| 37 | + ssize_t widx; |
| 38 | + int empty; |
| 39 | +} buffer_t; |
| 40 | + |
| 41 | +/* the buffer is organised internally as follows: |
| 42 | + * |
| 43 | + * * There are b->size bytes in the buffer. |
| 44 | + * |
| 45 | + * * Bytes are at offsets 0 to b->size-1 |
| 46 | + * |
| 47 | + * * b->ridx points to the first readable byte |
| 48 | + * |
| 49 | + * * b->widx points to the first empty space |
| 50 | + * |
| 51 | + * * b->ridx < b->widx indicates a non-wrapped buffer: |
| 52 | + * |
| 53 | + * 0 ridx widx size |
| 54 | + * | | | | |
| 55 | + * V V V V |
| 56 | + * ........XXXXXXXXX................ |
| 57 | + * |
| 58 | + * * b->ridx > b->widx indicates a wrapped buffer: |
| 59 | + * |
| 60 | + * 0 widx ridx size |
| 61 | + * | | | | |
| 62 | + * V V V V |
| 63 | + * XXXXXXXX.........XXXXXXXXXXXXXXXX |
| 64 | + * |
| 65 | + * * b->ridx == b->widx indicates a FULL buffer: |
| 66 | + * |
| 67 | + * * b->ridx == b->widx indicates a wrapped buffer: |
| 68 | + * |
| 69 | + * 0 widx == ridx size |
| 70 | + * | | | |
| 71 | + * V V V |
| 72 | + * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 73 | + * |
| 74 | + * An empty buffer is indicated by empty=1 |
| 75 | + * |
| 76 | + */ |
| 77 | + |
| 78 | +buffer_t * |
| 79 | +bufNew (ssize_t size, ssize_t hwm) |
| 80 | +{ |
| 81 | + buffer_t *b = calloc (1, sizeof (buffer_t)); |
| 82 | + b->buf = calloc (1, size); |
| 83 | + b->size = size; |
| 84 | + b->hwm = hwm; |
| 85 | + b->empty = 1; |
| 86 | + return b; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +void |
| 91 | +bufFree (buffer_t * b) |
| 92 | +{ |
| 93 | + free (b->buf); |
| 94 | + free (b); |
| 95 | +} |
| 96 | + |
| 97 | +/* get a maximal span to read. Returns 0 if buffer |
| 98 | + * is empty |
| 99 | + */ |
| 100 | +ssize_t |
| 101 | +bufGetReadSpan (buffer_t * b, void **addr) |
| 102 | +{ |
| 103 | + if (b->empty) |
| 104 | + { |
| 105 | + *addr = NULL; |
| 106 | + return 0; |
| 107 | + } |
| 108 | + *addr = &(b->buf[b->ridx]); |
| 109 | + ssize_t len = b->widx - b->ridx; |
| 110 | + if (len <= 0) |
| 111 | + len = b->size - b->ridx; |
| 112 | + return len; |
| 113 | +} |
| 114 | + |
| 115 | +/* get a maximal span to write. Returns 0 id buffer is full |
| 116 | + */ |
| 117 | +ssize_t |
| 118 | +bufGetWriteSpan (buffer_t * b, void **addr) |
| 119 | +{ |
| 120 | + if (b->empty) |
| 121 | + { |
| 122 | + *addr = b->buf; |
| 123 | + b->ridx = 0; |
| 124 | + b->widx = 0; |
| 125 | + return b->size; |
| 126 | + } |
| 127 | + if (b->ridx == b->widx) |
| 128 | + { |
| 129 | + *addr = NULL; |
| 130 | + return 0; |
| 131 | + } |
| 132 | + *addr = &(b->buf[b->widx]); |
| 133 | + ssize_t len = b->ridx - b->widx; |
| 134 | + if (len <= 0) |
| 135 | + len = b->size - b->widx; |
| 136 | + return len; |
| 137 | +} |
| 138 | + |
| 139 | +/* mark size bytes as read */ |
| 140 | +void |
| 141 | +bufDoneRead (buffer_t * b, ssize_t size) |
| 142 | +{ |
| 143 | + while (!b->empty && (size > 0)) |
| 144 | + { |
| 145 | + /* empty can't occur here, so equal pointers means full */ |
| 146 | + ssize_t len = b->widx - b->ridx; |
| 147 | + if (len <= 0) |
| 148 | + len = b->size - b->ridx; |
| 149 | + |
| 150 | + /* len is the number of bytes in one read span */ |
| 151 | + if (len > size) |
| 152 | + len = size; |
| 153 | + |
| 154 | + b->ridx += len; |
| 155 | + if (b->ridx >= b->size) |
| 156 | + b->ridx = 0; |
| 157 | + |
| 158 | + if (b->ridx == b->widx) |
| 159 | + { |
| 160 | + b->ridx = 0; |
| 161 | + b->widx = 0; |
| 162 | + b->empty = 1; |
| 163 | + } |
| 164 | + |
| 165 | + size -= len; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/* mark size bytes as written */ |
| 170 | +void |
| 171 | +bufDoneWrite (buffer_t * b, ssize_t size) |
| 172 | +{ |
| 173 | + while ((b->empty || (b->ridx != b->widx)) && (size > 0)) |
| 174 | + { |
| 175 | + /* full can't occur here, so equal pointers means empty */ |
| 176 | + ssize_t len = b->ridx - b->widx; |
| 177 | + if (len <= 0) |
| 178 | + len = b->size - b->widx; |
| 179 | + |
| 180 | + /* len is the number of bytes in one write span */ |
| 181 | + if (len > size) |
| 182 | + len = size; |
| 183 | + |
| 184 | + b->widx += len; |
| 185 | + if (b->widx >= b->size) |
| 186 | + b->widx = 0; |
| 187 | + |
| 188 | + /* it can't be empty as we've written at least one byte */ |
| 189 | + b->empty = 0; |
| 190 | + |
| 191 | + size -= len; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +int |
| 196 | +bufIsEmpty (buffer_t * b) |
| 197 | +{ |
| 198 | + return b->empty; |
| 199 | +} |
| 200 | + |
| 201 | +int |
| 202 | +bufIsFull (buffer_t * b) |
| 203 | +{ |
| 204 | + return !b->empty && (b->ridx == b->widx); |
| 205 | +} |
| 206 | + |
| 207 | +int |
| 208 | +bufIsOverHWM (buffer_t * b) |
| 209 | +{ |
| 210 | + return bufGetCount (b) > b->hwm; |
| 211 | +} |
| 212 | + |
| 213 | +ssize_t |
| 214 | +bufGetFree (buffer_t * b) |
| 215 | +{ |
| 216 | + return b->size - bufGetCount (b); |
| 217 | +} |
| 218 | + |
| 219 | +ssize_t |
| 220 | +bufGetCount (buffer_t * b) |
| 221 | +{ |
| 222 | + if (b->empty) |
| 223 | + return 0; |
| 224 | + return b->widx - b->ridx + ((b->ridx < b->widx) ? 0 : b->size); |
| 225 | +} |
0 commit comments