server: Use ringbuffer for socket backlog

Currently, the socket handler uses a linear buffer for the backlog data;
this means we need to shift up to 128kB of data after each socket
write().

This change introduces a single-producer-multiple-consumer ringbuffer,
to avoid the need for memmove()ing data around; we can simply update
pointers instead of shifting data.

We add this as a new file (ringbuffer.c), to make it a little more
modular. To mitigate the risk of subtle pointer arithmetic issues, we
add a set of tests too.

Change-Id: Ib7c5151d3cf1f588436f5461000b6fed22d0681c
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/test/test-ringbuffer-boundary-poll.c b/test/test-ringbuffer-boundary-poll.c
new file mode 100644
index 0000000..6f6ddc7
--- /dev/null
+++ b/test/test-ringbuffer-boundary-poll.c
@@ -0,0 +1,51 @@
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include "ringbuffer.c"
+#include "ringbuffer-test-utils.c"
+
+void test_boundary_poll(void)
+{
+	uint8_t in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
+	struct rb_test_ctx _ctx, *ctx = &_ctx;
+	struct ringbuffer *rb;
+	int rc;
+
+	ringbuffer_test_context_init(ctx);
+
+	rb = ringbuffer_init(10);
+
+	ctx->rbc = ringbuffer_consumer_register(rb,
+			ringbuffer_poll_append_all, ctx);
+
+	/* don't consume initial data in the poll callback */
+	ctx->ignore_poll = true;
+
+	/* queue and dequeue, so our tail is non-zero */
+	ringbuffer_queue(rb, in_buf, sizeof(in_buf));
+	ringbuffer_dequeue_commit(ctx->rbc, sizeof(in_buf));
+
+	/* start queueing data */
+	ctx->ignore_poll = false;
+
+	/* ensure we're getting the second batch of data back */
+	in_buf[0] = 'A';
+
+	rc = ringbuffer_queue(rb, in_buf, sizeof(in_buf));
+	assert(!rc);
+
+	assert(ctx->count == 1);
+	assert(ctx->len == sizeof(in_buf));
+	assert(!memcmp(in_buf, ctx->data, ctx->len));
+
+	ringbuffer_fini(rb);
+	ringbuffer_test_context_fini(ctx);
+}
+
+int main(void)
+{
+	test_boundary_poll();
+	return EXIT_SUCCESS;
+}