flist: add flist_first_entry()

Signed-off-by: Jens Axboe <axboe@fb.com>
diff --git a/diskutil.c b/diskutil.c
index cb285cf..c5c5ea6 100644
--- a/diskutil.c
+++ b/diskutil.c
@@ -30,7 +30,7 @@
 	while (!flist_empty(&du->slaves)) {
 		struct disk_util *slave;
 
-		slave = flist_entry(du->slaves.next, struct disk_util, slavelist);
+		slave = flist_first_entry(&du->slaves, struct disk_util, slavelist);
 		flist_del(&slave->slavelist);
 		slave->users--;
 	}
@@ -562,7 +562,7 @@
 	while (!flist_empty(&disk_list)) {
 		struct disk_util *du;
 
-		du = flist_entry(disk_list.next, struct disk_util, list);
+		du = flist_first_entry(&disk_list, struct disk_util, list);
 		flist_del(&du->list);
 		disk_util_free(du);
 	}
diff --git a/flist.h b/flist.h
index 938ce1d..d453e79 100644
--- a/flist.h
+++ b/flist.h
@@ -147,6 +147,15 @@
 		__flist_splice(list, head->prev, head);
 }
 
+static inline void flist_splice_tail_init(struct flist_head *list,
+					  struct flist_head *head)
+{
+	if (!flist_empty(list)) {
+		__flist_splice(list, head->prev, head);
+		INIT_FLIST_HEAD(list);
+	}
+}
+
 static inline void flist_splice_init(struct flist_head *list,
 				    struct flist_head *head)
 {
@@ -165,6 +174,9 @@
 #define flist_entry(ptr, type, member) \
 	container_of(ptr, type, member)
 
+#define flist_first_entry(ptr, type, member) \
+	flist_entry((ptr)->next, type, member)
+
 /**
  * flist_for_each	-	iterate over a list
  * @pos:	the &struct flist_head to use as a loop counter.
diff --git a/gclient.c b/gclient.c
index d236f86..42bc761 100644
--- a/gclient.c
+++ b/gclient.c
@@ -694,7 +694,7 @@
 
 static void gfio_client_iolog(struct fio_client *client, struct cmd_iolog_pdu *pdu)
 {
-	printf("got iolog: name=%s, type=%u, entries=%u\n", pdu->name, pdu->log_type, pdu->nr_samples);
+	printf("got iolog: name=%s, type=%u, entries=%lu\n", pdu->name, pdu->log_type, (unsigned long) pdu->nr_samples);
 	free(pdu);
 }
 
diff --git a/gfio.c b/gfio.c
index 65302e6..37c1db6 100644
--- a/gfio.c
+++ b/gfio.c
@@ -444,7 +444,7 @@
 	while (!flist_empty(&gc->o_list)) {
 		struct gfio_client_options *gco;
 
-		gco = flist_entry(gc->o_list.next, struct gfio_client_options, list);
+		gco = flist_first_entry(&gc->o_list, struct gfio_client_options, list);
 		flist_del(&gco->list);
 		free(gco);
 	}
diff --git a/goptions.c b/goptions.c
index 5b5c89e..c01b6cc 100644
--- a/goptions.c
+++ b/goptions.c
@@ -1433,7 +1433,7 @@
 		goto done;
 
 	while (!flist_empty(&gjv->changed_list)) {
-		gopt = flist_entry(gjv->changed_list.next, struct gopt, changed_list);
+		gopt = flist_first_entry(&gjv->changed_list, struct gopt, changed_list);
 		flist_del_init(&gopt->changed_list);
 	}
 
@@ -1577,7 +1577,7 @@
 
 	gjv = calloc(1, sizeof(*gjv));
 	INIT_FLIST_HEAD(&gjv->changed_list);
-	gco = flist_entry(gc->o_list.next, struct gfio_client_options, list);
+	gco = flist_first_entry(&gc->o_list, struct gfio_client_options, list);
 	gjv->o = &gco->o;
 	gjv->dialog = dialog;
 	gjv->client = gc;
diff --git a/graph.c b/graph.c
index 5c865dc..c45954c 100644
--- a/graph.c
+++ b/graph.c
@@ -687,7 +687,7 @@
 	 */
 	while (!(v->flags & GV_F_ON_PRIO)) {
 		assert(!flist_empty(&v->alias));
-		v = flist_entry(v->alias.next, struct graph_value, alias);
+		v = flist_first_entry(&v->alias, struct graph_value, alias);
 	}
 
 	prio_tree_remove(&l->prio_tree, &v->node);
@@ -698,7 +698,7 @@
 	while (!flist_empty(&v->alias)) {
 		struct graph_value *a;
 
-		a = flist_entry(v->alias.next, struct graph_value, alias);
+		a = flist_first_entry(&v->alias, struct graph_value, alias);
 		flist_del_init(&a->alias);
 
 		__graph_value_drop(l, a);
@@ -773,7 +773,7 @@
 			to_drop = 2;
 
 		while (to_drop-- && !flist_empty(&i->value_list)) {
-			x = flist_entry(i->value_list.next, struct graph_value, list);
+			x = flist_first_entry(&i->value_list, struct graph_value, list);
 			graph_value_drop(i, x);
 
 			/*
@@ -836,7 +836,7 @@
 	struct graph_value *i;
 
 	while (!flist_empty(&l->value_list)) {
-		i = flist_entry(l->value_list.next, struct graph_value, list);
+		i = flist_first_entry(&l->value_list, struct graph_value, list);
 		graph_value_drop(l, i);
 	}
 }
@@ -846,7 +846,7 @@
 	struct graph_label *i;
 
 	while (!flist_empty(&g->label_list)) {
-		i = flist_entry(g->label_list.next, struct graph_label, list);
+		i = flist_first_entry(&g->label_list, struct graph_label, list);
 		flist_del(&i->list);
 		graph_free_values(i);
 		free(i);
@@ -1010,7 +1010,7 @@
 					}
 				}
 				if (!flist_empty(&v->alias))
-					v = flist_entry(v->alias.next, struct graph_value, alias);
+					v = flist_first_entry(&v->alias, struct graph_value, alias);
 			} while (v != rootv);
 		} while ((n = prio_tree_next(&iter)) != NULL);
 
diff --git a/io_u.c b/io_u.c
index 5b9d483..16b52d6 100644
--- a/io_u.c
+++ b/io_u.c
@@ -223,7 +223,7 @@
 	if (!flist_empty(&td->next_rand_list)) {
 		struct rand_off *r;
 fetch:
-		r = flist_entry(td->next_rand_list.next, struct rand_off, list);
+		r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
 		flist_del(&r->list);
 		*b = r->off;
 		free(r);
diff --git a/iolog.c b/iolog.c
index 8bfd5bc..c725a4f 100644
--- a/iolog.c
+++ b/iolog.c
@@ -131,7 +131,7 @@
 	while (!flist_empty(&td->io_log_list)) {
 		int ret;
 
-		ipo = flist_entry(td->io_log_list.next, struct io_piece, list);
+		ipo = flist_first_entry(&td->io_log_list, struct io_piece, list);
 		flist_del(&ipo->list);
 		remove_trim_entry(td, ipo);
 
@@ -184,7 +184,7 @@
 	}
 
 	while (!flist_empty(&td->io_hist_list)) {
-		ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
+		ipo = flist_entry(&td->io_hist_list, struct io_piece, list);
 		flist_del(&ipo->list);
 		remove_trim_entry(td, ipo);
 		td->io_hist_len--;
@@ -773,14 +773,12 @@
 static void flush_gz_chunks(struct io_log *log, FILE *f)
 {
 	struct flush_chunk_iter iter = { .chunk_sz = log->log_gz, };
-	struct flist_head *node;
 	z_stream stream;
 
 	while (!flist_empty(&log->chunk_list)) {
 		struct iolog_compress *ic;
 
-		node = log->chunk_list.next;
-		ic = flist_entry(node, struct iolog_compress, list);
+		ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list);
 		flist_del(&ic->list);
 
 		if (log->log_gz_store)
diff --git a/lib/tp.c b/lib/tp.c
index 25f7eb6..5111910 100644
--- a/lib/tp.c
+++ b/lib/tp.c
@@ -33,10 +33,8 @@
 		if (!tdat->thread_exit && flist_empty(&tdat->work))
 			pthread_cond_wait(&tdat->cv, &tdat->lock);
 
-		if (!flist_empty(&tdat->work)) {
-			flist_splice(&tdat->work, &work_list);
-			INIT_FLIST_HEAD(&tdat->work);
-		}
+		if (!flist_empty(&tdat->work))
+			flist_splice_tail_init(&tdat->work, &work_list);
 
 		pthread_mutex_unlock(&tdat->lock);
 
diff --git a/trim.c b/trim.c
index de792dc..a7f1b86 100644
--- a/trim.c
+++ b/trim.c
@@ -24,7 +24,7 @@
 		return 1;
 
 	assert(td->trim_entries);
-	ipo = flist_entry(td->trim_list.next, struct io_piece, trim_list);
+	ipo = flist_first_entry(&td->trim_list, struct io_piece, trim_list);
 	remove_trim_entry(td, ipo);
 
 	io_u->offset = ipo->offset;
diff --git a/verify.c b/verify.c
index 2615701..11963e1 100644
--- a/verify.c
+++ b/verify.c
@@ -1082,7 +1082,7 @@
 		assert(ipo->flags & IP_F_ONRB);
 		ipo->flags &= ~IP_F_ONRB;
 	} else if (!flist_empty(&td->io_hist_list)) {
-		ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
+		ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
 
 		/*
 		 * Ensure that the associated IO has completed
@@ -1187,7 +1187,7 @@
 			continue;
 
 		while (!flist_empty(&list)) {
-			io_u = flist_entry(list.next, struct io_u, verify_list);
+			io_u = flist_first_entry(&list, struct io_u, verify_list);
 			flist_del(&io_u->verify_list);
 
 			ret = verify_io_u(td, io_u);