Annotation of sys/sys/malloc.h, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: malloc.h,v 1.85 2007/06/17 20:06:10 jasper Exp $ */
2: /* $NetBSD: malloc.h,v 1.39 1998/07/12 19:52:01 augustss Exp $ */
3:
4: /*
5: * Copyright (c) 1987, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. Neither the name of the University nor the names of its contributors
17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: *
32: * @(#)malloc.h 8.5 (Berkeley) 5/3/95
33: */
34:
35: #ifndef _SYS_MALLOC_H_
36: #define _SYS_MALLOC_H_
37:
38: #define KERN_MALLOC_BUCKETS 1
39: #define KERN_MALLOC_BUCKET 2
40: #define KERN_MALLOC_KMEMNAMES 3
41: #define KERN_MALLOC_KMEMSTATS 4
42: #define KERN_MALLOC_MAXID 5
43:
44: #define CTL_KERN_MALLOC_NAMES { \
45: { 0, 0 }, \
46: { "buckets", CTLTYPE_STRING }, \
47: { "bucket", CTLTYPE_NODE }, \
48: { "kmemnames", CTLTYPE_STRING }, \
49: { "kmemstat", CTLTYPE_NODE }, \
50: }
51:
52: /*
53: * flags to malloc
54: */
55: #define M_WAITOK 0x0000
56: #define M_NOWAIT 0x0001
57: #define M_CANFAIL 0x0002
58:
59: /*
60: * Types of memory to be allocated
61: */
62: #define M_FREE 0 /* should be on free list */
63: #define M_MBUF 1 /* mbuf */
64: #define M_DEVBUF 2 /* device driver memory */
65: #define M_DEBUG 3 /* debug chunk */
66: #define M_PCB 4 /* protocol control block */
67: #define M_RTABLE 5 /* routing tables */
68: /* 6 - free */
69: #define M_FTABLE 7 /* fragment reassembly header */
70: /* 8 - free */
71: #define M_IFADDR 9 /* interface address */
72: #define M_SOOPTS 10 /* socket options */
73: #define M_SYSCTL 11 /* sysctl buffers (persistent storage) */
74: /* 12 - free */
75: /* 13 - free */
76: #define M_IOCTLOPS 14 /* ioctl data buffer */
77: /* 15-18 - free */
78: #define M_IOV 19 /* large iov's */
79: #define M_MOUNT 20 /* vfs mount struct */
80: /* 21 - free */
81: #define M_NFSREQ 22 /* NFS request header */
82: #define M_NFSMNT 23 /* NFS mount structure */
83: #define M_NFSNODE 24 /* NFS vnode private part */
84: #define M_VNODE 25 /* Dynamically allocated vnodes */
85: #define M_CACHE 26 /* Dynamically allocated cache entries */
86: #define M_DQUOT 27 /* UFS quota entries */
87: #define M_UFSMNT 28 /* UFS mount structure */
88: #define M_SHM 29 /* SVID compatible shared memory segments */
89: #define M_VMMAP 30 /* VM map structures */
90: #define M_SEM 31 /* SVID compatible semaphores */
91: #define M_DIRHASH 32 /* UFS dirhash */
92: /* 33 - free */
93: #define M_VMPMAP 34 /* VM pmap */
94: /* 35-37 - free */
95: #define M_FILE 38 /* Open file structure */
96: #define M_FILEDESC 39 /* Open file descriptor table */
97: /* 40 - free */
98: #define M_PROC 41 /* Proc structures */
99: #define M_SUBPROC 42 /* Proc sub-structures */
100: #define M_VCLUSTER 43 /* Cluster for VFS */
101: /* 45-46 - free */
102: #define M_MFSNODE 46 /* MFS vnode private part */
103: /* 47-48 - free */
104: #define M_NETADDR 49 /* Export host address structure */
105: #define M_NFSSVC 50 /* Nfs server structure */
106: #define M_NFSUID 51 /* Nfs uid mapping structure */
107: #define M_NFSD 52 /* Nfs server daemon structure */
108: #define M_IPMOPTS 53 /* internet multicast options */
109: #define M_IPMADDR 54 /* internet multicast address */
110: #define M_IFMADDR 55 /* link-level multicast address */
111: #define M_MRTABLE 56 /* multicast routing tables */
112: #define M_ISOFSMNT 57 /* ISOFS mount structure */
113: #define M_ISOFSNODE 58 /* ISOFS vnode private part */
114: #define M_MSDOSFSMNT 59 /* MSDOS FS mount structure */
115: #define M_MSDOSFSFAT 60 /* MSDOS FS fat table */
116: #define M_MSDOSFSNODE 61 /* MSDOS FS vnode private part */
117: #define M_TTYS 62 /* allocated tty structures */
118: #define M_EXEC 63 /* argument lists & other mem used by exec */
119: #define M_MISCFSMNT 64 /* miscfs mount structures */
120: /* 65-73 - free */
121: #define M_PFKEY 74 /* pfkey data */
122: #define M_TDB 75 /* Transforms database */
123: #define M_XDATA 76 /* IPsec data */
124: /* 77 - free */
125: #define M_PAGEDEP 78 /* File page dependencies */
126: #define M_INODEDEP 79 /* Inode dependencies */
127: #define M_NEWBLK 80 /* New block allocation */
128: /* 81-82 - free */
129: #define M_INDIRDEP 83 /* Indirect block dependencies */
130: /* 84-91 - free */
131: #define M_VMSWAP 92 /* VM swap structures */
132: /* 93-96 - free */
133: #define M_RAIDFRAME 97 /* RAIDframe data */
134: #define M_UVMAMAP 98 /* UVM amap and related */
135: #define M_UVMAOBJ 99 /* UVM aobj and related */
136: /* 100 - free */
137: #define M_USB 101 /* USB general */
138: #define M_USBDEV 102 /* USB device driver */
139: #define M_USBHC 103 /* USB host controller */
140: /* 104 - free */
141: #define M_MEMDESC 105 /* Memory range */
142: /* 106-107 - free */
143: #define M_CRYPTO_DATA 108 /* Crypto framework data buffers (keys etc.) */
144: /* 109 - free */
145: #define M_CREDENTIALS 110 /* IPsec-related credentials and ID info */
146: #define M_PACKET_TAGS 111 /* Packet-attached information */
147: #define M_1394CTL 112 /* IEEE 1394 control structures */
148: #define M_1394DATA 113 /* IEEE 1394 data buffers */
149: #define M_EMULDATA 114 /* Per-process emulation data */
150: /* 115-122 - free */
151:
152: /* KAME IPv6 */
153: #define M_IP6OPT 123 /* IPv6 options */
154: #define M_IP6NDP 124 /* IPv6 Neighbour Discovery */
155: #define M_IP6RR 125 /* IPv6 Router Renumbering Prefix */
156: #define M_RR_ADDR 126 /* IPv6 Router Renumbering Ifid */
157: #define M_TEMP 127 /* misc temporary data buffers */
158:
159: #define M_NTFSMNT 128 /* NTFS mount structure */
160: #define M_NTFSNTNODE 129 /* NTFS ntnode information */
161: #define M_NTFSFNODE 130 /* NTFS fnode information */
162: #define M_NTFSDIR 131 /* NTFS dir buffer */
163: #define M_NTFSNTHASH 132 /* NTFS ntnode hash tables */
164: #define M_NTFSNTVATTR 133 /* NTFS file attribute information */
165: #define M_NTFSRDATA 134 /* NTFS resident data */
166: #define M_NTFSDECOMP 135 /* NTFS decompression temporary */
167: #define M_NTFSRUN 136 /* NTFS vrun storage */
168:
169: #define M_KEVENT 137 /* kqueue related */
170:
171: #define M_BLUETOOTH 138 /* Bluetooth */
172:
173: #define M_BWMETER 139 /* Multicast upcall bw meters */
174:
175: #define M_UDFMOUNT 140 /* UDF mount */
176: #define M_UDFFENTRY 141 /* UDF file entry */
177: #define M_UDFFID 142 /* UDF file id */
178:
179: #define M_LAST 143 /* Must be last type + 1 */
180:
181: #define INITKMEMNAMES { \
182: "free", /* 0 M_FREE */ \
183: "mbuf", /* 1 M_MBUF */ \
184: "devbuf", /* 2 M_DEVBUF */ \
185: "debug", /* 3 M_DEBUG */ \
186: "pcb", /* 4 M_PCB */ \
187: "routetbl", /* 5 M_RTABLE */ \
188: NULL, /* 6 */ \
189: "fragtbl", /* 7 M_FTABLE */ \
190: NULL, \
191: "ifaddr", /* 9 M_IFADDR */ \
192: "soopts", /* 10 M_SOOPTS */ \
193: "sysctl", /* 11 M_SYSCTL */ \
194: NULL, \
195: NULL, \
196: "ioctlops", /* 14 M_IOCTLOPS */ \
197: NULL, \
198: NULL, \
199: NULL, \
200: NULL, \
201: "iov", /* 19 M_IOV */ \
202: "mount", /* 20 M_MOUNT */ \
203: NULL, \
204: "NFS req", /* 22 M_NFSREQ */ \
205: "NFS mount", /* 23 M_NFSMNT */ \
206: "NFS node", /* 24 M_NFSNODE */ \
207: "vnodes", /* 25 M_VNODE */ \
208: "namecache", /* 26 M_CACHE */ \
209: "UFS quota", /* 27 M_DQUOT */ \
210: "UFS mount", /* 28 M_UFSMNT */ \
211: "shm", /* 29 M_SHM */ \
212: "VM map", /* 30 M_VMMAP */ \
213: "sem", /* 31 M_SEM */ \
214: "dirhash", /* 32 M_DIRHASH */ \
215: NULL, \
216: "VM pmap", /* 34 M_VMPMAP */ \
217: NULL, /* 35 */ \
218: NULL, /* 36 */ \
219: NULL, /* 37 */ \
220: "file", /* 38 M_FILE */ \
221: "file desc", /* 39 M_FILEDESC */ \
222: NULL, /* 40 */ \
223: "proc", /* 41 M_PROC */ \
224: "subproc", /* 42 M_SUBPROC */ \
225: "VFS cluster", /* 43 M_VCLUSTER */ \
226: NULL, \
227: NULL, \
228: "MFS node", /* 46 M_MFSNODE */ \
229: NULL, \
230: NULL, \
231: "Export Host", /* 49 M_NETADDR */ \
232: "NFS srvsock", /* 50 M_NFSSVC */ \
233: "NFS uid", /* 51 M_NFSUID */ \
234: "NFS daemon", /* 52 M_NFSD */ \
235: "ip_moptions", /* 53 M_IPMOPTS */ \
236: "in_multi", /* 54 M_IPMADDR */ \
237: "ether_multi", /* 55 M_IFMADDR */ \
238: "mrt", /* 56 M_MRTABLE */ \
239: "ISOFS mount", /* 57 M_ISOFSMNT */ \
240: "ISOFS node", /* 58 M_ISOFSNODE */ \
241: "MSDOSFS mount", /* 59 M_MSDOSFSMNT */ \
242: "MSDOSFS fat", /* 60 M_MSDOSFSFAT */ \
243: "MSDOSFS node", /* 61 M_MSDOSFSNODE */ \
244: "ttys", /* 62 M_TTYS */ \
245: "exec", /* 63 M_EXEC */ \
246: "miscfs mount", /* 64 M_MISCFSMNT */ \
247: NULL, \
248: NULL, \
249: NULL, \
250: NULL, \
251: NULL, \
252: NULL, \
253: NULL, \
254: NULL, \
255: NULL, \
256: "pfkey data", /* 74 M_PFKEY */ \
257: "tdb", /* 75 M_TDB */ \
258: "xform_data", /* 76 M_XDATA */ \
259: NULL, \
260: "pagedep", /* 78 M_PAGEDEP */ \
261: "inodedep", /* 79 M_INODEDEP */ \
262: "newblk", /* 80 M_NEWBLK */ \
263: NULL, \
264: NULL, \
265: "indirdep", /* 83 M_INDIRDEP */ \
266: NULL, NULL, NULL, NULL, \
267: NULL, NULL, NULL, NULL, \
268: "VM swap", /* 92 M_VMSWAP */ \
269: NULL, NULL, NULL, NULL, \
270: "RAIDframe data", /* 97 M_RAIDFRAME */ \
271: "UVM amap", /* 98 M_UVMAMAP */ \
272: "UVM aobj", /* 99 M_UVMAOBJ */ \
273: NULL, \
274: "USB", /* 101 M_USB */ \
275: "USB device", /* 102 M_USBDEV */ \
276: "USB HC", /* 103 M_USBHC */ \
277: NULL, \
278: "memdesc", /* 105 M_MEMDESC */ \
279: NULL, /* 106 */ \
280: NULL, \
281: "crypto data", /* 108 M_CRYPTO_DATA */ \
282: NULL, \
283: "IPsec creds", /* 110 M_CREDENTIALS */ \
284: "packet tags", /* 111 M_PACKET_TAGS */ \
285: "1394ctl", /* 112 M_1394CTL */ \
286: "1394data", /* 113 M_1394DATA */ \
287: "emuldata", /* 114 M_EMULDATA */ \
288: NULL, NULL, NULL, NULL, \
289: NULL, NULL, NULL, NULL, \
290: "ip6_options", /* 123 M_IP6OPT */ \
291: "NDP", /* 124 M_IP6NDP */ \
292: "ip6rr", /* 125 M_IP6RR */ \
293: "rp_addr", /* 126 M_RR_ADDR */ \
294: "temp", /* 127 M_TEMP */ \
295: "NTFS mount", /* 128 M_NTFSMNT */ \
296: "NTFS node", /* 129 M_NTFSNTNODE */ \
297: "NTFS fnode", /* 130 M_NTFSFNODE */ \
298: "NTFS dir", /* 131 M_NTFSDIR */ \
299: "NTFS hash tables", /* 132 M_NTFSNTHASH */ \
300: "NTFS file attr", /* 133 M_NTFSNTVATTR */ \
301: "NTFS resident data ", /* 134 M_NTFSRDATA */ \
302: "NTFS decomp", /* 135 M_NTFSDECOMP */ \
303: "NTFS vrun", /* 136 M_NTFSRUN */ \
304: "kqueue", /* 137 M_KEVENT */ \
305: "bluetooth", /* 138 M_BLUETOOTH */ \
306: "bwmeter", /* 139 M_BWMETER */ \
307: "UDF mount", /* 140 M_UDFMOUNT */ \
308: "UDF file entry", /* 141 M_UDFFENTRY */ \
309: "UDF file id", /* 142 M_UDFFID */ \
310: }
311:
312: struct kmemstats {
313: long ks_inuse; /* # of packets of this type currently in use */
314: long ks_calls; /* total packets of this type ever allocated */
315: long ks_memuse; /* total memory held in bytes */
316: u_short ks_limblocks; /* number of times blocked for hitting limit */
317: u_short ks_mapblocks; /* number of times blocked for kernel map */
318: long ks_maxused; /* maximum number ever used */
319: long ks_limit; /* most that are allowed to exist */
320: long ks_size; /* sizes of this thing that are allocated */
321: long ks_spare;
322: };
323:
324: /*
325: * Array of descriptors that describe the contents of each page
326: */
327: struct kmemusage {
328: short ku_indx; /* bucket index */
329: union {
330: u_short freecnt;/* for small allocations, free pieces in page */
331: u_short pagecnt;/* for large allocations, pages alloced */
332: } ku_un;
333: };
334: #define ku_freecnt ku_un.freecnt
335: #define ku_pagecnt ku_un.pagecnt
336:
337: /*
338: * Set of buckets for each size of memory block that is retained
339: */
340: struct kmembuckets {
341: caddr_t kb_next; /* list of free blocks */
342: caddr_t kb_last; /* last free block */
343: u_int64_t kb_calls; /* total calls to allocate this size */
344: u_int64_t kb_total; /* total number of blocks allocated */
345: u_int64_t kb_totalfree; /* # of free elements in this bucket */
346: u_int64_t kb_elmpercl; /* # of elements in this sized allocation */
347: u_int64_t kb_highwat; /* high water mark */
348: u_int64_t kb_couldfree; /* over high water mark and could free */
349: };
350:
351: #ifdef _KERNEL
352: #define MINALLOCSIZE (1 << MINBUCKET)
353: #define BUCKETINDX(size) \
354: ((size) <= (MINALLOCSIZE * 128) \
355: ? (size) <= (MINALLOCSIZE * 8) \
356: ? (size) <= (MINALLOCSIZE * 2) \
357: ? (size) <= (MINALLOCSIZE * 1) \
358: ? (MINBUCKET + 0) \
359: : (MINBUCKET + 1) \
360: : (size) <= (MINALLOCSIZE * 4) \
361: ? (MINBUCKET + 2) \
362: : (MINBUCKET + 3) \
363: : (size) <= (MINALLOCSIZE* 32) \
364: ? (size) <= (MINALLOCSIZE * 16) \
365: ? (MINBUCKET + 4) \
366: : (MINBUCKET + 5) \
367: : (size) <= (MINALLOCSIZE * 64) \
368: ? (MINBUCKET + 6) \
369: : (MINBUCKET + 7) \
370: : (size) <= (MINALLOCSIZE * 2048) \
371: ? (size) <= (MINALLOCSIZE * 512) \
372: ? (size) <= (MINALLOCSIZE * 256) \
373: ? (MINBUCKET + 8) \
374: : (MINBUCKET + 9) \
375: : (size) <= (MINALLOCSIZE * 1024) \
376: ? (MINBUCKET + 10) \
377: : (MINBUCKET + 11) \
378: : (size) <= (MINALLOCSIZE * 8192) \
379: ? (size) <= (MINALLOCSIZE * 4096) \
380: ? (MINBUCKET + 12) \
381: : (MINBUCKET + 13) \
382: : (size) <= (MINALLOCSIZE * 16384) \
383: ? (MINBUCKET + 14) \
384: : (MINBUCKET + 15))
385:
386: /*
387: * Turn virtual addresses into kmem map indices
388: */
389: #define kmemxtob(alloc) (kmembase + (alloc) * NBPG)
390: #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG)
391: #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> PAGE_SHIFT])
392:
393: /*
394: * Macro versions for the usual cases of malloc/free
395: */
396: #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(_LKM) || defined(SMALL_KERNEL)
397: #define MALLOC(space, cast, size, type, flags) \
398: (space) = (cast)malloc((u_long)(size), type, flags)
399: #define FREE(addr, type) free((caddr_t)(addr), type)
400:
401: #else /* do not collect statistics */
402: #define MALLOC(space, cast, size, type, flags) do { \
403: u_long kbp_size = (u_long)(size); \
404: struct kmembuckets *kbp = &bucket[BUCKETINDX(kbp_size)]; \
405: int __s = splvm(); \
406: if (kbp->kb_next == NULL) { \
407: (space) = (cast)malloc(kbp_size, type, flags); \
408: } else { \
409: (space) = (cast)kbp->kb_next; \
410: kbp->kb_next = *(caddr_t *)(space); \
411: } \
412: splx(__s); \
413: } while (0)
414:
415: #define FREE(addr, type) do { \
416: struct kmembuckets *kbp; \
417: struct kmemusage *kup = btokup(addr); \
418: int __s = splvm(); \
419: if (1 << kup->ku_indx > MAXALLOCSAVE) { \
420: free((caddr_t)(addr), type); \
421: } else { \
422: kbp = &bucket[kup->ku_indx]; \
423: if (kbp->kb_next == NULL) \
424: kbp->kb_next = (caddr_t)(addr); \
425: else \
426: *(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
427: *(caddr_t *)(addr) = NULL; \
428: kbp->kb_last = (caddr_t)(addr); \
429: } \
430: splx(__s); \
431: } while(0)
432: #endif /* do not collect statistics */
433:
434: extern struct kmemstats kmemstats[];
435: extern struct kmemusage *kmemusage;
436: extern char *kmembase;
437: extern struct kmembuckets bucket[];
438:
439: extern void *malloc(unsigned long size, int type, int flags);
440: extern void free(void *addr, int type);
441: extern int sysctl_malloc(int *, u_int, void *, size_t *, void *, size_t,
442: struct proc *);
443:
444: size_t malloc_roundup(size_t);
445: void malloc_printit(int (*)(const char *, ...));
446:
447: #ifdef MALLOC_DEBUG
448: int debug_malloc(unsigned long, int, int, void **);
449: int debug_free(void *, int);
450: void debug_malloc_init(void);
451: void debug_malloc_assert_allocated(void *, const char *);
452: #define DEBUG_MALLOC_ASSERT_ALLOCATED(addr) \
453: debug_malloc_assert_allocated(addr, __func__)
454:
455: void debug_malloc_print(void);
456: void debug_malloc_printit(int (*)(const char *, ...), vaddr_t);
457: #endif /* MALLOC_DEBUG */
458: #endif /* _KERNEL */
459: #endif /* !_SYS_MALLOC_H_ */
CVSweb