windowsaio: initialize and map windowsaio IO structure to io_u

Instead of searching for a free entry everytime we enter
queue, do this when the io_u is setup. It's cleaner and
faster.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/backend.c b/backend.c
index faa861c..1b5c2eb 100644
--- a/backend.c
+++ b/backend.c
@@ -803,6 +803,10 @@
 		io_u = flist_entry(entry, struct io_u, list);
 
 		flist_del(&io_u->list);
+
+		if (td->io_ops->io_u_free)
+			td->io_ops->io_u_free(td, io_u);
+
 		fio_memfree(io_u, sizeof(*io_u));
 	}
 
@@ -885,6 +889,16 @@
 		io_u->index = i;
 		io_u->flags = IO_U_F_FREE;
 		flist_add(&io_u->list, &td->io_u_freelist);
+
+		if (td->io_ops->io_u_init) {
+			int ret = td->io_ops->io_u_init(td, io_u);
+
+			if (ret) {
+				log_err("fio: failed to init engine data: %d\n", ret);
+				return 1;
+			}
+		}
+
 		p += max_bs;
 	}
 
diff --git a/engines/windowsaio.c b/engines/windowsaio.c
index db75730..96ec0f4 100644
--- a/engines/windowsaio.c
+++ b/engines/windowsaio.c
@@ -20,11 +20,9 @@
 	OVERLAPPED o;
 	struct io_u *io_u;
 	BOOL io_complete;
-	BOOL io_free;
 };
 
 struct windowsaio_data {
-	struct fio_overlapped *ovls;
 	struct io_u **aio_events;
 	HANDLE iothread;
 	HANDLE iocomplete_event;
@@ -139,7 +137,6 @@
 	struct windowsaio_data *wd;
 	HANDLE hKernel32Dll;
 	int rc = 0;
-	int i;
 
 	wd = malloc(sizeof(struct windowsaio_data));
 	if (wd != NULL)
@@ -154,25 +151,6 @@
 	}
 
 	if (!rc) {
-		wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
-		if (wd->ovls == NULL)
-			rc = 1;
-	}
-
-	if (!rc) {
-		for (i = 0; i < td->o.iodepth; i++) {
-			wd->ovls[i].io_free = TRUE;
-			wd->ovls[i].io_complete = FALSE;
-
-			wd->ovls[i].o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-			if (wd->ovls[i].o.hEvent == NULL) {
-				rc = 1;
-				break;
-			}
-		}
-	}
-
-	if (!rc) {
 		/* Create an auto-reset event */
 		wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
 		if (wd->iocomplete_event == NULL)
@@ -181,8 +159,6 @@
 
 	if (rc) {
 		if (wd != NULL) {
-			if (wd->ovls != NULL)
-				free(wd->ovls);
 			if (wd->aio_events != NULL)
 				free(wd->aio_events);
 
@@ -199,7 +175,6 @@
 
 static void fio_windowsaio_cleanup(struct thread_data *td)
 {
-	int i;
 	struct windowsaio_data *wd;
 
 	wd = td->io_ops->data;
@@ -211,12 +186,7 @@
 		CloseHandle(wd->iothread);
 		CloseHandle(wd->iocomplete_event);
 
-		for (i = 0; i < td->o.iodepth; i++) {
-			CloseHandle(wd->ovls[i].o.hEvent);
-		}
-
 		free(wd->aio_events);
-		free(wd->ovls);
 		free(wd);
 
 		td->io_ops->data = NULL;
@@ -364,7 +334,6 @@
 
 			if (fov->io_complete) {
 				fov->io_complete = FALSE;
-				fov->io_free  = TRUE;
 				ResetEvent(fov->o.hEvent);
 				wd->aio_events[dequeued] = io_u;
 				dequeued++;
@@ -389,32 +358,18 @@
 
 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
 {
-	LPOVERLAPPED lpOvl = NULL;
-	struct windowsaio_data *wd;
+	struct fio_overlapped *o = io_u->engine_data;
+	LPOVERLAPPED lpOvl = &o->o;
 	DWORD iobytes;
 	BOOL success = FALSE;
-	int index;
 	int rc = FIO_Q_COMPLETED;
 
 	fio_ro_check(td, io_u);
 
-	wd = td->io_ops->data;
-
-	for (index = 0; index < td->o.iodepth; index++) {
-		if (wd->ovls[index].io_free)
-			break;
-	}
-
-	assert(index < td->o.iodepth);
-
-	wd->ovls[index].io_free = FALSE;
-	wd->ovls[index].io_u = io_u;
-	lpOvl = &wd->ovls[index].o;
 	lpOvl->Internal = STATUS_PENDING;
 	lpOvl->InternalHigh = 0;
 	lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
 	lpOvl->OffsetHigh = io_u->offset >> 32;
-	io_u->engine_data = &wd->ovls[index];
 
 	switch (io_u->ddir) {
 	case DDIR_WRITE:
@@ -510,6 +465,34 @@
 	return rc;
 }
 
+static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
+{
+	struct fio_overlapped *o = io_u->engine_data;
+
+	if (o) {
+		CloseHandle(o->o.hEvent);
+		io_u->engine_data = NULL;
+		free(o);
+	}
+}
+
+static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
+{
+	struct fio_overlapped *o;
+
+	o = malloc(sizeof(*o));
+	o->io_complete = FALSE:
+	o->io_u = io_u;
+	o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+	if (!o->o.hEvent) {
+		free(o);
+		return 1;
+	}
+
+	io_u->engine_data = o;
+	return 0;
+}
+
 static struct ioengine_ops ioengine = {
 	.name		= "windowsaio",
 	.version	= FIO_IOOPS_VERSION,
@@ -521,7 +504,9 @@
 	.cleanup	= fio_windowsaio_cleanup,
 	.open_file	= fio_windowsaio_open_file,
 	.close_file	= fio_windowsaio_close_file,
-	.get_file_size	= generic_get_file_size
+	.get_file_size	= generic_get_file_size,
+	.io_u_init	= fio_windowsaio_io_u_init,
+	.io_u_free	= fio_windowsaio_io_u_free,
 };
 
 static void fio_init fio_posixaio_register(void)
diff --git a/ioengine.h b/ioengine.h
index 997f90a..0285b08 100644
--- a/ioengine.h
+++ b/ioengine.h
@@ -121,6 +121,8 @@
 	int (*close_file)(struct thread_data *, struct fio_file *);
 	int (*get_file_size)(struct thread_data *, struct fio_file *);
 	void (*terminate)(struct thread_data *);
+	int (*io_u_init)(struct thread_data *, struct io_u *);
+	void (*io_u_free)(struct thread_data *, struct io_u *);
 	int option_struct_size;
 	struct fio_option *options;
 	void *data;