--- //depot/vendor/freebsd/src/sys/kern/subr_sleepqueue.c	2006/12/17 00:17:01
+++ //depot/user/attilio/attilio_smpng/kern/subr_sleepqueue.c	2006/12/17 01:35:45
@@ -54,9 +54,7 @@
  * mix the sleep/wakeup and cv APIs for a given wait channel.  Also, one
  * must consistently use the same lock to synchronize with a wait channel,
  * though this check is currently only a warning for sleep/wakeup due to
- * pre-existing abuse of that API.  The same lock must also be held when
- * awakening threads, though that is currently only enforced for condition
- * variables.
+ * pre-existing abuse of that API.
  */
 
 #include <sys/cdefs.h>
@@ -70,7 +68,6 @@
 #include <sys/lock.h>
 #include <sys/kernel.h>
 #include <sys/ktr.h>
-#include <sys/malloc.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/sched.h>
@@ -78,6 +75,8 @@
 #include <sys/sleepqueue.h>
 #include <sys/sysctl.h>
 
+#include <vm/uma.h>
+
 #ifdef DDB
 #include <ddb/ddb.h>
 #endif
@@ -142,18 +141,20 @@
     0, "maxmimum depth achieved of a single chain");
 #endif
 static struct sleepqueue_chain sleepq_chains[SC_TABLESIZE];
+static uma_zone_t sleepq_zone;
 
-static MALLOC_DEFINE(M_SLEEPQUEUE, "sleepqueue", "sleep queues");
-
 /*
  * Prototypes for non-exported routines.
  */
 static int	sleepq_catch_signals(void *wchan);
 static int	sleepq_check_signals(void);
 static int	sleepq_check_timeout(void);
+static void	sleepq_fini(void *mem, int size);
+static int	sleepq_init(void *mem, int size, int flags);
+static void	sleepq_resume_thread(struct sleepqueue *sq, struct thread *td,
+		    int pri);
 static void	sleepq_switch(void *wchan);
 static void	sleepq_timeout(void *arg);
-static void	sleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri);
 
 /*
  * Early initialization of sleep queues that is called from the sleepinit()
@@ -184,23 +185,19 @@
 		    NULL);
 #endif
 	}
+	sleepq_zone = uma_zcreate("SLEEPQUEUE", sizeof(struct sleepqueue),
+	    NULL, NULL, sleepq_init, sleepq_fini, UMA_ALIGN_SYNC, 0);
 	thread0.td_sleepqueue = sleepq_alloc();
 }
 
 /*
- * Malloc and initialize a new sleep queue for a new thread.
+ * Get a sleep queue for a new thread.
  */
 struct sleepqueue *
 sleepq_alloc(void)
 {
-	struct sleepqueue *sq;
-	int i;
 
-	sq = malloc(sizeof(struct sleepqueue), M_SLEEPQUEUE, M_WAITOK | M_ZERO);
-	for (i = 0; i < NR_SLEEPQS; i++)
-		TAILQ_INIT(&sq->sq_blocked[i]);
-	LIST_INIT(&sq->sq_free);
-	return (sq);
+	return (uma_zalloc(sleepq_zone, M_WAITOK));
 }
 
 /*
@@ -209,12 +206,8 @@
 void
 sleepq_free(struct sleepqueue *sq)
 {
-	int i;
 
-	MPASS(sq != NULL);
-	for (i = 0; i < NR_SLEEPQS; i++)
-		MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
-	free(sq, M_SLEEPQUEUE);
+	uma_zfree(sleepq_zone, sq);
 }
 
 /*
@@ -667,6 +660,37 @@
 }
 
 /*
+ * UMA zone item deallocator.
+ */
+static void
+sleepq_fini(void *mem, int size)
+{
+	struct sleepqueue *sq;
+	int i;
+
+	sq = mem;
+	for (i = 0; i < NR_SLEEPQS; i++)
+		MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
+}
+
+/*
+ * UMA zone item initializer.
+ */
+static int
+sleepq_init(void *mem, int size, int flags)
+{
+	struct sleepqueue *sq;
+	int i;
+
+	bzero(mem, size);
+	sq = mem;
+	for (i = 0; i < NR_SLEEPQS; i++)
+		TAILQ_INIT(&sq->sq_blocked[i]);
+	LIST_INIT(&sq->sq_free);
+	return (0);
+}
+
+/*
  * Find the highest priority thread sleeping on a wait channel and resume it.
  */
 void
--- //depot/vendor/freebsd/src/sys/kern/subr_turnstile.c	2007/01/23 08:52:34
+++ //depot/user/attilio/attilio_smpng/kern/subr_turnstile.c	2007/02/06 01:28:29
@@ -46,14 +46,14 @@
  * chain.  Each chain contains a spin mutex that protects all of the
  * turnstiles in the chain.
  *
- * Each time a thread is created, a turnstile is malloc'd and attached to
- * that thread.  When a thread blocks on a lock, if it is the first thread
- * to block, it lends its turnstile to the lock.  If the lock already has
- * a turnstile, then it gives its turnstile to the lock's turnstile's free
- * list.  When a thread is woken up, it takes a turnstile from the free list
- * if there are any other waiters.  If it is the only thread blocked on the
- * lock, then it reclaims the turnstile associated with the lock and removes
- * it from the hash table.
+ * Each time a thread is created, a turnstile is got from a specific UMA zone
+ * and attached to that thread.  When a thread blocks on a lock, if it is the
+ * first thread to block, it lends its turnstile to the lock.  If the lock
+ * already has a turnstile, then it gives its turnstile to the lock's
+ * turnstile's free list.  When a thread is woken up, it takes a turnstile from
+ * the free list if there are any other waiters.  If it is the only thread
+ * blocked on the lock, then it reclaims the turnstile associated with the lock
+ * and removes it from the hash table.
  */
 
 #include <sys/cdefs.h>
@@ -67,7 +67,6 @@
 #include <sys/kernel.h>
 #include <sys/ktr.h>
 #include <sys/lock.h>
-#include <sys/malloc.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/queue.h>
@@ -75,6 +74,8 @@
 #include <sys/sysctl.h>
 #include <sys/turnstile.h>
 
+#include <vm/uma.h>
+
 #ifdef DDB
 #include <sys/kdb.h>
 #include <ddb/ddb.h>
@@ -143,8 +144,7 @@
 #endif
 static struct mtx td_contested_lock;
 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
-
-static MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
+static uma_zone_t turnstile_zone;
 
 /*
  * Prototypes for non-exported routines.
@@ -158,6 +158,8 @@
 		    struct thread *td);
 static struct thread *turnstile_first_waiter(struct turnstile *ts);
 static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
+static void	turnstile_fini(void *mem, int size);
+static int	turnstile_init(void *mem, int size, int flags);
 
 /*
  * Walks the chain of turnstiles and their owners to propagate the priority
@@ -379,6 +381,8 @@
 init_turnstile0(void *dummy)
 {
 
+	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
+	    NULL, NULL, turnstile_init, turnstile_fini, UMA_ALIGN_SYNC, 0);
 	thread0.td_turnstile = turnstile_alloc();
 }
 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
@@ -448,19 +452,44 @@
 }
 
 /*
- * Malloc a turnstile for a new thread, initialize it and return it.
+ * UMA zone item deallocator.
+ */
+static void
+turnstile_fini(void *mem, int size)
+{
+	struct turnstile *ts;
+
+	ts = mem;
+	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
+	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
+	MPASS(TAILQ_EMPTY(&ts->ts_pending));
+}
+
+/*
+ * UMA zone item initializer.
  */
-struct turnstile *
-turnstile_alloc(void)
+static int
+turnstile_init(void *mem, int size, int flags)
 {
 	struct turnstile *ts;
 
-	ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
+	bzero(mem, size);
+	ts = mem;
 	TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
 	TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
 	TAILQ_INIT(&ts->ts_pending);
 	LIST_INIT(&ts->ts_free);
-	return (ts);
+	return (0);
+}
+
+/*
+ * Get a turnstile for a new thread.
+ */
+struct turnstile *
+turnstile_alloc(void)
+{
+
+	return (uma_zalloc(turnstile_zone, M_WAITOK));
 }
 
 /*
@@ -470,11 +499,7 @@
 turnstile_free(struct turnstile *ts)
 {
 
-	MPASS(ts != NULL);
-	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
-	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
-	MPASS(TAILQ_EMPTY(&ts->ts_pending));
-	free(ts, M_TURNSTILE);
+	uma_zfree(turnstile_zone, ts);
 }
 
 /*
--- //depot/vendor/freebsd/src/sys/vm/uma.h	2007/01/25 01:07:07
+++ //depot/user/attilio/attilio_smpng/vm/uma.h	2007/02/06 01:28:29
@@ -237,6 +237,9 @@
 #define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
 #define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
 #define UMA_ALIGN_CACHE	(16 - 1)		/* Cache line size align */
+#ifndef UMA_ALIGN_SYNC
+#define	UMA_ALIGN_SYNC	UMA_ALIGN_CACHE		/* Syncronizing primitives */
+#endif
 
 /*
  * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
