smalloc: alloc failure cleanups

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/cgroup.c b/cgroup.c
index 86d4d5e..34b61de 100644
--- a/cgroup.c
+++ b/cgroup.c
@@ -52,9 +52,22 @@
 {
 	struct cgroup_member *cm;
 
+	if (!lock)
+		return;
+
 	cm = smalloc(sizeof(*cm));
+	if (!cm) {
+err:
+		log_err("fio: failed to allocate cgroup member\n");
+		return;
+	}
+
 	INIT_FLIST_HEAD(&cm->list);
 	cm->root = smalloc_strdup(name);
+	if (!cm->root) {
+		sfree(cm);
+		goto err;
+	}
 	if (td->o.cgroup_nodelete)
 		cm->cgroup_nodelete = 1;
 	fio_mutex_down(lock);
@@ -67,6 +80,9 @@
 	struct flist_head *n, *tmp;
 	struct cgroup_member *cm;
 
+	if (!lock)
+		return;
+
 	fio_mutex_down(lock);
 
 	flist_for_each_safe(n, tmp, clist) {
@@ -183,6 +199,8 @@
 static void fio_init cgroup_init(void)
 {
 	lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
+	if (!lock)
+		log_err("fio: failed to allocate cgroup lock\n");
 }
 
 static void fio_exit cgroup_exit(void)
diff --git a/diskutil.c b/diskutil.c
index 3681dde..e29d1c3 100644
--- a/diskutil.c
+++ b/diskutil.c
@@ -281,6 +281,11 @@
 	dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
 
 	du = smalloc(sizeof(*du));
+	if (!du) {
+		log_err("fio: smalloc() pool exhausted\n");
+		return NULL;
+	}
+
 	memset(du, 0, sizeof(*du));
 	INIT_FLIST_HEAD(&du->list);
 	l = snprintf(du->path, sizeof(du->path), "%s/stat", path);
diff --git a/fio.c b/fio.c
index a408727..af4c12c 100644
--- a/fio.c
+++ b/fio.c
@@ -26,15 +26,7 @@
 #include <time.h>
 
 #include "fio.h"
-#include "hash.h"
 #include "smalloc.h"
-#include "verify.h"
-#include "trim.h"
-#include "diskutil.h"
-#include "profile.h"
-#include "lib/rand.h"
-#include "memalign.h"
-#include "server.h"
 
 uintptr_t page_mask;
 uintptr_t page_size;
diff --git a/flow.c b/flow.c
index 2993f4e..b7a2fb1 100644
--- a/flow.c
+++ b/flow.c
@@ -39,6 +39,9 @@
 	struct fio_flow *flow = NULL;
 	struct flist_head *n;
 
+	if (!flow_lock)
+		return NULL;
+
 	fio_mutex_down(flow_lock);
 
 	flist_for_each(n, flow_list) {
@@ -51,6 +54,10 @@
 
 	if (!flow) {
 		flow = smalloc(sizeof(*flow));
+		if (!flow) {
+			log_err("fio: smalloc pool exhausted\n");
+			return NULL;
+		}
 		flow->refs = 0;
 		INIT_FLIST_HEAD(&flow->list);
 		flow->id = id;
@@ -66,6 +73,9 @@
 
 static void flow_put(struct fio_flow *flow)
 {
+	if (!flow_lock)
+		return;
+
 	fio_mutex_down(flow_lock);
 
 	if (!--flow->refs) {
@@ -92,13 +102,26 @@
 
 void flow_init(void)
 {
-	flow_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
 	flow_list = smalloc(sizeof(*flow_list));
+	if (!flow_list) {
+		log_err("fio: smalloc pool exhausted\n");
+		return;
+	}
+
+	flow_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
+	if (!flow_lock) {
+		log_err("fio: failed to allocate flow lock\n");
+		sfree(flow_list);
+		return;
+	}
+
 	INIT_FLIST_HEAD(flow_list);
 }
 
 void flow_exit(void)
 {
-	fio_mutex_remove(flow_lock);
-	sfree(flow_list);
+	if (flow_lock)
+		fio_mutex_remove(flow_lock);
+	if (flow_list)
+		sfree(flow_list);
 }
diff --git a/gettime-thread.c b/gettime-thread.c
index da40904..c1b4b09 100644
--- a/gettime-thread.c
+++ b/gettime-thread.c
@@ -14,12 +14,14 @@
 void fio_gtod_init(void)
 {
 	fio_tv = smalloc(sizeof(struct timeval));
-	assert(fio_tv);
+	if (!fio_tv)
+		log_err("fio: smalloc pool exhausted\n");
 }
 
 static void fio_gtod_update(void)
 {
-	gettimeofday(fio_tv, NULL);
+	if (fio_tv)
+		gettimeofday(fio_tv, NULL);
 }
 
 static void *gtod_thread_main(void *data)
diff --git a/verify.c b/verify.c
index cb13b62..daa2f04 100644
--- a/verify.c
+++ b/verify.c
@@ -10,7 +10,6 @@
 
 #include "fio.h"
 #include "verify.h"
-#include "smalloc.h"
 #include "trim.h"
 #include "lib/rand.h"
 #include "lib/hweight.h"