Date: Wed, 05 Feb 2003 05:10:33 +0100 From: Dag-Erling Smorgrav <des@ofug.org> To: arch@freebsd.org Subject: New kernel allocation API Message-ID: <xzp8ywvs6yu.fsf@flood.ping.uio.no>
next in thread | raw e-mail | index | archive | help
--=-=-= The attached patch creates two new entry points for malloc(), named kalloc() and kalloc_nowait(), eliminating the need for the M_NOWAIT and M_TRYWAIT constants. The patch also defines malloc() as a macro which invokes the appropriate API function, so source-level compatibility is maintained. 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:09:05 -0000 @@ -153,6 +153,9 @@ * If M_NOWAIT is set, this routine will not block and return NULL if * the allocation fails. */ +#ifdef malloc +#undef malloc +#endif void * malloc(size, type, flags) unsigned long size; @@ -216,6 +219,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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzp8ywvs6yu.fsf>