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