|
|
1.1 root 1: /*
2: * Copyright (c) 1987 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by the University of
16: * California, Berkeley and its contributors.
17: * 4. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
1.1.1.3 ! root 33: * from: @(#)malloc.h 7.25 (Berkeley) 5/15/91
! 34: * malloc.h,v 1.10.2.1 1993/08/05 02:37:33 cgd Exp
1.1 root 35: */
36:
1.1.1.3 ! root 37: #ifndef _SYS_MALLOC_H_
! 38: #define _SYS_MALLOC_H_
1.1 root 39:
40: #define KMEMSTATS
41:
42: /*
43: * flags to malloc
44: */
45: #define M_WAITOK 0x0000
46: #define M_NOWAIT 0x0001
47:
48: /*
49: * Types of memory to be allocated
50: */
51: #define M_FREE 0 /* should be on free list */
52: #define M_MBUF 1 /* mbuf */
53: #define M_DEVBUF 2 /* device driver memory */
54: #define M_SOCKET 3 /* socket structure */
55: #define M_PCB 4 /* protocol control block */
56: #define M_RTABLE 5 /* routing tables */
57: #define M_HTABLE 6 /* IMP host tables */
58: #define M_FTABLE 7 /* fragment reassembly header */
59: #define M_ZOMBIE 8 /* zombie proc status */
60: #define M_IFADDR 9 /* interface address */
61: #define M_SOOPTS 10 /* socket options */
62: #define M_SONAME 11 /* socket name */
63: #define M_NAMEI 12 /* namei path name buffer */
64: #define M_GPROF 13 /* kernel profiling buffer */
65: #define M_IOCTLOPS 14 /* ioctl data buffer */
66: #define M_SUPERBLK 15 /* super block data */
67: #define M_CRED 16 /* credentials */
68: #define M_PGRP 17 /* process group header */
69: #define M_SESSION 18 /* session header */
70: #define M_IOV 19 /* large iov's */
71: #define M_MOUNT 20 /* vfs mount struct */
72: #define M_FHANDLE 21 /* network file handle */
73: #define M_NFSREQ 22 /* NFS request header */
74: #define M_NFSMNT 23 /* NFS mount structure */
75: #define M_VNODE 24 /* Dynamically allocated vnodes */
76: #define M_CACHE 25 /* Dynamically allocated cache entries */
77: #define M_DQUOT 26 /* UFS quota entries */
78: #define M_UFSMNT 27 /* UFS mount structure */
79: #define M_MAPMEM 28 /* mapped memory descriptors */
80: #define M_SHM 29 /* SVID compatible shared memory segments */
81: #define M_VMMAP 30 /* VM map structures */
82: #define M_VMMAPENT 31 /* VM map entry structures */
83: #define M_VMOBJ 32 /* VM object structure */
84: #define M_VMOBJHASH 33 /* VM object hash structure */
85: #define M_VMPMAP 34 /* VM pmap */
86: #define M_VMPVENT 35 /* VM phys-virt mapping entry */
87: #define M_VMPAGER 36 /* XXX: VM pager struct */
88: #define M_VMPGDATA 37 /* XXX: VM pager private data */
89: #define M_FILE 38 /* Open file structure */
90: #define M_FILEDESC 39 /* Open file descriptor table */
91: #define M_LOCKF 40 /* Byte-range locking structures */
92: #define M_PROC 41 /* Proc structures */
93: #define M_SUBPROC 42 /* Proc sub-structures */
1.1.1.3 ! root 94: #define M_MSDOSFSMNT 43 /* MSDOS FS mount structure */
! 95: #define M_MSDOSFSFAT 44 /* MSDOS FS fat table */
! 96: #define M_TTYS 45 /* allocated tty structures */
! 97: #define M_EXEC 46 /* argument lists & other mem used by exec */
! 98: #define M_MISCFSMNT 47 /* miscfs mount structures */
! 99: #define M_ISOFSMNT 48 /* isofs mount structures */
1.1 root 100: #define M_TEMP 49 /* misc temporary data buffers */
1.1.1.3 ! root 101: #define M_LAST M_TEMP
1.1 root 102:
103: #define INITKMEMNAMES { \
104: "free", /* 0 M_FREE */ \
105: "mbuf", /* 1 M_MBUF */ \
106: "devbuf", /* 2 M_DEVBUF */ \
107: "socket", /* 3 M_SOCKET */ \
108: "pcb", /* 4 M_PCB */ \
109: "routetbl", /* 5 M_RTABLE */ \
110: "hosttbl", /* 6 M_HTABLE */ \
111: "fragtbl", /* 7 M_FTABLE */ \
112: "zombie", /* 8 M_ZOMBIE */ \
113: "ifaddr", /* 9 M_IFADDR */ \
114: "soopts", /* 10 M_SOOPTS */ \
115: "soname", /* 11 M_SONAME */ \
116: "namei", /* 12 M_NAMEI */ \
117: "gprof", /* 13 M_GPROF */ \
118: "ioctlops", /* 14 M_IOCTLOPS */ \
119: "superblk", /* 15 M_SUPERBLK */ \
120: "cred", /* 16 M_CRED */ \
121: "pgrp", /* 17 M_PGRP */ \
122: "session", /* 18 M_SESSION */ \
123: "iov", /* 19 M_IOV */ \
124: "mount", /* 20 M_MOUNT */ \
125: "fhandle", /* 21 M_FHANDLE */ \
126: "NFS req", /* 22 M_NFSREQ */ \
127: "NFS mount", /* 23 M_NFSMNT */ \
128: "vnodes", /* 24 M_VNODE */ \
129: "namecache", /* 25 M_CACHE */ \
130: "UFS quota", /* 26 M_DQUOT */ \
131: "UFS mount", /* 27 M_UFSMNT */ \
132: "mapmem", /* 28 M_MAPMEM */ \
133: "shm", /* 29 M_SHM */ \
134: "VM map", /* 30 M_VMMAP */ \
135: "VM mapent", /* 31 M_VMMAPENT */ \
136: "VM object", /* 32 M_VMOBJ */ \
137: "VM objhash", /* 33 M_VMOBJHASH */ \
138: "VM pmap", /* 34 M_VMPMAP */ \
139: "VM pvmap", /* 35 M_VMPVENT */ \
140: "VM pager", /* 36 M_VMPAGER */ \
141: "VM pgdata", /* 37 M_VMPGDATA */ \
142: "file", /* 38 M_FILE */ \
143: "file desc", /* 39 M_FILEDESC */ \
144: "lockf", /* 40 M_LOCKF */ \
145: "proc", /* 41 M_PROC */ \
146: "subproc", /* 42 M_PROC */ \
1.1.1.3 ! root 147: "MSDOSFS mount", /* 43 M_MSDOSFSMNT */ \
! 148: "MSDOSFS fat", /* 44 M_MSDOSFSFAT */ \
! 149: "ttys", /* 45 M_TTYS */ \
! 150: "exec", /* 46 M_EXEC */ \
! 151: "miscfs mount", /* 47 M_MISCFSMNT */ \
! 152: "isofs mount", /* 48 M_ISOFSMNT */ \
1.1 root 153: "temp", /* 49 M_TEMP */ \
154: }
155:
156: struct kmemstats {
157: long ks_inuse; /* # of packets of this type currently in use */
158: long ks_calls; /* total packets of this type ever allocated */
159: long ks_memuse; /* total memory held in bytes */
160: u_short ks_limblocks; /* number of times blocked for hitting limit */
161: u_short ks_mapblocks; /* number of times blocked for kernel map */
162: long ks_maxused; /* maximum number ever used */
163: long ks_limit; /* most that are allowed to exist */
164: };
165:
166: /*
167: * Array of descriptors that describe the contents of each page
168: */
169: struct kmemusage {
170: short ku_indx; /* bucket index */
171: union {
172: u_short freecnt;/* for small allocations, free pieces in page */
173: u_short pagecnt;/* for large allocations, pages alloced */
174: } ku_un;
175: };
176: #define ku_freecnt ku_un.freecnt
177: #define ku_pagecnt ku_un.pagecnt
178:
179: /*
180: * Set of buckets for each size of memory block that is retained
181: */
182: struct kmembuckets {
183: caddr_t kb_next; /* list of free blocks */
184: long kb_calls; /* total calls to allocate this size */
185: long kb_total; /* total number of blocks allocated */
186: long kb_totalfree; /* # of free elements in this bucket */
187: long kb_elmpercl; /* # of elements in this sized allocation */
188: long kb_highwat; /* high water mark */
189: long kb_couldfree; /* over high water mark and could free */
190: };
191:
192: #ifdef KERNEL
193: #define MINALLOCSIZE (1 << MINBUCKET)
194: #define BUCKETINDX(size) \
195: (size) <= (MINALLOCSIZE * 128) \
196: ? (size) <= (MINALLOCSIZE * 8) \
197: ? (size) <= (MINALLOCSIZE * 2) \
198: ? (size) <= (MINALLOCSIZE * 1) \
199: ? (MINBUCKET + 0) \
200: : (MINBUCKET + 1) \
201: : (size) <= (MINALLOCSIZE * 4) \
202: ? (MINBUCKET + 2) \
203: : (MINBUCKET + 3) \
204: : (size) <= (MINALLOCSIZE* 32) \
205: ? (size) <= (MINALLOCSIZE * 16) \
206: ? (MINBUCKET + 4) \
207: : (MINBUCKET + 5) \
208: : (size) <= (MINALLOCSIZE * 64) \
209: ? (MINBUCKET + 6) \
210: : (MINBUCKET + 7) \
211: : (size) <= (MINALLOCSIZE * 2048) \
212: ? (size) <= (MINALLOCSIZE * 512) \
213: ? (size) <= (MINALLOCSIZE * 256) \
214: ? (MINBUCKET + 8) \
215: : (MINBUCKET + 9) \
216: : (size) <= (MINALLOCSIZE * 1024) \
217: ? (MINBUCKET + 10) \
218: : (MINBUCKET + 11) \
219: : (size) <= (MINALLOCSIZE * 8192) \
220: ? (size) <= (MINALLOCSIZE * 4096) \
221: ? (MINBUCKET + 12) \
222: : (MINBUCKET + 13) \
223: : (size) <= (MINALLOCSIZE * 16384) \
224: ? (MINBUCKET + 14) \
225: : (MINBUCKET + 15)
226:
227: /*
228: * Turn virtual addresses into kmem map indicies
229: */
230: #define kmemxtob(alloc) (kmembase + (alloc) * NBPG)
231: #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG)
232: #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
233:
234: /*
235: * Macro versions for the usual cases of malloc/free
236: */
237: #ifdef KMEMSTATS
238: #define MALLOC(space, cast, size, type, flags) \
239: (space) = (cast)malloc((u_long)(size), type, flags)
240: #define FREE(addr, type) free((caddr_t)(addr), type)
241:
242: #else /* do not collect statistics */
243: #define MALLOC(space, cast, size, type, flags) { \
244: register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
245: long s = splimp(); \
246: if (kbp->kb_next == NULL) { \
247: (space) = (cast)malloc((u_long)(size), type, flags); \
248: } else { \
249: (space) = (cast)kbp->kb_next; \
250: kbp->kb_next = *(caddr_t *)(space); \
251: } \
252: splx(s); \
253: }
254:
255: #define FREE(addr, type) { \
256: register struct kmembuckets *kbp; \
257: register struct kmemusage *kup = btokup(addr); \
258: long s = splimp(); \
259: if (1 << kup->ku_indx > MAXALLOCSAVE) { \
260: free((caddr_t)(addr), type); \
261: } else { \
262: kbp = &bucket[kup->ku_indx]; \
263: *(caddr_t *)(addr) = kbp->kb_next; \
264: kbp->kb_next = (caddr_t)(addr); \
265: } \
266: splx(s); \
267: }
268: #endif /* do not collect statistics */
269:
270: extern struct kmemstats kmemstats[];
271: extern struct kmemusage *kmemusage;
272: extern char *kmembase;
273: extern struct kmembuckets bucket[];
274: extern void *malloc __P((unsigned long size, int type, int flags));
275: extern void free __P((void *addr, int type));
276: #endif /* KERNEL */
1.1.1.3 ! root 277:
! 278: #endif /* !_SYS_MALLOC_H_ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.