Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 1 | |
| 2 | struct rb_test_ctx { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 3 | struct ringbuffer_consumer *rbc; |
| 4 | bool ignore_poll; |
| 5 | bool force_only; |
| 6 | int count; |
| 7 | uint8_t *data; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 8 | size_t len; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 9 | }; |
| 10 | |
| 11 | void ringbuffer_test_context_init(struct rb_test_ctx *ctx) |
| 12 | { |
| 13 | ctx->count = 0; |
| 14 | ctx->data = NULL; |
| 15 | ctx->len = 0; |
| 16 | ctx->ignore_poll = false; |
| 17 | ctx->force_only = false; |
| 18 | } |
| 19 | |
| 20 | void ringbuffer_test_context_fini(struct rb_test_ctx *ctx) |
| 21 | { |
| 22 | free(ctx->data); |
| 23 | } |
| 24 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 25 | enum ringbuffer_poll_ret ringbuffer_poll_nop(void *data __attribute__((unused)), |
| 26 | size_t force_len |
| 27 | __attribute__((unused))) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 28 | { |
| 29 | return RINGBUFFER_POLL_OK; |
| 30 | } |
| 31 | |
| 32 | enum ringbuffer_poll_ret ringbuffer_poll_append_all(void *data, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 33 | size_t force_len) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 34 | { |
| 35 | struct rb_test_ctx *ctx = data; |
| 36 | size_t len, total_len; |
| 37 | uint8_t *buf; |
| 38 | |
| 39 | if (ctx->ignore_poll) |
| 40 | return RINGBUFFER_POLL_OK; |
| 41 | |
| 42 | if (ctx->force_only && !force_len) |
| 43 | return RINGBUFFER_POLL_OK; |
| 44 | |
| 45 | ctx->count++; |
| 46 | |
| 47 | total_len = 0; |
| 48 | for (;;) { |
| 49 | len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf); |
| 50 | if (!len) |
| 51 | break; |
| 52 | |
| 53 | if (ctx->force_only && total_len + len > force_len) |
| 54 | len = force_len - total_len; |
| 55 | |
| 56 | ctx->data = realloc(ctx->data, ctx->len + len); |
| 57 | memcpy(ctx->data + ctx->len, buf, len); |
| 58 | ctx->len += len; |
| 59 | total_len += len; |
| 60 | |
| 61 | if (ctx->force_only && total_len >= force_len) |
| 62 | break; |
| 63 | } |
| 64 | ringbuffer_dequeue_commit(ctx->rbc, total_len); |
| 65 | |
| 66 | return RINGBUFFER_POLL_OK; |
| 67 | } |
| 68 | |
| 69 | void ringbuffer_dump(struct ringbuffer *rb) |
| 70 | { |
| 71 | struct ringbuffer_consumer *rbc; |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame] | 72 | size_t i; |
| 73 | int j; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 74 | |
| 75 | printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 76 | rb->n_consumers == 1 ? "" : "s"); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 77 | |
| 78 | for (i = 0; i < rb->size; i++) { |
| 79 | bool has_consumer = false; |
| 80 | const char *prefix = ""; |
| 81 | |
| 82 | if (rb->tail == i) |
| 83 | prefix = "tail=>"; |
| 84 | |
| 85 | printf("%6s %02x", prefix, rb->buf[i]); |
| 86 | for (j = 0; j < rb->n_consumers; j++) { |
| 87 | rbc = rb->consumers[j]; |
| 88 | if (rbc->pos != i) |
| 89 | continue; |
| 90 | if (!has_consumer) |
| 91 | printf(" <="); |
| 92 | printf("c[%d],len=%zd ", j, ringbuffer_len(rbc)); |
| 93 | has_consumer = true; |
| 94 | } |
| 95 | printf("\n"); |
| 96 | } |
| 97 | } |