changeset 801:ddeb5fa3ea47

taskq: use a mem cache to allocate taskq structs Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 09 Apr 2020 12:15:24 -0400
parents 299dffade145
children aa6e8f99966f
files taskq.c
diffstat 1 files changed, 13 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/taskq.c	Fri Apr 03 15:11:57 2020 -0400
+++ b/taskq.c	Thu Apr 09 12:15:24 2020 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2017-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -25,9 +25,18 @@
 #include <jeffpc/error.h>
 #include <jeffpc/taskq.h>
 #include <jeffpc/cstr.h>
+#include <jeffpc/mem.h>
 
 static LOCK_CLASS(taskq_lc);
 
+static struct mem_cache *taskq_cache;
+
+static void __attribute__((constructor)) init_taskq_subsys(void)
+{
+	taskq_cache = mem_cache_create("taskq-cache", sizeof(struct taskq), 0);
+	ASSERT(!IS_ERR(taskq_cache));
+}
+
 static void enqueue(struct taskq *tq, struct taskq_item *item)
 {
 	list_insert_tail(&tq->queue, item);
@@ -132,7 +141,7 @@
 
 	VERIFY(nthreads);
 
-	tq = malloc(sizeof(struct taskq));
+	tq = mem_cache_alloc(taskq_cache);
 	if (!tq)
 		goto err;
 
@@ -162,7 +171,7 @@
 	return tq;
 
 err_free:
-	free(tq);
+	mem_cache_free(taskq_cache, tq);
 err:
 	return ERR_PTR(-ENOMEM);
 }
@@ -233,5 +242,5 @@
 	MXDESTROY(&tq->lock);
 	list_destroy(&tq->queue);
 	free(tq->threads);
-	free(tq);
+	mem_cache_free(taskq_cache, tq);
 }