kern_malloc.c revision 1.139 1 /* $NetBSD: kern_malloc.c,v 1.139 2012/04/28 23:03:40 rmind Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
32 */
33
34 /*
35 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
63 * SUCH DAMAGE.
64 *
65 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
66 */
67
68 /*
69 * Wrapper interface for obsolete malloc(9).
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.139 2012/04/28 23:03:40 rmind Exp $");
74
75 #include <sys/param.h>
76 #include <sys/malloc.h>
77 #include <sys/kmem.h>
78
79 #include <uvm/uvm_extern.h>
80
81 /*
82 * Built-in malloc types. Note: ought to be removed.
83 */
84 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
85 MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
86 MALLOC_DEFINE(M_FREE, "free", "should be on free list");
87 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
88 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
89 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
90 MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
91 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
92 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
93 MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
94 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
95 MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
96 MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
97
98 /*
99 * Header contains total size, including the header itself.
100 */
101 struct malloc_header {
102 size_t mh_size;
103 } __aligned(ALIGNBYTES + 1);
104
105 void *
106 kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
107 {
108 const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
109 size_t allocsize, hdroffset;
110 struct malloc_header *mh;
111 void *p;
112
113 if (size >= PAGE_SIZE) {
114 allocsize = PAGE_SIZE + size; /* for page alignment */
115 hdroffset = PAGE_SIZE - sizeof(struct malloc_header);
116 } else {
117 allocsize = sizeof(struct malloc_header) + size;
118 hdroffset = 0;
119 }
120
121 p = kmem_intr_alloc(allocsize, kmflags);
122 if (p == NULL)
123 return NULL;
124
125 if ((flags & M_ZERO) != 0) {
126 memset(p, 0, allocsize);
127 }
128 mh = (void *)((char *)p + hdroffset);
129 mh->mh_size = allocsize - hdroffset;
130
131 return mh + 1;
132 }
133
134 void
135 kern_free(void *addr, struct malloc_type *ksp)
136 {
137 struct malloc_header *mh;
138
139 mh = addr;
140 mh--;
141
142 if (mh->mh_size >= PAGE_SIZE + sizeof(struct malloc_header))
143 kmem_intr_free((char *)addr - PAGE_SIZE,
144 mh->mh_size + PAGE_SIZE - sizeof(struct malloc_header));
145 else
146 kmem_intr_free(mh, mh->mh_size);
147 }
148
149 void *
150 kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
151 int flags)
152 {
153 struct malloc_header *mh;
154 unsigned long cursize;
155 void *newaddr;
156
157 /*
158 * realloc() with a NULL pointer is the same as malloc().
159 */
160 if (curaddr == NULL)
161 return malloc(newsize, ksp, flags);
162
163 /*
164 * realloc() with zero size is the same as free().
165 */
166 if (newsize == 0) {
167 free(curaddr, ksp);
168 return NULL;
169 }
170
171 if ((flags & M_NOWAIT) == 0) {
172 ASSERT_SLEEPABLE();
173 }
174
175 mh = curaddr;
176 mh--;
177
178 cursize = mh->mh_size - sizeof(struct malloc_header);
179
180 /*
181 * If we already actually have as much as they want, we're done.
182 */
183 if (newsize <= cursize)
184 return curaddr;
185
186 /*
187 * Can't satisfy the allocation with the existing block.
188 * Allocate a new one and copy the data.
189 */
190 newaddr = malloc(newsize, ksp, flags);
191 if (__predict_false(newaddr == NULL)) {
192 /*
193 * malloc() failed, because flags included M_NOWAIT.
194 * Return NULL to indicate that failure. The old
195 * pointer is still valid.
196 */
197 return NULL;
198 }
199 memcpy(newaddr, curaddr, cursize);
200
201 /*
202 * We were successful: free the old allocation and return
203 * the new one.
204 */
205 free(curaddr, ksp);
206 return newaddr;
207 }
208
209 void
210 malloc_type_attach(struct malloc_type *type)
211 {
212 KASSERT(type->ks_magic == M_MAGIC);
213 }
214
215 void
216 malloc_type_detach(struct malloc_type *type)
217 {
218 KASSERT(type->ks_magic == M_MAGIC);
219 }
220
221 /*
222 * Initialize the kernel memory allocator
223 */
224 void
225 kmeminit(void)
226 {
227 __link_set_decl(malloc_types, struct malloc_type);
228 struct malloc_type * const *ksp;
229
230 /* Attach all of the statically-linked malloc types. */
231 __link_set_foreach(ksp, malloc_types)
232 malloc_type_attach(*ksp);
233 }
234