Mon Jan 30 01:56:48 2012 UTC ()
- kern_realloc: fix a recent regression, use correct size of current allocation.
- kern_malloc: constify.


(rmind)
diff -r1.135 -r1.136 src/sys/kern/kern_malloc.c

cvs diff -r1.135 -r1.136 src/sys/kern/kern_malloc.c (expand / switch to unified diff)

--- src/sys/kern/kern_malloc.c 2012/01/28 23:09:06 1.135
+++ src/sys/kern/kern_malloc.c 2012/01/30 01:56:47 1.136
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_malloc.c,v 1.135 2012/01/28 23:09:06 rmind Exp $ */ 1/* $NetBSD: kern_malloc.c,v 1.136 2012/01/30 01:56:47 rmind Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1987, 1991, 1993 4 * Copyright (c) 1987, 1991, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -56,70 +56,70 @@ @@ -56,70 +56,70 @@
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95 65 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
66 */ 66 */
67 67
68#include <sys/cdefs.h> 68#include <sys/cdefs.h>
69__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.135 2012/01/28 23:09:06 rmind Exp $"); 69__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.136 2012/01/30 01:56:47 rmind Exp $");
70 70
71#include <sys/param.h> 71#include <sys/param.h>
72#include <sys/proc.h> 72#include <sys/proc.h>
73#include <sys/kernel.h> 73#include <sys/kernel.h>
74#include <sys/malloc.h> 74#include <sys/malloc.h>
75#include <sys/kmem.h> 75#include <sys/kmem.h>
76#include <sys/systm.h> 76#include <sys/systm.h>
77#include <sys/debug.h> 77#include <sys/debug.h>
78#include <sys/mutex.h> 78#include <sys/mutex.h>
79#include <sys/lockdebug.h> 79#include <sys/lockdebug.h>
80 80
81#include <uvm/uvm_extern.h> 81#include <uvm/uvm_extern.h>
82 82
83#include "opt_kmemstats.h" 83#include "opt_kmemstats.h"
84#include "opt_malloclog.h" 84#include "opt_malloclog.h"
85#include "opt_malloc_debug.h" 85#include "opt_malloc_debug.h"
86 86
87struct kmembuckets kmembuckets[MINBUCKET + 16]; 87struct kmembuckets kmembuckets[MINBUCKET + 16];
88struct kmemusage *kmemusage; 88struct kmemusage *kmemusage;
89struct malloc_type *kmemstatistics; 89struct malloc_type *kmemstatistics;
90 90
91kmutex_t malloc_lock; 91kmutex_t malloc_lock;
92 92
93struct malloc_header { 93struct malloc_header {
94 size_t mh_size; 94 /* Total size, include the header. */
 95 size_t mh_size;
95}; 96};
96 97
97/* 98/*
98 * Allocate a block of memory 99 * Allocate a block of memory
99 */ 100 */
100#ifdef MALLOCLOG 101#ifdef MALLOCLOG
101void * 102void *
102_kern_malloc(unsigned long size, struct malloc_type *ksp, int flags, 103_kern_malloc(unsigned long size, struct malloc_type *ksp, int flags,
103 const char *file, long line) 104 const char *file, long line)
104#else 105#else
105void * 106void *
106kern_malloc(unsigned long size, struct malloc_type *ksp, int flags) 107kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
107#endif /* MALLOCLOG */ 108#endif /* MALLOCLOG */
108{ 109{
 110 const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
 111 const size_t allocsize = sizeof(struct malloc_header) + size;
109 struct malloc_header *mh; 112 struct malloc_header *mh;
110 int kmflags = ((flags & M_NOWAIT) != 0 
111 ? KM_NOSLEEP : KM_SLEEP); 
112 size_t allocsize = sizeof(struct malloc_header) + size; 
113 void *p; 113 void *p;
114 114
115 p = kmem_intr_alloc(allocsize, kmflags); 115 p = kmem_intr_alloc(allocsize, kmflags);
116 if (p == NULL) 116 if (p == NULL)
117 return NULL; 117 return NULL;
118 118
119 if ((flags & M_ZERO) != 0) { 119 if ((flags & M_ZERO) != 0) {
120 memset(p, 0, allocsize); 120 memset(p, 0, allocsize);
121 } 121 }
122 mh = (void *)p; 122 mh = (void *)p;
123 mh->mh_size = allocsize; 123 mh->mh_size = allocsize;
124 124
125 return mh + 1; 125 return mh + 1;
@@ -168,27 +168,27 @@ kern_realloc(void *curaddr, unsigned lon @@ -168,27 +168,27 @@ kern_realloc(void *curaddr, unsigned lon
168 free(curaddr, ksp); 168 free(curaddr, ksp);
169 return (NULL); 169 return (NULL);
170 } 170 }
171 171
172#ifdef LOCKDEBUG 172#ifdef LOCKDEBUG
173 if ((flags & M_NOWAIT) == 0) { 173 if ((flags & M_NOWAIT) == 0) {
174 ASSERT_SLEEPABLE(); 174 ASSERT_SLEEPABLE();
175 } 175 }
176#endif 176#endif
177 177
178 mh = curaddr; 178 mh = curaddr;
179 mh--; 179 mh--;
180 180
181 cursize = mh->mh_size; 181 cursize = mh->mh_size - sizeof(struct malloc_header);
182 182
183 /* 183 /*
184 * If we already actually have as much as they want, we're done. 184 * If we already actually have as much as they want, we're done.
185 */ 185 */
186 if (newsize <= cursize) 186 if (newsize <= cursize)
187 return (curaddr); 187 return (curaddr);
188 188
189 /* 189 /*
190 * Can't satisfy the allocation with the existing block. 190 * Can't satisfy the allocation with the existing block.
191 * Allocate a new one and copy the data. 191 * Allocate a new one and copy the data.
192 */ 192 */
193 newaddr = malloc(newsize, ksp, flags); 193 newaddr = malloc(newsize, ksp, flags);
194 if (__predict_false(newaddr == NULL)) { 194 if (__predict_false(newaddr == NULL)) {