Add simple aligned alloc helper

We don't have posix_memalign() on some Solaris and Darwin, so just
add a poor mans implementation of it. Outside of arch code where
we have proper posix_memalign(), this is only used to ensure good
alignment of the io_u in the core code.

Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
diff --git a/memalign.c b/memalign.c
new file mode 100644
index 0000000..87667b4
--- /dev/null
+++ b/memalign.c
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <assert.h>
+
+#include "memalign.h"
+
+struct align_footer {
+	unsigned int offset;
+};
+
+#define PTR_ALIGN(ptr, mask)	\
+	(char *) (((unsigned long) ((ptr) + (mask)) & ~(mask)))
+
+void *fio_memalign(size_t alignment, size_t size)
+{
+	struct align_footer *f;
+	void *ptr, *ret = NULL;
+
+	assert(!(alignment & (alignment - 1)));
+
+	ptr = malloc(size + alignment + size + sizeof(*f) - 1);
+	if (ptr) {
+		ret = PTR_ALIGN(ptr, alignment);
+		f = ret + size;
+		f->offset = (unsigned long) ret - (unsigned long) ptr;
+	}
+
+	return ret;
+}
+
+void fio_memfree(void *ptr, size_t size)
+{
+	struct align_footer *f = ptr + size;
+
+	free(ptr - f->offset);
+}