summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_malloc.c
diff options
context:
space:
mode:
authormiod <miod@openbsd.org>2009-08-25 17:59:43 +0000
committermiod <miod@openbsd.org>2009-08-25 17:59:43 +0000
commit1eb28d48fb22e9416762770578f65502da1ad857 (patch)
treec7888401c25c92f4a9d262845c70c9c97ebe7ddb /sys/kern/kern_malloc.c
parentsync (diff)
downloadwireguard-openbsd-1eb28d48fb22e9416762770578f65502da1ad857.tar.xz
wireguard-openbsd-1eb28d48fb22e9416762770578f65502da1ad857.zip
The BUCKETINDX() giant macro is used to compute the base 2 logarithm of its
input, in order to pick the appropriate malloc() bucket. Replace it with an inline function in kern_malloc.c, which will either do a tightest-but-slower loop (if option SMALL_KERNEL), or a geometric search equivalent to what the macro does, but producing smaller code (especially on platforms which can not load large constants in one instruction).
Diffstat (limited to 'sys/kern/kern_malloc.c')
-rw-r--r--sys/kern/kern_malloc.c34
1 files changed, 33 insertions, 1 deletions
diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index 9e3269356ee..9c94a64657e 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_malloc.c,v 1.79 2009/02/22 19:57:59 miod Exp $ */
+/* $OpenBSD: kern_malloc.c,v 1.80 2009/08/25 17:59:43 miod Exp $ */
/* $NetBSD: kern_malloc.c,v 1.15.4.2 1996/06/13 17:10:56 cgd Exp $ */
/*
@@ -43,6 +43,38 @@
#include <uvm/uvm_extern.h>
+static __inline__ long BUCKETINDX(size_t sz)
+{
+#ifdef SMALL_KERNEL
+ long b;
+
+ if (sz-- == 0)
+ return MINBUCKET;
+
+ for (b = MINBUCKET; b < MINBUCKET + 15; b++)
+ if ((sz >> b) == 0)
+ break;
+#else
+ long b, d;
+
+ /* note that this relies upon MINALLOCSIZE being 1 << MINBUCKET */
+ b = 7 + MINBUCKET; d = 4;
+ while (d != 0) {
+ if (sz <= (1 << b))
+ b -= d;
+ else
+ b += d;
+ d >>= 1;
+ }
+ if (sz <= (1 << b))
+ b += 0;
+ else
+ b += 1;
+#endif
+
+ return b;
+}
+
static struct vm_map kmem_map_store;
struct vm_map *kmem_map = NULL;