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-contained-read.c b/test/test-ringbuffer-contained-read.c
new file mode 100644
index 0000000..37df3cf
--- /dev/null
+++ b/test/test-ringbuffer-contained-read.c
@@ -0,0 +1,34 @@
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include "ringbuffer.c"
+#include "ringbuffer-test-utils.c"
+
+void test_contained_read(void)
+{
+	uint8_t *out_buf, in_buf[] = { 'a', 'b', 'c' };
+	struct ringbuffer_consumer *rbc;
+	struct ringbuffer *rb;
+	size_t len;
+	int rc;
+
+	rb = ringbuffer_init(10);
+	rbc = ringbuffer_consumer_register(rb, ringbuffer_poll_nop, NULL);
+
+	rc = ringbuffer_queue(rb, in_buf, sizeof(in_buf));
+	assert(!rc);
+
+	len = ringbuffer_dequeue_peek(rbc, 0, &out_buf);
+	assert(len == sizeof(in_buf));
+	assert(!memcmp(in_buf, out_buf, sizeof(in_buf)));
+
+	ringbuffer_fini(rb);
+}
+
+int main(void)
+{
+	test_contained_read();
+	return EXIT_SUCCESS;
+}