Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Jan 2002 16:03:24 -0500
From:      Dan Moschuk <dan@freebsd.org>
To:        arch@freebsd.org
Subject:   review request: new malloc flag M_SHORTWAIT
Message-ID:  <20020112160324.A32256@spirit.jaded.net>

next in thread | raw e-mail | index | archive | help


The following patch adds a new malloc flag M_SHORTWAIT.  All this really
does is pass a small timeout (0.5 seconds) to the msleep() call in
the event that no memory is available.  It's needed for things like the
md driver that don't want to block until memory is available (but still would
like to wait a short time).

Catching a case where M_SHORTWAIT | M_WAITOK is passed may want to be
added for the sake of adding it.

Comments?

Index: kern/kern_malloc.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_malloc.c,v
retrieving revision 1.93
diff -u -r1.93 kern_malloc.c
--- kern/kern_malloc.c	12 Sep 2001 08:37:44 -0000	1.93
+++ kern/kern_malloc.c	10 Jan 2002 17:25:49 -0000
@@ -133,7 +133,7 @@
 	register struct kmemusage *kup;
 	register struct freelist *freep;
 	long indx, npg, allocsize;
-	int s;
+	int s, timo, r;
 	caddr_t va, cp, savedlist;
 #ifdef INVARIANTS
 	long *end, *lp;
@@ -147,6 +147,7 @@
 		KASSERT(curthread->td_intr_nesting_level == 0,
 		   ("malloc(M_WAITOK) in interrupt context"));
 #endif
+	timo = (flags & M_SHORTWAIT) ? (hz / 2) : 0;
 	indx = BUCKETINDX(size);
 	kbp = &bucket[indx];
 	s = splmem();
@@ -159,8 +160,13 @@
 		}
 		if (ksp->ks_limblocks < 65535)
 			ksp->ks_limblocks++;
-		msleep((caddr_t)ksp, &malloc_mtx, PSWP+2, type->ks_shortdesc,
-		    0);
+		r = msleep((caddr_t)ksp, &malloc_mtx, PSWP+2, 
+			type->ks_shortdesc, timo);
+		if (r == EWOULDBLOCK) {
+			splx(s);
+			mtx_unlock(&malloc_mtx);
+			return ((void *) NULL);
+		}
 	}
 	ksp->ks_size |= 1 << indx;
 #ifdef INVARIANTS
Index: sys/malloc.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/malloc.h,v
retrieving revision 1.54
diff -u -r1.54 malloc.h
--- sys/malloc.h	10 Aug 2001 06:37:04 -0000	1.54
+++ sys/malloc.h	10 Jan 2002 17:26:00 -0000
@@ -46,6 +46,7 @@
 #define	M_NOWAIT	0x0001		/* do not block */
 #define	M_USE_RESERVE	0x0002		/* can alloc out of reserve memory */
 #define	M_ZERO		0x0004		/* bzero the allocation */
+#define M_SHORTWAIT	0x0008		/* wait for 1s */
 
 #define	M_MAGIC		877983977	/* time when first defined :-) */
 
-- 
Build a man a fire and he'll be warm for a day.
Set a man on fire and he'll be warm for the rest of his life.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020112160324.A32256>