From owner-freebsd-arch Tue Feb 4 20:14:48 2003 Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1649C37B401 for ; Tue, 4 Feb 2003 20:14:45 -0800 (PST) Received: from flood.ping.uio.no (flood.ping.uio.no [129.240.78.31]) by mx1.FreeBSD.org (Postfix) with ESMTP id A3D7143F3F for ; Tue, 4 Feb 2003 20:14:43 -0800 (PST) (envelope-from des@ofug.org) Received: by flood.ping.uio.no (Postfix, from userid 2602) id B22D7536E; Wed, 5 Feb 2003 05:14:41 +0100 (CET) X-URL: http://www.ofug.org/~des/ X-Disclaimer: The views expressed in this message do not necessarily coincide with those of any organisation or company with which I am or have been affiliated. To: arch@freebsd.org Subject: Re: New kernel allocation API From: Dag-Erling Smorgrav Date: Wed, 05 Feb 2003 05:14:40 +0100 In-Reply-To: (Dag-Erling Smorgrav's message of "Wed, 05 Feb 2003 05:10:33 +0100") Message-ID: User-Agent: Gnus/5.090014 (Oort Gnus v0.14) Emacs/21.2 (i386--freebsd) References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" 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 --=-=-= A slight amendment is necessary to eliminate warnings. Here's the new patch. DES -- Dag-Erling Smorgrav - des@ofug.org --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=kalloc.diff Index: sys/sys/malloc.h =================================================================== RCS file: /home/ncvs/src/sys/sys/malloc.h,v retrieving revision 1.69 diff -u -r1.69 malloc.h --- sys/sys/malloc.h 21 Jan 2003 08:56:14 -0000 1.69 +++ sys/sys/malloc.h 5 Feb 2003 04:05:14 -0000 @@ -107,7 +107,8 @@ unsigned long low, unsigned long high, unsigned long alignment, unsigned long boundary); void free(void *addr, struct malloc_type *type); -void *malloc(unsigned long size, struct malloc_type *type, int flags); +void *kalloc(size_t n, struct malloc_type *t, int z, struct mtx *m); +void *kalloc_nowait(size_t n, struct malloc_type *t, int z); void malloc_init(void *); int malloc_last_fail(void); void malloc_uninit(void *); @@ -115,6 +116,13 @@ int flags); void *reallocf(void *addr, unsigned long size, struct malloc_type *type, int flags); +/* + * Legacy API. + */ +#define malloc(sz, mt, fl) (((fl) & M_NOWAIT) ? \ + kalloc_nowait((sz), (mt), ((fl) & M_ZERO)) : \ + kalloc((sz), (mt), ((fl) & M_ZERO), NULL)) + #endif /* _KERNEL */ #endif /* !_SYS_MALLOC_H_ */ Index: sys/kern/kern_malloc.c =================================================================== RCS file: /home/ncvs/src/sys/kern/kern_malloc.c,v retrieving revision 1.115 diff -u -r1.115 kern_malloc.c --- sys/kern/kern_malloc.c 1 Feb 2003 10:07:49 -0000 1.115 +++ sys/kern/kern_malloc.c 5 Feb 2003 04:13:12 -0000 @@ -153,11 +153,12 @@ * If M_NOWAIT is set, this routine will not block and return NULL if * the allocation fails. */ +#ifdef malloc +#undef malloc +void *malloc(unsigned long size, struct malloc_type *type, int flags); +#endif void * -malloc(size, type, flags) - unsigned long size; - struct malloc_type *type; - int flags; +malloc(unsigned long size, struct malloc_type *type, int flags) { int indx; caddr_t va; @@ -216,6 +217,43 @@ } #endif return ((void *) va); +} + +/* + * Allocates size bytes, blocking if the request can't be satisfied right + * away. If mtx is non-NULL it may be released during kalloc(), and will + * be re-acquired before returning. If zero is non-zero, the allocated + * memory is cleared before kalloc() returns. + */ +void * +kalloc(size_t size, struct malloc_type *type, int zero, struct mtx *mtx) +{ + int flags; + void *va; + + KASSERT(curthread->td_intr_nesting_level == 0, + ("kalloc() called in interrupt context")); + flags = zero ? 0 : M_ZERO; + mtx_unlock(mtx); + va = malloc(size, type, flags); + mtx_lock(mtx); + return (va); +} + +/* + * Attempts to allocate size bytes, and returns NULL if the request can't + * be satisfied right away. If zero is non-zero, the allocated memory is + * cleared before kalloc_nowait() returns. + */ +void * +kalloc_nowait(size_t size, struct malloc_type *type, int zero) +{ + int flags; + void *va; + + flags = M_NOWAIT | (zero ? 0 : M_ZERO); + va = malloc(size, type, flags); + return (va); } /* --=-=-=-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message