FreeBSD/Linux Kernel Cross Reference
sys/sys/malloc.h
1 /* $NetBSD: malloc.h,v 1.90 2004/09/04 23:30:07 manu Exp $ */
2
3 /*
4 * Copyright (c) 1987, 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 * @(#)malloc.h 8.5 (Berkeley) 5/3/95
32 */
33
34 #ifndef _SYS_MALLOC_H_
35 #define _SYS_MALLOC_H_
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_kmemstats.h"
39 #include "opt_malloclog.h"
40 #include "opt_malloc_debug.h"
41 #include "opt_lockdebug.h"
42 #endif
43
44
45 /*
46 * flags to malloc
47 */
48 #define M_WAITOK 0x0000 /* can wait for resources */
49 #define M_NOWAIT 0x0001 /* do not wait for resources */
50 #define M_ZERO 0x0002 /* zero the allocation */
51 #define M_CANFAIL 0x0004 /* can fail if requested memory can't ever
52 * be allocated */
53 #ifdef _KERNEL
54
55 #include <sys/lock.h>
56 #include <sys/mallocvar.h>
57 /*
58 * The following are standard, built-in malloc types that are
59 * not specific to any one subsystem.
60 */
61 MALLOC_DECLARE(M_DEVBUF);
62 MALLOC_DECLARE(M_DMAMAP);
63 MALLOC_DECLARE(M_FREE);
64 MALLOC_DECLARE(M_PCB);
65 MALLOC_DECLARE(M_SOFTINTR);
66 MALLOC_DECLARE(M_TEMP);
67
68 /* XXX These should all be declared elsewhere. */
69 MALLOC_DECLARE(M_RTABLE);
70 MALLOC_DECLARE(M_FTABLE);
71 MALLOC_DECLARE(M_UFSMNT);
72 MALLOC_DECLARE(M_NETADDR);
73 MALLOC_DECLARE(M_IPMOPTS);
74 MALLOC_DECLARE(M_IPMADDR);
75 MALLOC_DECLARE(M_MRTABLE);
76 MALLOC_DECLARE(M_BWMETER);
77 MALLOC_DECLARE(M_1394DATA);
78 #endif /* _KERNEL */
79
80 /*
81 * Array of descriptors that describe the contents of each page
82 */
83 struct kmemusage {
84 short ku_indx; /* bucket index */
85 union {
86 u_short freecnt;/* for small allocations, free pieces in page */
87 u_short pagecnt;/* for large allocations, pages alloced */
88 } ku_un;
89 };
90 #define ku_freecnt ku_un.freecnt
91 #define ku_pagecnt ku_un.pagecnt
92
93 /*
94 * Set of buckets for each size of memory block that is retained
95 */
96 struct kmembuckets {
97 caddr_t kb_next; /* list of free blocks */
98 caddr_t kb_last; /* last free block */
99 long kb_calls; /* total calls to allocate this size */
100 long kb_total; /* total number of blocks allocated */
101 long kb_totalfree; /* # of free elements in this bucket */
102 long kb_elmpercl; /* # of elements in this sized allocation */
103 long kb_highwat; /* high water mark */
104 long kb_couldfree; /* over high water mark and could free */
105 };
106
107 #ifdef _KERNEL
108 #define MINALLOCSIZE (1 << MINBUCKET)
109 #define BUCKETINDX(size) \
110 ((size) <= (MINALLOCSIZE * 128) \
111 ? (size) <= (MINALLOCSIZE * 8) \
112 ? (size) <= (MINALLOCSIZE * 2) \
113 ? (size) <= (MINALLOCSIZE * 1) \
114 ? (MINBUCKET + 0) \
115 : (MINBUCKET + 1) \
116 : (size) <= (MINALLOCSIZE * 4) \
117 ? (MINBUCKET + 2) \
118 : (MINBUCKET + 3) \
119 : (size) <= (MINALLOCSIZE* 32) \
120 ? (size) <= (MINALLOCSIZE * 16) \
121 ? (MINBUCKET + 4) \
122 : (MINBUCKET + 5) \
123 : (size) <= (MINALLOCSIZE * 64) \
124 ? (MINBUCKET + 6) \
125 : (MINBUCKET + 7) \
126 : (size) <= (MINALLOCSIZE * 2048) \
127 ? (size) <= (MINALLOCSIZE * 512) \
128 ? (size) <= (MINALLOCSIZE * 256) \
129 ? (MINBUCKET + 8) \
130 : (MINBUCKET + 9) \
131 : (size) <= (MINALLOCSIZE * 1024) \
132 ? (MINBUCKET + 10) \
133 : (MINBUCKET + 11) \
134 : (size) <= (MINALLOCSIZE * 8192) \
135 ? (size) <= (MINALLOCSIZE * 4096) \
136 ? (MINBUCKET + 12) \
137 : (MINBUCKET + 13) \
138 : (size) <= (MINALLOCSIZE * 16384) \
139 ? (MINBUCKET + 14) \
140 : (MINBUCKET + 15))
141
142 /*
143 * Turn virtual addresses into kmem map indicies
144 */
145 #define kmemxtob(alloc) (kmembase + (alloc) * NBPG)
146 #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG)
147 #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> PGSHIFT])
148
149 extern struct simplelock malloc_slock;
150
151 /*
152 * Macro versions for the usual cases of malloc/free
153 */
154 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(_LKM) || \
155 defined(MALLOCLOG) || defined(LOCKDEBUG) || defined(MALLOC_NOINLINE)
156 #define MALLOC(space, cast, size, type, flags) \
157 (space) = (cast)malloc((u_long)(size), (type), (flags))
158 #define FREE(addr, type) free((caddr_t)(addr), (type))
159
160 #else /* do not collect statistics */
161 #define MALLOC(space, cast, size, type, flags) \
162 do { \
163 register struct kmembuckets *__kbp = &bucket[BUCKETINDX((size))]; \
164 long __s = splvm(); \
165 simple_lock(&malloc_slock); \
166 if (__kbp->kb_next == NULL) { \
167 simple_unlock(&malloc_slock); \
168 (space) = (cast)malloc((u_long)(size), (type), (flags)); \
169 splx(__s); \
170 } else { \
171 (space) = (cast)__kbp->kb_next; \
172 __kbp->kb_next = *(caddr_t *)(space); \
173 simple_unlock(&malloc_slock); \
174 splx(__s); \
175 if ((flags) & M_ZERO) \
176 memset((space), 0, (size)); \
177 } \
178 } while (/* CONSTCOND */ 0)
179
180 #define FREE(addr, type) \
181 do { \
182 register struct kmembuckets *__kbp; \
183 register struct kmemusage *__kup = btokup((addr)); \
184 long __s = splvm(); \
185 if (1 << __kup->ku_indx > MAXALLOCSAVE) { \
186 free((caddr_t)(addr), (type)); \
187 } else { \
188 simple_lock(&malloc_slock); \
189 __kbp = &bucket[__kup->ku_indx]; \
190 if (__kbp->kb_next == NULL) \
191 __kbp->kb_next = (caddr_t)(addr); \
192 else \
193 *(caddr_t *)(__kbp->kb_last) = (caddr_t)(addr); \
194 *(caddr_t *)(addr) = NULL; \
195 __kbp->kb_last = (caddr_t)(addr); \
196 simple_unlock(&malloc_slock); \
197 } \
198 splx(__s); \
199 } while(/* CONSTCOND */ 0)
200 #endif /* do not collect statistics */
201
202 extern struct kmemusage *kmemusage;
203 extern char *kmembase;
204 extern struct kmembuckets bucket[];
205
206 #ifdef MALLOCLOG
207 void *_malloc(unsigned long, struct malloc_type *, int, const char *, long);
208 void _free(void *, struct malloc_type *, const char *, long);
209 #define malloc(size, type, flags) \
210 _malloc((size), (type), (flags), __FILE__, __LINE__)
211 #define free(addr, type) \
212 _free((addr), (type), __FILE__, __LINE__)
213 #else
214 void *malloc(unsigned long, struct malloc_type *, int);
215 void free(void *, struct malloc_type *);
216 #endif /* MALLOCLOG */
217
218 #ifdef MALLOC_DEBUG
219 int debug_malloc(unsigned long, struct malloc_type *, int, void **);
220 int debug_free(void *, struct malloc_type *);
221 void debug_malloc_init(void);
222
223 void debug_malloc_print(void);
224 void debug_malloc_printit(void (*)(const char *, ...), vaddr_t);
225 #endif /* MALLOC_DEBUG */
226
227 void *realloc(void *, unsigned long, struct malloc_type *, int);
228 unsigned long
229 malloc_roundup(unsigned long);
230 #endif /* _KERNEL */
231 #endif /* !_SYS_MALLOC_H_ */
Cache object: e2f5226a2c6c0b26056b5476bb66cfbd
|