| #include <stdbool.h> |
| #include <stdlib.h> |
| |
| #include "console-server.h" |
| |
| struct rb_test_ctx { |
| struct ringbuffer_consumer *rbc; |
| bool ignore_poll; |
| bool force_only; |
| int count; |
| uint8_t *data; |
| size_t len; |
| }; |
| |
| void ringbuffer_test_context_init(struct rb_test_ctx *ctx) |
| { |
| ctx->count = 0; |
| ctx->data = NULL; |
| ctx->len = 0; |
| ctx->ignore_poll = false; |
| ctx->force_only = false; |
| } |
| |
| void ringbuffer_test_context_fini(struct rb_test_ctx *ctx) |
| { |
| free(ctx->data); |
| } |
| |
| enum ringbuffer_poll_ret ringbuffer_poll_nop(void *data __attribute__((unused)), |
| size_t force_len |
| __attribute__((unused))) |
| { |
| return RINGBUFFER_POLL_OK; |
| } |
| |
| enum ringbuffer_poll_ret ringbuffer_poll_append_all(void *data, |
| size_t force_len) |
| { |
| struct rb_test_ctx *ctx = data; |
| size_t len; |
| size_t total_len; |
| uint8_t *buf; |
| |
| if (ctx->ignore_poll) { |
| return RINGBUFFER_POLL_OK; |
| } |
| |
| if (ctx->force_only && !force_len) { |
| return RINGBUFFER_POLL_OK; |
| } |
| |
| ctx->count++; |
| |
| total_len = 0; |
| for (;;) { |
| len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf); |
| if (!len) { |
| break; |
| } |
| |
| if (ctx->force_only && total_len + len > force_len) { |
| len = force_len - total_len; |
| } |
| |
| ctx->data = realloc(ctx->data, ctx->len + len); |
| memcpy(ctx->data + ctx->len, buf, len); |
| ctx->len += len; |
| total_len += len; |
| |
| if (ctx->force_only && total_len >= force_len) { |
| break; |
| } |
| } |
| ringbuffer_dequeue_commit(ctx->rbc, total_len); |
| |
| return RINGBUFFER_POLL_OK; |
| } |
| |
| void ringbuffer_dump(struct ringbuffer *rb) |
| { |
| struct ringbuffer_consumer *rbc; |
| size_t i; |
| int j; |
| |
| printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers, |
| rb->n_consumers == 1 ? "" : "s"); |
| |
| for (i = 0; i < rb->size; i++) { |
| bool has_consumer = false; |
| const char *prefix = ""; |
| |
| if (rb->tail == i) { |
| prefix = "tail=>"; |
| } |
| |
| printf("%6s %02x", prefix, rb->buf[i]); |
| for (j = 0; j < rb->n_consumers; j++) { |
| rbc = rb->consumers[j]; |
| if (rbc->pos != i) { |
| continue; |
| } |
| if (!has_consumer) { |
| printf(" <="); |
| } |
| printf("c[%d],len=%zd ", j, ringbuffer_len(rbc)); |
| has_consumer = true; |
| } |
| printf("\n"); |
| } |
| } |