From owner-freebsd-arch Sat Jan 12 13: 3: 4 2002 Delivered-To: freebsd-arch@freebsd.org Received: from enigma.trueimpact.net (enigma.trueimpact.net [209.82.45.201]) by hub.freebsd.org (Postfix) with ESMTP id 2A9E737B400 for ; Sat, 12 Jan 2002 13:02:59 -0800 (PST) Received: from spirit.jaded.net (unknown [24.141.6.76]) by enigma.trueimpact.net (Postfix) with ESMTP id 290E666B02 for ; Sat, 12 Jan 2002 16:02:53 -0500 (EST) Received: (from dan@localhost) by spirit.jaded.net (8.11.6/8.11.6) id g0CL3Sm32632 for arch@freebsd.org; Sat, 12 Jan 2002 16:03:28 -0500 (EST) (envelope-from dan) Date: Sat, 12 Jan 2002 16:03:24 -0500 From: Dan Moschuk To: arch@freebsd.org Subject: review request: new malloc flag M_SHORTWAIT Message-ID: <20020112160324.A32256@spirit.jaded.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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