Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 1 | #include <haproxy/ncbuf.h> |
| 2 | |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 3 | #include <string.h> |
| 4 | |
| 5 | #ifndef MIN |
| 6 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 7 | #endif |
| 8 | |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 9 | #ifdef STANDALONE |
| 10 | #include <stdarg.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdio.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #include <haproxy/list.h> |
| 16 | #endif /* STANDALONE */ |
| 17 | |
Amaury Denoyelle | 2526a6a | 2022-11-28 15:10:30 +0100 | [diff] [blame] | 18 | #ifdef DEBUG_STRICT |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 19 | # include <haproxy/bug.h> |
| 20 | #else |
| 21 | # include <stdio.h> |
| 22 | # include <stdlib.h> |
| 23 | |
| 24 | # undef BUG_ON |
| 25 | # define BUG_ON(x) if (x) { fprintf(stderr, "CRASH ON %s:%d\n", __func__, __LINE__); abort(); } |
| 26 | |
| 27 | # undef BUG_ON_HOT |
| 28 | # define BUG_ON_HOT(x) if (x) { fprintf(stderr, "CRASH ON %s:%d\n", __func__, __LINE__); abort(); } |
| 29 | #endif /* DEBUG_DEV */ |
| 30 | |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 31 | #include <haproxy/compiler.h> |
| 32 | |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 33 | /* ******** internal API ******** */ |
| 34 | |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 35 | #define NCB_BLK_NULL ((struct ncb_blk){ .st = NULL }) |
| 36 | |
| 37 | #define NCB_BK_F_GAP 0x01 /* block represents a gap */ |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 38 | #define NCB_BK_F_FIN 0x02 /* special reduced gap present at the end of the buffer */ |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 39 | struct ncb_blk { |
| 40 | char *st; /* first byte of the block */ |
| 41 | char *end; /* first byte after this block */ |
| 42 | |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 43 | char *sz_ptr; /* pointer to size element - NULL for reduced gap */ |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 44 | ncb_sz_t sz; /* size of the block */ |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 45 | ncb_sz_t sz_data; /* size of the data following the block - invalid for reduced GAP */ |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 46 | ncb_sz_t off; /* offset of block in buffer */ |
| 47 | |
| 48 | char flag; |
| 49 | }; |
| 50 | |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 51 | /* Return pointer to <off> relative to <buf> head. Support buffer wrapping. */ |
| 52 | static char *ncb_peek(const struct ncbuf *buf, ncb_sz_t off) |
| 53 | { |
| 54 | char *ptr = ncb_head(buf) + off; |
| 55 | if (ptr >= buf->area + buf->size) |
| 56 | ptr -= buf->size; |
| 57 | return ptr; |
| 58 | } |
| 59 | |
| 60 | /* Returns the reserved space of <buf> which contains the size of the first |
| 61 | * data block. |
| 62 | */ |
| 63 | static char *ncb_reserved(const struct ncbuf *buf) |
| 64 | { |
| 65 | return ncb_peek(buf, buf->size - NCB_RESERVED_SZ); |
| 66 | } |
| 67 | |
| 68 | /* Encode <off> at <st> position in <buf>. Support wrapping. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 69 | static forceinline void ncb_write_off(const struct ncbuf *buf, char *st, ncb_sz_t off) |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 70 | { |
| 71 | int i; |
| 72 | |
| 73 | BUG_ON_HOT(st >= buf->area + buf->size); |
| 74 | |
| 75 | for (i = 0; i < sizeof(ncb_sz_t); ++i) { |
| 76 | (*st) = off >> (8 * i) & 0xff; |
| 77 | |
| 78 | if ((++st) == ncb_wrap(buf)) |
| 79 | st = ncb_orig(buf); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* Decode offset stored at <st> position in <buf>. Support wrapping. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 84 | static forceinline ncb_sz_t ncb_read_off(const struct ncbuf *buf, char *st) |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 85 | { |
| 86 | int i; |
| 87 | ncb_sz_t off = 0; |
| 88 | |
| 89 | BUG_ON_HOT(st >= buf->area + buf->size); |
| 90 | |
| 91 | for (i = 0; i < sizeof(ncb_sz_t); ++i) { |
| 92 | off |= (unsigned char )(*st) << (8 * i); |
| 93 | |
| 94 | if ((++st) == ncb_wrap(buf)) |
| 95 | st = ncb_orig(buf); |
| 96 | } |
| 97 | |
| 98 | return off; |
| 99 | } |
| 100 | |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 101 | /* Add <off> to the offset stored at <st> in <buf>. Support wrapping. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 102 | static forceinline void ncb_inc_off(const struct ncbuf *buf, char *st, ncb_sz_t off) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 103 | { |
| 104 | const ncb_sz_t old = ncb_read_off(buf, st); |
| 105 | ncb_write_off(buf, st, old + off); |
| 106 | } |
| 107 | |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 108 | /* Returns true if a gap cannot be inserted at <off> : a reduced gap must be used. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 109 | static forceinline int ncb_off_reduced(const struct ncbuf *b, ncb_sz_t off) |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 110 | { |
| 111 | return off + NCB_GAP_MIN_SZ > ncb_size(b); |
| 112 | } |
| 113 | |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 114 | /* Returns true if <blk> is the special NULL block. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 115 | static forceinline int ncb_blk_is_null(const struct ncb_blk *blk) |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 116 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 117 | return !blk->st; |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Returns true if <blk> is the last block of <buf>. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 121 | static forceinline int ncb_blk_is_last(const struct ncbuf *buf, const struct ncb_blk *blk) |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 122 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 123 | BUG_ON_HOT(blk->off + blk->sz > ncb_size(buf)); |
| 124 | return blk->off + blk->sz == ncb_size(buf); |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* Returns the first block of <buf> which is always a DATA. */ |
| 128 | static struct ncb_blk ncb_blk_first(const struct ncbuf *buf) |
| 129 | { |
| 130 | struct ncb_blk blk; |
| 131 | |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 132 | if (ncb_is_null(buf)) |
| 133 | return NCB_BLK_NULL; |
| 134 | |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 135 | blk.st = ncb_head(buf); |
| 136 | |
| 137 | blk.sz_ptr = ncb_reserved(buf); |
| 138 | blk.sz = ncb_read_off(buf, ncb_reserved(buf)); |
Amaury Denoyelle | b5ca943 | 2022-05-13 15:33:50 +0200 | [diff] [blame] | 139 | blk.sz_data = 0; |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 140 | BUG_ON_HOT(blk.sz > ncb_size(buf)); |
| 141 | |
| 142 | blk.end = ncb_peek(buf, blk.sz); |
| 143 | blk.off = 0; |
| 144 | blk.flag = 0; |
| 145 | |
| 146 | return blk; |
| 147 | } |
| 148 | |
| 149 | /* Returns the block following <prev> in the buffer <buf>. */ |
| 150 | static struct ncb_blk ncb_blk_next(const struct ncbuf *buf, |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 151 | const struct ncb_blk *prev) |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 152 | { |
| 153 | struct ncb_blk blk; |
| 154 | |
| 155 | BUG_ON_HOT(ncb_blk_is_null(prev)); |
| 156 | |
| 157 | if (ncb_blk_is_last(buf, prev)) |
| 158 | return NCB_BLK_NULL; |
| 159 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 160 | blk.st = prev->end; |
| 161 | blk.off = prev->off + prev->sz; |
| 162 | blk.flag = ~prev->flag & NCB_BK_F_GAP; |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 163 | |
| 164 | if (blk.flag & NCB_BK_F_GAP) { |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 165 | if (ncb_off_reduced(buf, blk.off)) { |
| 166 | blk.flag |= NCB_BK_F_FIN; |
| 167 | blk.sz_ptr = NULL; |
| 168 | blk.sz = ncb_size(buf) - blk.off; |
| 169 | blk.sz_data = 0; |
| 170 | |
| 171 | /* A reduced gap can only be the last block. */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 172 | BUG_ON_HOT(!ncb_blk_is_last(buf, &blk)); |
Amaury Denoyelle | edeb0a6 | 2022-05-04 16:16:39 +0200 | [diff] [blame] | 173 | } |
| 174 | else { |
| 175 | blk.sz_ptr = ncb_peek(buf, blk.off + NCB_GAP_SZ_OFF); |
| 176 | blk.sz = ncb_read_off(buf, blk.sz_ptr); |
| 177 | blk.sz_data = ncb_read_off(buf, ncb_peek(buf, blk.off + NCB_GAP_SZ_DATA_OFF)); |
| 178 | BUG_ON_HOT(blk.sz < NCB_GAP_MIN_SZ); |
| 179 | } |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 180 | } |
| 181 | else { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 182 | blk.sz_ptr = ncb_peek(buf, prev->off + NCB_GAP_SZ_DATA_OFF); |
| 183 | blk.sz = prev->sz_data; |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 184 | blk.sz_data = 0; |
| 185 | |
| 186 | /* only first DATA block can be empty. If this happens, a GAP |
| 187 | * merge should have been realized. |
| 188 | */ |
| 189 | BUG_ON_HOT(!blk.sz); |
| 190 | } |
| 191 | |
| 192 | BUG_ON_HOT(blk.off + blk.sz > ncb_size(buf)); |
| 193 | blk.end = ncb_peek(buf, blk.off + blk.sz); |
| 194 | |
| 195 | return blk; |
| 196 | } |
| 197 | |
| 198 | /* Returns the block containing offset <off>. Note that if <off> is at the |
| 199 | * frontier between two blocks, this function will return the preceding one. |
| 200 | * This is done to easily merge blocks on insertion/deletion. |
| 201 | */ |
| 202 | static struct ncb_blk ncb_blk_find(const struct ncbuf *buf, ncb_sz_t off) |
| 203 | { |
| 204 | struct ncb_blk blk; |
| 205 | |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 206 | if (ncb_is_null(buf)) |
| 207 | return NCB_BLK_NULL; |
| 208 | |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 209 | BUG_ON_HOT(off >= ncb_size(buf)); |
| 210 | |
| 211 | for (blk = ncb_blk_first(buf); off > blk.off + blk.sz; |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 212 | blk = ncb_blk_next(buf, &blk)) { |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return blk; |
| 216 | } |
| 217 | |
| 218 | /* Transform absolute offset <off> to a relative one from <blk> start. */ |
Amaury Denoyelle | d64a26f | 2022-11-28 15:08:14 +0100 | [diff] [blame] | 219 | static forceinline ncb_sz_t ncb_blk_off(const struct ncb_blk *blk, ncb_sz_t off) |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 220 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 221 | BUG_ON_HOT(off < blk->off || off > blk->off + blk->sz); |
| 222 | BUG_ON_HOT(off - blk->off > blk->sz); |
| 223 | return off - blk->off; |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 226 | /* Simulate insertion in <buf> of <data> of length <len> at offset <off>. This |
| 227 | * ensures that minimal block size are respected for newly formed gaps. <blk> |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 228 | * must be the block where the insert operation begins. If <mode> is |
| 229 | * NCB_ADD_COMPARE, old and new overlapped data are compared to validate the |
| 230 | * insertion. |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 231 | * |
| 232 | * Returns NCB_RET_OK if insertion can proceed. |
| 233 | */ |
| 234 | static enum ncb_ret ncb_check_insert(const struct ncbuf *buf, |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 235 | const struct ncb_blk *blk, ncb_sz_t off, |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 236 | const char *data, ncb_sz_t len, |
| 237 | enum ncb_add_mode mode) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 238 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 239 | struct ncb_blk next; |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 240 | ncb_sz_t off_blk = ncb_blk_off(blk, off); |
| 241 | ncb_sz_t to_copy; |
| 242 | ncb_sz_t left = len; |
| 243 | |
| 244 | /* If insertion starts in a gap, it must leave enough space to keep the |
| 245 | * gap header. |
| 246 | */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 247 | if (left && (blk->flag & NCB_BK_F_GAP)) { |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 248 | if (off_blk < NCB_GAP_MIN_SZ) |
| 249 | return NCB_RET_GAP_SIZE; |
| 250 | } |
| 251 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 252 | next = *blk; |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 253 | while (left) { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 254 | off_blk = ncb_blk_off(&next, off); |
| 255 | to_copy = MIN(left, next.sz - off_blk); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 256 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 257 | if (next.flag & NCB_BK_F_GAP && off_blk + to_copy < next.sz) { |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 258 | /* Insertion must leave enough space for a new gap |
| 259 | * header if stopped in a middle of a gap. |
| 260 | */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 261 | const ncb_sz_t gap_sz = next.sz - (off_blk + to_copy); |
| 262 | if (gap_sz < NCB_GAP_MIN_SZ && !ncb_blk_is_last(buf, &next)) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 263 | return NCB_RET_GAP_SIZE; |
| 264 | } |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 265 | else if (!(next.flag & NCB_BK_F_GAP) && mode == NCB_ADD_COMPARE) { |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 266 | /* Compare memory of data block in NCB_ADD_COMPARE mode. */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 267 | const ncb_sz_t off_blk = ncb_blk_off(&next, off); |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 268 | char *st = ncb_peek(buf, off); |
| 269 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 270 | to_copy = MIN(left, next.sz - off_blk); |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 271 | if (st + to_copy > ncb_wrap(buf)) { |
| 272 | const ncb_sz_t sz1 = ncb_wrap(buf) - st; |
| 273 | if (memcmp(st, data, sz1)) |
| 274 | return NCB_RET_DATA_REJ; |
| 275 | if (memcmp(ncb_orig(buf), data + sz1, to_copy - sz1)) |
| 276 | return NCB_RET_DATA_REJ; |
| 277 | } |
| 278 | else { |
| 279 | if (memcmp(st, data, to_copy)) |
| 280 | return NCB_RET_DATA_REJ; |
| 281 | } |
| 282 | } |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 283 | |
| 284 | left -= to_copy; |
| 285 | data += to_copy; |
| 286 | off += to_copy; |
| 287 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 288 | next = ncb_blk_next(buf, &next); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return NCB_RET_OK; |
| 292 | } |
| 293 | |
| 294 | /* Fill new <data> of length <len> inside an already existing data <blk> at |
| 295 | * offset <off>. Offset is relative to <blk> so it cannot be greater than the |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 296 | * block size. <mode> specifies if old data are preserved or overwritten. |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 297 | */ |
| 298 | static ncb_sz_t ncb_fill_data_blk(const struct ncbuf *buf, |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 299 | const struct ncb_blk *blk, ncb_sz_t off, |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 300 | const char *data, ncb_sz_t len, |
| 301 | enum ncb_add_mode mode) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 302 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 303 | const ncb_sz_t to_copy = MIN(len, blk->sz - off); |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 304 | char *ptr = NULL; |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 305 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 306 | BUG_ON_HOT(off > blk->sz); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 307 | /* This can happens due to previous ncb_blk_find() usage. In this |
| 308 | * case the current fill is a noop. |
| 309 | */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 310 | if (off == blk->sz) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 311 | return 0; |
| 312 | |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 313 | if (mode == NCB_ADD_OVERWRT) { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 314 | ptr = ncb_peek(buf, blk->off + off); |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 315 | |
| 316 | if (ptr + to_copy >= ncb_wrap(buf)) { |
| 317 | const ncb_sz_t sz1 = ncb_wrap(buf) - ptr; |
| 318 | memcpy(ptr, data, sz1); |
| 319 | memcpy(ncb_orig(buf), data + sz1, to_copy - sz1); |
| 320 | } |
| 321 | else { |
| 322 | memcpy(ptr, data, to_copy); |
| 323 | } |
| 324 | } |
| 325 | |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 326 | return to_copy; |
| 327 | } |
| 328 | |
| 329 | /* Fill the gap <blk> starting at <off> with new <data> of length <len>. <off> |
| 330 | * is relative to <blk> so it cannot be greater than the block size. |
| 331 | */ |
| 332 | static ncb_sz_t ncb_fill_gap_blk(const struct ncbuf *buf, |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 333 | const struct ncb_blk *blk, ncb_sz_t off, |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 334 | const char *data, ncb_sz_t len) |
| 335 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 336 | const ncb_sz_t to_copy = MIN(len, blk->sz - off); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 337 | char *ptr; |
| 338 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 339 | BUG_ON_HOT(off > blk->sz); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 340 | /* This can happens due to previous ncb_blk_find() usage. In this |
| 341 | * case the current fill is a noop. |
| 342 | */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 343 | if (off == blk->sz) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 344 | return 0; |
| 345 | |
| 346 | /* A new gap must be created if insertion stopped before gap end. */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 347 | if (off + to_copy < blk->sz) { |
| 348 | const ncb_sz_t gap_off = blk->off + off + to_copy; |
| 349 | const ncb_sz_t gap_sz = blk->sz - off - to_copy; |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 350 | |
| 351 | BUG_ON_HOT(!ncb_off_reduced(buf, gap_off) && |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 352 | blk->off + blk->sz - gap_off < NCB_GAP_MIN_SZ); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 353 | |
| 354 | /* write the new gap header unless this is a reduced gap. */ |
| 355 | if (!ncb_off_reduced(buf, gap_off)) { |
| 356 | char *gap_ptr = ncb_peek(buf, gap_off + NCB_GAP_SZ_OFF); |
| 357 | char *gap_data_ptr = ncb_peek(buf, gap_off + NCB_GAP_SZ_DATA_OFF); |
| 358 | |
| 359 | ncb_write_off(buf, gap_ptr, gap_sz); |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 360 | ncb_write_off(buf, gap_data_ptr, blk->sz_data); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | /* fill the gap with new data */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 365 | ptr = ncb_peek(buf, blk->off + off); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 366 | if (ptr + to_copy >= ncb_wrap(buf)) { |
| 367 | ncb_sz_t sz1 = ncb_wrap(buf) - ptr; |
| 368 | memcpy(ptr, data, sz1); |
| 369 | memcpy(ncb_orig(buf), data + sz1, to_copy - sz1); |
| 370 | } |
| 371 | else { |
| 372 | memcpy(ptr, data, to_copy); |
| 373 | } |
| 374 | |
| 375 | return to_copy; |
| 376 | } |
| 377 | |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 378 | /* ******** public API ******** */ |
| 379 | |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 380 | /* Initialize or reset <buf> by clearing all data. Its size is untouched. |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 381 | * Buffer is positioned to <head> offset. Use 0 to realign it. <buf> must not |
| 382 | * be NCBUF_NULL. |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 383 | */ |
| 384 | void ncb_init(struct ncbuf *buf, ncb_sz_t head) |
| 385 | { |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 386 | BUG_ON_HOT(ncb_is_null(buf)); |
| 387 | |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 388 | BUG_ON_HOT(head >= buf->size); |
| 389 | buf->head = head; |
| 390 | |
| 391 | ncb_write_off(buf, ncb_reserved(buf), 0); |
| 392 | ncb_write_off(buf, ncb_head(buf), ncb_size(buf)); |
| 393 | ncb_write_off(buf, ncb_peek(buf, sizeof(ncb_sz_t)), 0); |
| 394 | } |
| 395 | |
| 396 | /* Construct a ncbuf with all its parameters. */ |
| 397 | struct ncbuf ncb_make(char *area, ncb_sz_t size, ncb_sz_t head) |
| 398 | { |
| 399 | struct ncbuf buf; |
| 400 | |
| 401 | /* Ensure that there is enough space for the reserved space and data. |
| 402 | * This is the minimal value to not crash later. |
| 403 | */ |
| 404 | BUG_ON_HOT(size <= NCB_RESERVED_SZ); |
| 405 | |
| 406 | buf.area = area; |
| 407 | buf.size = size; |
| 408 | buf.head = head; |
| 409 | |
| 410 | return buf; |
| 411 | } |
| 412 | |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 413 | /* Returns the total number of bytes stored in whole <buf>. */ |
| 414 | ncb_sz_t ncb_total_data(const struct ncbuf *buf) |
| 415 | { |
| 416 | struct ncb_blk blk; |
| 417 | int total = 0; |
| 418 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 419 | for (blk = ncb_blk_first(buf); !ncb_blk_is_null(&blk); blk = ncb_blk_next(buf, &blk)) { |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 420 | if (!(blk.flag & NCB_BK_F_GAP)) |
| 421 | total += blk.sz; |
| 422 | } |
| 423 | |
| 424 | return total; |
| 425 | } |
| 426 | |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 427 | /* Returns true if there is no data anywhere in <buf>. */ |
| 428 | int ncb_is_empty(const struct ncbuf *buf) |
| 429 | { |
Amaury Denoyelle | f6dbdc1 | 2022-05-17 18:52:22 +0200 | [diff] [blame] | 430 | int first_data, first_gap; |
| 431 | |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 432 | if (ncb_is_null(buf)) |
| 433 | return 1; |
| 434 | |
Amaury Denoyelle | f6dbdc1 | 2022-05-17 18:52:22 +0200 | [diff] [blame] | 435 | first_data = ncb_read_off(buf, ncb_reserved(buf)); |
| 436 | BUG_ON_HOT(first_data > ncb_size(buf)); |
| 437 | /* Buffer is not empty if first data block is not nul. */ |
| 438 | if (first_data) |
| 439 | return 0; |
| 440 | |
| 441 | /* Head contains the first gap size if first data block is empty. */ |
| 442 | first_gap = ncb_read_off(buf, ncb_head(buf)); |
| 443 | BUG_ON_HOT(first_gap > ncb_size(buf)); |
| 444 | return first_gap == ncb_size(buf); |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* Returns true if no more data can be inserted in <buf>. */ |
| 448 | int ncb_is_full(const struct ncbuf *buf) |
| 449 | { |
Amaury Denoyelle | f6dbdc1 | 2022-05-17 18:52:22 +0200 | [diff] [blame] | 450 | int first_data; |
| 451 | |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 452 | if (ncb_is_null(buf)) |
| 453 | return 0; |
| 454 | |
Amaury Denoyelle | f6dbdc1 | 2022-05-17 18:52:22 +0200 | [diff] [blame] | 455 | /* First data block must cover whole buffer if full. */ |
| 456 | first_data = ncb_read_off(buf, ncb_reserved(buf)); |
| 457 | BUG_ON_HOT(first_data > ncb_size(buf)); |
| 458 | return first_data == ncb_size(buf); |
Amaury Denoyelle | 1b5f77f | 2022-05-09 09:37:27 +0200 | [diff] [blame] | 459 | } |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 460 | |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 461 | /* Returns true if <buf> contains data fragmented by gaps. */ |
| 462 | int ncb_is_fragmented(const struct ncbuf *buf) |
| 463 | { |
| 464 | struct ncb_blk data, gap; |
| 465 | |
| 466 | if (ncb_is_null(buf)) |
| 467 | return 0; |
| 468 | |
| 469 | /* check if buffer is empty or full */ |
| 470 | if (ncb_is_empty(buf) || ncb_is_full(buf)) |
| 471 | return 0; |
| 472 | |
| 473 | /* check that following gap is the last block */ |
| 474 | data = ncb_blk_first(buf); |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 475 | gap = ncb_blk_next(buf, &data); |
| 476 | return !ncb_blk_is_last(buf, &gap); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 479 | /* Returns the number of bytes of data available in <buf> starting at offset |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 480 | * <off> until the next gap or the buffer end. The counted data may wrapped if |
| 481 | * the buffer storage is not aligned. |
| 482 | */ |
| 483 | ncb_sz_t ncb_data(const struct ncbuf *buf, ncb_sz_t off) |
| 484 | { |
Amaury Denoyelle | 1194db2 | 2022-05-31 11:44:25 +0200 | [diff] [blame] | 485 | struct ncb_blk blk; |
| 486 | ncb_sz_t off_blk; |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 487 | |
Amaury Denoyelle | 1194db2 | 2022-05-31 11:44:25 +0200 | [diff] [blame] | 488 | if (ncb_is_null(buf)) |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 489 | return 0; |
| 490 | |
Amaury Denoyelle | 1194db2 | 2022-05-31 11:44:25 +0200 | [diff] [blame] | 491 | blk = ncb_blk_find(buf, off); |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 492 | off_blk = ncb_blk_off(&blk, off); |
Amaury Denoyelle | 1194db2 | 2022-05-31 11:44:25 +0200 | [diff] [blame] | 493 | |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 494 | /* if <off> at the frontier between two and <blk> is gap, retrieve the |
| 495 | * next data block. |
| 496 | */ |
| 497 | if (blk.flag & NCB_BK_F_GAP && off_blk == blk.sz && |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 498 | !ncb_blk_is_last(buf, &blk)) { |
| 499 | blk = ncb_blk_next(buf, &blk); |
| 500 | off_blk = ncb_blk_off(&blk, off); |
Amaury Denoyelle | d5d2ed9 | 2022-05-09 09:38:45 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (blk.flag & NCB_BK_F_GAP) |
| 504 | return 0; |
| 505 | |
| 506 | return blk.sz - off_blk; |
| 507 | } |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 508 | |
| 509 | /* Add a new block at <data> of size <len> in <buf> at offset <off>. |
| 510 | * |
| 511 | * Returns NCB_RET_OK on success. On error the following codes are returned : |
| 512 | * - NCB_RET_GAP_SIZE : cannot add data because the gap formed is too small |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 513 | * - NCB_RET_DATA_REJ : old data would be overwritten by different ones in |
| 514 | * NCB_ADD_COMPARE mode. |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 515 | */ |
| 516 | enum ncb_ret ncb_add(struct ncbuf *buf, ncb_sz_t off, |
Amaury Denoyelle | b830f0d | 2022-05-09 11:59:15 +0200 | [diff] [blame] | 517 | const char *data, ncb_sz_t len, enum ncb_add_mode mode) |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 518 | { |
| 519 | struct ncb_blk blk; |
| 520 | ncb_sz_t left = len; |
| 521 | enum ncb_ret ret; |
| 522 | char *new_sz; |
| 523 | |
| 524 | if (!len) |
| 525 | return NCB_RET_OK; |
| 526 | |
| 527 | BUG_ON_HOT(off + len > ncb_size(buf)); |
| 528 | |
| 529 | /* Get block where insertion begins. */ |
| 530 | blk = ncb_blk_find(buf, off); |
| 531 | |
| 532 | /* Check if insertion is possible. */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 533 | ret = ncb_check_insert(buf, &blk, off, data, len, mode); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 534 | if (ret != NCB_RET_OK) |
| 535 | return ret; |
| 536 | |
| 537 | if (blk.flag & NCB_BK_F_GAP) { |
| 538 | /* Reduce gap size if insertion begins in a gap. Gap data size |
| 539 | * is reset and will be recalculated during insertion. |
| 540 | */ |
| 541 | const ncb_sz_t gap_sz = off - blk.off; |
| 542 | BUG_ON_HOT(gap_sz < NCB_GAP_MIN_SZ); |
| 543 | |
| 544 | /* pointer to data size to increase. */ |
| 545 | new_sz = ncb_peek(buf, blk.off + NCB_GAP_SZ_DATA_OFF); |
| 546 | |
| 547 | ncb_write_off(buf, blk.sz_ptr, gap_sz); |
| 548 | ncb_write_off(buf, new_sz, 0); |
| 549 | } |
| 550 | else { |
| 551 | /* pointer to data size to increase. */ |
| 552 | new_sz = blk.sz_ptr; |
| 553 | } |
| 554 | |
| 555 | /* insert data */ |
| 556 | while (left) { |
| 557 | struct ncb_blk next; |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 558 | const ncb_sz_t off_blk = ncb_blk_off(&blk, off); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 559 | ncb_sz_t done; |
| 560 | |
| 561 | /* retrieve the next block. This is necessary to do this |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 562 | * before overwriting a gap. |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 563 | */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 564 | next = ncb_blk_next(buf, &blk); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 565 | |
| 566 | if (blk.flag & NCB_BK_F_GAP) { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 567 | done = ncb_fill_gap_blk(buf, &blk, off_blk, data, left); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 568 | |
| 569 | /* update the inserted data block size */ |
| 570 | if (off + done == blk.off + blk.sz) { |
| 571 | /* merge next data block if insertion reached gap end */ |
| 572 | ncb_inc_off(buf, new_sz, done + blk.sz_data); |
| 573 | } |
| 574 | else { |
| 575 | /* insertion stopped before gap end */ |
| 576 | ncb_inc_off(buf, new_sz, done); |
| 577 | } |
| 578 | } |
| 579 | else { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 580 | done = ncb_fill_data_blk(buf, &blk, off_blk, data, left, mode); |
Amaury Denoyelle | 077e096 | 2022-05-09 09:43:11 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | BUG_ON_HOT(done > blk.sz || done > left); |
| 584 | left -= done; |
| 585 | data += done; |
| 586 | off += done; |
| 587 | |
| 588 | blk = next; |
| 589 | } |
| 590 | |
| 591 | return NCB_RET_OK; |
| 592 | } |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 593 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 594 | /* Advance the head of <buf> to the offset <adv>. Data at the start of buffer |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 595 | * will be lost while some space will be formed at the end to be able to insert |
| 596 | * new data. |
| 597 | * |
Amaury Denoyelle | 7f0295f | 2022-11-18 15:54:40 +0100 | [diff] [blame] | 598 | * Returns NCB_RET_OK on success. It may return NCB_RET_GAP_SIZE if operation |
| 599 | * is rejected due to the formation of a too small gap in front. If advance is |
| 600 | * done only inside a data block it is guaranteed to succeed. |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 601 | */ |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 602 | enum ncb_ret ncb_advance(struct ncbuf *buf, ncb_sz_t adv) |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 603 | { |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 604 | struct ncb_blk start, last; |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 605 | ncb_sz_t off_blk; |
| 606 | ncb_sz_t first_data_sz; |
| 607 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 608 | BUG_ON_HOT(adv > ncb_size(buf)); |
| 609 | if (!adv) |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 610 | return NCB_RET_OK; |
| 611 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 612 | /* Special case if adv is full size. This is equivalent to a reset. */ |
| 613 | if (adv == ncb_size(buf)) { |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 614 | ncb_init(buf, buf->head); |
| 615 | return NCB_RET_OK; |
| 616 | } |
| 617 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 618 | start = ncb_blk_find(buf, adv); |
| 619 | |
| 620 | /* Special case if advance until the last block which is a GAP. The |
| 621 | * buffer will be left empty and is thus equivalent to a reset. |
| 622 | */ |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 623 | if (ncb_blk_is_last(buf, &start) && (start.flag & NCB_BK_F_GAP)) { |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 624 | ncb_sz_t new_head = buf->head + adv; |
| 625 | if (new_head >= buf->size) |
| 626 | new_head -= buf->size; |
| 627 | |
| 628 | ncb_init(buf, new_head); |
| 629 | return NCB_RET_OK; |
| 630 | } |
| 631 | |
| 632 | last = start; |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 633 | while (!ncb_blk_is_last(buf, &last)) |
| 634 | last = ncb_blk_next(buf, &last); |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 635 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 636 | off_blk = ncb_blk_off(&start, adv); |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 637 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 638 | if (start.flag & NCB_BK_F_GAP) { |
| 639 | /* If advance in a GAP, its new size must be big enough. */ |
| 640 | if (start.sz == off_blk) { |
| 641 | /* GAP removed. Buffer will start with following DATA block. */ |
| 642 | first_data_sz = start.sz_data; |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 643 | } |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 644 | else if (start.sz - off_blk < NCB_GAP_MIN_SZ) { |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 645 | return NCB_RET_GAP_SIZE; |
| 646 | } |
| 647 | else { |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 648 | /* Buffer will start with this GAP block. */ |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 649 | first_data_sz = 0; |
| 650 | } |
| 651 | } |
| 652 | else { |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 653 | /* If off_blk less than start.sz, the data block will becomes the |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 654 | * first block. If equal, the data block is completely removed |
| 655 | * and thus the following GAP will be the first block. |
| 656 | */ |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 657 | first_data_sz = start.sz - off_blk; |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 660 | if (last.flag & NCB_BK_F_GAP) { |
| 661 | /* Extend last GAP unless this is a reduced gap. */ |
| 662 | if (!(last.flag & NCB_BK_F_FIN) || last.sz + adv >= NCB_GAP_MIN_SZ) { |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 663 | /* use .st instead of .sz_ptr which can be NULL if reduced gap */ |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 664 | ncb_write_off(buf, last.st, last.sz + adv); |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 665 | ncb_write_off(buf, ncb_peek(buf, last.off + NCB_GAP_SZ_DATA_OFF), 0); |
| 666 | } |
| 667 | } |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 668 | else { |
| 669 | /* Insert a GAP after the last DATA block. */ |
| 670 | if (adv >= NCB_GAP_MIN_SZ) { |
| 671 | ncb_write_off(buf, ncb_peek(buf, last.off + last.sz + NCB_GAP_SZ_OFF), adv); |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 672 | ncb_write_off(buf, ncb_peek(buf, last.off + last.sz + NCB_GAP_SZ_DATA_OFF), 0); |
| 673 | } |
| 674 | } |
| 675 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 676 | /* Advance head and update reserved header with new first data size. */ |
| 677 | buf->head += adv; |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 678 | if (buf->head >= buf->size) |
| 679 | buf->head -= buf->size; |
| 680 | ncb_write_off(buf, ncb_reserved(buf), first_data_sz); |
| 681 | |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 682 | /* If advance in a GAP, reduce its size. */ |
| 683 | if (start.flag & NCB_BK_F_GAP && !first_data_sz) { |
| 684 | ncb_write_off(buf, ncb_head(buf), start.sz - off_blk); |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 685 | /* Recopy the block sz_data at the new position. */ |
Amaury Denoyelle | ca21c76 | 2022-05-17 18:52:39 +0200 | [diff] [blame] | 686 | ncb_write_off(buf, ncb_peek(buf, NCB_GAP_SZ_DATA_OFF), start.sz_data); |
Amaury Denoyelle | df25acf | 2022-05-04 16:47:09 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return NCB_RET_OK; |
| 690 | } |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 691 | |
| 692 | /* ******** testing API ******** */ |
| 693 | /* To build it : |
Amaury Denoyelle | f46393a | 2022-05-16 11:09:05 +0200 | [diff] [blame] | 694 | * gcc -Wall -DSTANDALONE -lasan -I./include -o ncbuf src/ncbuf.c |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 695 | */ |
| 696 | #ifdef STANDALONE |
| 697 | |
| 698 | int ncb_print = 0; |
| 699 | |
| 700 | static void ncbuf_printf(char *str, ...) |
| 701 | { |
| 702 | va_list args; |
| 703 | |
| 704 | va_start(args, str); |
| 705 | if (ncb_print) |
| 706 | vfprintf(stderr, str, args); |
| 707 | va_end(args); |
| 708 | } |
| 709 | |
| 710 | struct rand_off { |
| 711 | struct list el; |
| 712 | ncb_sz_t off; |
| 713 | ncb_sz_t len; |
| 714 | }; |
| 715 | |
| 716 | static struct rand_off *ncb_generate_rand_off(const struct ncbuf *buf) |
| 717 | { |
| 718 | struct rand_off *roff; |
Tim Duesterhus | 9fb57e8 | 2022-06-01 21:58:37 +0200 | [diff] [blame] | 719 | roff = calloc(1, sizeof(*roff)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 720 | BUG_ON(!roff); |
| 721 | |
| 722 | roff->off = rand() % (ncb_size(buf)); |
| 723 | if (roff->off > 0 && roff->off < NCB_GAP_MIN_SZ) |
| 724 | roff->off = 0; |
| 725 | |
| 726 | roff->len = rand() % (ncb_size(buf) - roff->off + 1); |
| 727 | |
| 728 | return roff; |
| 729 | } |
| 730 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 731 | static void ncb_print_blk(const struct ncb_blk *blk) |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 732 | { |
| 733 | if (ncb_print) { |
Amaury Denoyelle | f46393a | 2022-05-16 11:09:05 +0200 | [diff] [blame] | 734 | fprintf(stderr, "%s(%s): %2u/%u.\n", |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 735 | blk->flag & NCB_BK_F_GAP ? "GAP " : "DATA", |
| 736 | blk->flag & NCB_BK_F_FIN ? "F" : "-", blk->off, blk->sz); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 740 | static int ncb_is_null_blk(const struct ncb_blk *blk) |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 741 | { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 742 | return !blk->st; |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static void ncb_loop(const struct ncbuf *buf) |
| 746 | { |
| 747 | struct ncb_blk blk; |
| 748 | |
| 749 | blk = ncb_blk_first(buf); |
| 750 | do { |
Amaury Denoyelle | 17e20e8 | 2022-11-28 11:06:42 +0100 | [diff] [blame] | 751 | ncb_print_blk(&blk); |
| 752 | blk = ncb_blk_next(buf, &blk); |
| 753 | } while (!ncb_is_null_blk(&blk)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 754 | |
| 755 | ncbuf_printf("\n"); |
| 756 | } |
| 757 | |
| 758 | static void ncbuf_print_buf(struct ncbuf *b, ncb_sz_t len, |
| 759 | unsigned char *area, int line) |
| 760 | { |
| 761 | int i; |
| 762 | |
| 763 | ncbuf_printf("buffer status at line %d\n", line); |
| 764 | for (i = 0; i < len; ++i) { |
| 765 | ncbuf_printf("%02x.", area[i]); |
| 766 | if (i && i % 32 == 31) ncbuf_printf("\n"); |
| 767 | else if (i && i % 8 == 7) ncbuf_printf(" "); |
| 768 | } |
| 769 | ncbuf_printf("\n"); |
| 770 | |
| 771 | ncb_loop(b); |
| 772 | |
| 773 | if (ncb_print) |
| 774 | getchar(); |
| 775 | } |
| 776 | |
| 777 | static struct ncbuf b; |
| 778 | static unsigned char *bufarea = NULL; |
| 779 | static ncb_sz_t bufsize = 16384; |
| 780 | static ncb_sz_t bufhead = 15; |
| 781 | |
| 782 | #define NCB_INIT(buf) \ |
| 783 | if ((reset)) { memset(bufarea, 0xaa, bufsize); } \ |
| 784 | ncb_init(buf, bufhead); \ |
| 785 | ncbuf_print_buf(&b, bufsize, bufarea, __LINE__); |
| 786 | |
| 787 | #define NCB_ADD_EQ(buf, off, data, sz, mode, ret) \ |
| 788 | BUG_ON(ncb_add((buf), (off), (data), (sz), (mode)) != (ret)); \ |
| 789 | ncbuf_print_buf(buf, bufsize, bufarea, __LINE__); |
| 790 | |
| 791 | #define NCB_ADD_NEQ(buf, off, data, sz, mode, ret) \ |
| 792 | BUG_ON(ncb_add((buf), (off), (data), (sz), (mode)) == (ret)); \ |
| 793 | ncbuf_print_buf(buf, bufsize, bufarea, __LINE__); |
| 794 | |
| 795 | #define NCB_ADVANCE_EQ(buf, off, ret) \ |
| 796 | BUG_ON(ncb_advance((buf), (off)) != (ret)); \ |
| 797 | ncbuf_print_buf(buf, bufsize, bufarea, __LINE__); |
| 798 | |
| 799 | #define NCB_TOTAL_DATA_EQ(buf, data) \ |
| 800 | BUG_ON(ncb_total_data((buf)) != (data)); |
| 801 | |
| 802 | #define NCB_DATA_EQ(buf, off, data) \ |
| 803 | BUG_ON(ncb_data((buf), (off)) != (data)); |
| 804 | |
| 805 | static int ncbuf_test(ncb_sz_t head, int reset, int print_delay) |
| 806 | { |
| 807 | char *data0, data1[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; |
| 808 | struct list list = LIST_HEAD_INIT(list); |
| 809 | struct rand_off *roff, *roff_tmp; |
| 810 | enum ncb_ret ret; |
| 811 | |
| 812 | data0 = malloc(bufsize); |
Aurelien DARRAGON | ceb13b5 | 2023-05-11 15:28:20 +0200 | [diff] [blame] | 813 | BUG_ON(!data0); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 814 | memset(data0, 0xff, bufsize); |
| 815 | |
| 816 | bufarea = malloc(bufsize); |
Aurelien DARRAGON | ceb13b5 | 2023-05-11 15:28:20 +0200 | [diff] [blame] | 817 | BUG_ON(!bufarea); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 818 | |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 819 | fprintf(stderr, "running unit tests\n"); |
| 820 | |
| 821 | b = NCBUF_NULL; |
| 822 | BUG_ON(!ncb_is_null(&b)); |
| 823 | NCB_DATA_EQ(&b, 0, 0); |
| 824 | NCB_TOTAL_DATA_EQ(&b, 0); |
| 825 | BUG_ON(ncb_size(&b) != 0); |
| 826 | BUG_ON(!ncb_is_empty(&b)); |
| 827 | BUG_ON(ncb_is_full(&b)); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 828 | BUG_ON(ncb_is_fragmented(&b)); |
Amaury Denoyelle | 48fbad4 | 2022-05-16 11:09:29 +0200 | [diff] [blame] | 829 | |
Amaury Denoyelle | f46393a | 2022-05-16 11:09:05 +0200 | [diff] [blame] | 830 | b.area = (char *)bufarea; |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 831 | b.size = bufsize; |
| 832 | b.head = head; |
| 833 | NCB_INIT(&b); |
| 834 | |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 835 | /* insertion test suite */ |
| 836 | NCB_INIT(&b); |
| 837 | NCB_DATA_EQ(&b, 0, 0); NCB_DATA_EQ(&b, bufsize - NCB_RESERVED_SZ - 1, 0); /* first and last offset */ |
| 838 | NCB_ADD_EQ(&b, 24, data0, 9, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 24, 9); |
| 839 | /* insert new data at the same offset as old */ |
| 840 | NCB_ADD_EQ(&b, 24, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 24, 16); |
| 841 | |
| 842 | NCB_INIT(&b); NCB_DATA_EQ(&b, 0, 0); |
| 843 | NCB_ADD_EQ(&b, 0, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 0, 16); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 844 | BUG_ON(ncb_is_fragmented(&b)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 845 | NCB_ADD_EQ(&b, 24, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 0, 16); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 846 | BUG_ON(!ncb_is_fragmented(&b)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 847 | /* insert data overlapping two data blocks and a gap */ |
| 848 | NCB_ADD_EQ(&b, 12, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 0, 40); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 849 | BUG_ON(ncb_is_fragmented(&b)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 850 | |
| 851 | NCB_INIT(&b); |
| 852 | NCB_ADD_EQ(&b, 32, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 0, 0); NCB_DATA_EQ(&b, 16, 0); NCB_DATA_EQ(&b, 32, 16); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 853 | BUG_ON(!ncb_is_fragmented(&b)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 854 | NCB_ADD_EQ(&b, 0, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 0, 16); NCB_DATA_EQ(&b, 16, 0); NCB_DATA_EQ(&b, 32, 16); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 855 | BUG_ON(!ncb_is_fragmented(&b)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 856 | /* insert data to exactly cover a gap between two data blocks */ |
| 857 | NCB_ADD_EQ(&b, 16, data0, 16, NCB_ADD_PRESERVE, NCB_RET_OK); NCB_DATA_EQ(&b, 0, 48); NCB_DATA_EQ(&b, 16, 32); NCB_DATA_EQ(&b, 32, 16); |
Amaury Denoyelle | e0a92a7 | 2022-07-01 14:45:41 +0200 | [diff] [blame] | 858 | BUG_ON(ncb_is_fragmented(&b)); |
Amaury Denoyelle | eeeeed4 | 2022-05-04 16:51:19 +0200 | [diff] [blame] | 859 | |
| 860 | NCB_INIT(&b); |
| 861 | NCB_ADD_EQ(&b, 0, data0, 8, NCB_ADD_PRESERVE, NCB_RET_OK); |
| 862 | /* this insertion must be rejected because of minimal gap size */ |
| 863 | NCB_ADD_EQ(&b, 10, data0, 8, NCB_ADD_PRESERVE, NCB_RET_GAP_SIZE); |
| 864 | |
| 865 | /* Test reduced gap support */ |
| 866 | NCB_INIT(&b); |
| 867 | /* this insertion will form a reduced gap */ |
| 868 | NCB_ADD_EQ(&b, 0, data0, bufsize - (NCB_GAP_MIN_SZ - 1), NCB_ADD_COMPARE, NCB_RET_OK); |
| 869 | |
| 870 | /* Test the various insertion mode */ |
| 871 | NCB_INIT(&b); |
| 872 | NCB_ADD_EQ(&b, 10, data1, 16, NCB_ADD_PRESERVE, NCB_RET_OK); |
| 873 | NCB_ADD_EQ(&b, 12, data1, 16, NCB_ADD_COMPARE, NCB_RET_DATA_REJ); |
| 874 | NCB_ADD_EQ(&b, 12, data1, 16, NCB_ADD_PRESERVE, NCB_RET_OK); BUG_ON(*ncb_peek(&b, 12) != data1[2]); |
| 875 | NCB_ADD_EQ(&b, 12, data1, 16, NCB_ADD_OVERWRT, NCB_RET_OK); BUG_ON(*ncb_peek(&b, 12) == data1[2]); |
| 876 | |
| 877 | /* advance test suite */ |
| 878 | NCB_INIT(&b); |
| 879 | NCB_ADVANCE_EQ(&b, 10, NCB_RET_OK); /* advance in an empty buffer; this ensures we do not leave an empty DATA in the middle of the buffer */ |
| 880 | NCB_ADVANCE_EQ(&b, ncb_size(&b) - 2, NCB_RET_OK); |
| 881 | |
| 882 | NCB_INIT(&b); |
| 883 | /* first fill the buffer */ |
| 884 | NCB_ADD_EQ(&b, 0, data0, bufsize - NCB_RESERVED_SZ, NCB_ADD_COMPARE, NCB_RET_OK); |
| 885 | /* delete 2 bytes : a reduced gap must be created */ |
| 886 | NCB_ADVANCE_EQ(&b, 2, NCB_RET_OK); NCB_TOTAL_DATA_EQ(&b, ncb_size(&b) - 2); |
| 887 | /* delete 1 byte : extend the reduced gap */ |
| 888 | NCB_ADVANCE_EQ(&b, 1, NCB_RET_OK); NCB_TOTAL_DATA_EQ(&b, ncb_size(&b) - 3); |
| 889 | /* delete 5 bytes : a full gap must be present */ |
| 890 | NCB_ADVANCE_EQ(&b, 5, NCB_RET_OK); NCB_TOTAL_DATA_EQ(&b, ncb_size(&b) - 8); |
| 891 | /* completely clear the buffer */ |
| 892 | NCB_ADVANCE_EQ(&b, bufsize - NCB_RESERVED_SZ, NCB_RET_OK); NCB_TOTAL_DATA_EQ(&b, 0); |
| 893 | |
| 894 | |
| 895 | NCB_INIT(&b); |
| 896 | NCB_ADD_EQ(&b, 10, data0, 10, NCB_ADD_PRESERVE, NCB_RET_OK); |
| 897 | NCB_ADVANCE_EQ(&b, 2, NCB_RET_OK); /* reduce a gap in front of the buffer */ |
| 898 | NCB_ADVANCE_EQ(&b, 1, NCB_RET_GAP_SIZE); /* reject */ |
| 899 | NCB_ADVANCE_EQ(&b, 8, NCB_RET_OK); /* remove completely the gap */ |
| 900 | NCB_ADVANCE_EQ(&b, 8, NCB_RET_OK); /* remove inside the data */ |
| 901 | NCB_ADVANCE_EQ(&b, 10, NCB_RET_OK); /* remove completely the data */ |
| 902 | |
| 903 | fprintf(stderr, "first random pass\n"); |
| 904 | NCB_INIT(&b); |
| 905 | |
| 906 | /* generate randon data offsets until the buffer is full */ |
| 907 | while (!ncb_is_full(&b)) { |
| 908 | roff = ncb_generate_rand_off(&b); |
| 909 | LIST_INSERT(&list, &roff->el); |
| 910 | |
| 911 | ret = ncb_add(&b, roff->off, data0, roff->len, NCB_ADD_COMPARE); |
| 912 | BUG_ON(ret == NCB_RET_DATA_REJ); |
| 913 | ncbuf_print_buf(&b, bufsize, bufarea, __LINE__); |
| 914 | usleep(print_delay); |
| 915 | } |
| 916 | |
| 917 | fprintf(stderr, "buf full, prepare for reverse random\n"); |
| 918 | ncbuf_print_buf(&b, bufsize, bufarea, __LINE__); |
| 919 | |
| 920 | /* insert the previously generated random offsets in the reverse order. |
| 921 | * At the end, the buffer should be full. |
| 922 | */ |
| 923 | NCB_INIT(&b); |
| 924 | list_for_each_entry_safe(roff, roff_tmp, &list, el) { |
| 925 | int full = ncb_is_full(&b); |
| 926 | if (!full) { |
| 927 | ret = ncb_add(&b, roff->off, data0, roff->len, NCB_ADD_COMPARE); |
| 928 | BUG_ON(ret == NCB_RET_DATA_REJ); |
| 929 | ncbuf_print_buf(&b, bufsize, bufarea, __LINE__); |
| 930 | usleep(print_delay); |
| 931 | } |
| 932 | |
| 933 | LIST_DELETE(&roff->el); |
| 934 | free(roff); |
| 935 | } |
| 936 | |
| 937 | if (!ncb_is_full(&b)) |
| 938 | abort(); |
| 939 | |
| 940 | fprintf(stderr, "done\n"); |
| 941 | |
| 942 | free(bufarea); |
| 943 | free(data0); |
| 944 | |
| 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | int main(int argc, char **argv) |
| 949 | { |
| 950 | int reset = 0; |
| 951 | int print_delay = 100000; |
| 952 | char c; |
| 953 | |
| 954 | opterr = 0; |
| 955 | while ((c = getopt(argc, argv, "h:s:rp::")) != -1) { |
| 956 | switch (c) { |
| 957 | case 'h': |
| 958 | bufhead = atoi(optarg); |
| 959 | break; |
| 960 | case 's': |
| 961 | bufsize = atoi(optarg); |
| 962 | if (bufsize < 64) { |
| 963 | fprintf(stderr, "bufsize should be at least 64 bytes for unit test suite\n"); |
| 964 | exit(127); |
| 965 | } |
| 966 | break; |
| 967 | case 'r': |
| 968 | reset = 1; |
| 969 | break; |
| 970 | case 'p': |
| 971 | if (optarg) |
| 972 | print_delay = atoi(optarg); |
| 973 | ncb_print = 1; |
| 974 | break; |
| 975 | case '?': |
| 976 | default: |
| 977 | fprintf(stderr, "usage: %s [-r] [-s bufsize] [-h bufhead] [-p <delay_msec>]\n", argv[0]); |
| 978 | exit(127); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | ncbuf_test(bufhead, reset, print_delay); |
| 983 | return EXIT_SUCCESS; |
| 984 | } |
| 985 | |
| 986 | #endif /* STANDALONE */ |