Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 05 Feb 2003 05:14:40 +0100
From:      Dag-Erling Smorgrav <des@ofug.org>
To:        arch@freebsd.org
Subject:   Re: New kernel allocation API
Message-ID:  <xzp65rzs6ry.fsf@flood.ping.uio.no>
In-Reply-To: <xzp8ywvs6yu.fsf@flood.ping.uio.no> (Dag-Erling Smorgrav's message of "Wed, 05 Feb 2003 05:10:33 %2B0100")
References:  <xzp8ywvs6yu.fsf@flood.ping.uio.no>

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

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




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