|
|
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 are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: *
17: * @(#)malloc.h 7.7 (Berkeley) 6/27/88
18: */
19:
20: #define KMEMSTATS
21:
22: /*
23: * flags to malloc
24: */
25: #define M_WAITOK 0x0000
26: #define M_NOWAIT 0x0001
27:
28: /*
29: * Types of memory to be allocated
30: */
31: #define M_FREE 0 /* should be on free list */
32: #define M_MBUF 1 /* mbuf */
33: #define M_DEVBUF 2 /* device driver memory */
34: #define M_SOCKET 3 /* socket structure */
35: #define M_PCB 4 /* protocol control block */
36: #define M_RTABLE 5 /* routing tables */
37: #define M_HTABLE 6 /* IMP host tables */
38: #define M_FTABLE 7 /* fragment reassembly header */
39: #define M_ZOMBIE 8 /* zombie proc status */
40: #define M_IFADDR 9 /* interface address */
41: #define M_SOOPTS 10 /* socket options */
42: #define M_SONAME 11 /* socket name */
43: #define M_NAMEI 12 /* namei path name buffer */
44: #define M_GPROF 13 /* kernel profiling buffer */
45: #define M_IOCTLOPS 14 /* ioctl data buffer */
46: #define M_SUPERBLK 15 /* super block data */
47: #define M_CRED 16 /* credentials */
48: #define M_TEMP 49 /* misc temporary data buffers */
49: #define M_LAST 50
50:
51: struct kmemstats {
52: long ks_inuse; /* # of packets of this type currently in use */
53: long ks_calls; /* total packets of this type ever allocated */
54: long ks_memuse; /* total memory held in bytes */
55: u_short ks_limblocks; /* number of times blocked for hitting limit */
56: u_short ks_mapblocks; /* number of times blocked for kernel map */
57: long ks_maxused; /* maximum number ever used */
58: long ks_limit; /* most that are allowed to exist */
59: };
60:
61: /*
62: * Array of descriptors that describe the contents of each page
63: */
64: struct kmemusage {
65: short ku_indx; /* bucket index */
66: union {
67: u_short freecnt;/* for small allocations, free pieces in page */
68: u_short pagecnt;/* for large allocations, pages alloced */
69: } ku_un;
70: };
71: #define ku_freecnt ku_un.freecnt
72: #define ku_pagecnt ku_un.pagecnt
73:
74: /*
75: * Set of buckets for each size of memory block that is retained
76: */
77: struct kmembuckets {
78: caddr_t kb_next; /* list of free blocks */
79: long kb_calls; /* total calls to allocate this size */
80: long kb_total; /* total number of blocks allocated */
81: long kb_totalfree; /* # of free elements in this bucket */
82: long kb_elmpercl; /* # of elements in this sized allocation */
83: long kb_highwat; /* high water mark */
84: long kb_couldfree; /* over high water mark and could free */
85: };
86:
87: #ifdef KERNEL
88: #define MINALLOCSIZE (1 << MINBUCKET)
89: #define BUCKETINDX(size) \
90: (size) <= (MINALLOCSIZE * 128) \
91: ? (size) <= (MINALLOCSIZE * 8) \
92: ? (size) <= (MINALLOCSIZE * 2) \
93: ? (size) <= (MINALLOCSIZE * 1) \
94: ? (MINBUCKET + 0) \
95: : (MINBUCKET + 1) \
96: : (size) <= (MINALLOCSIZE * 4) \
97: ? (MINBUCKET + 2) \
98: : (MINBUCKET + 3) \
99: : (size) <= (MINALLOCSIZE* 32) \
100: ? (size) <= (MINALLOCSIZE * 16) \
101: ? (MINBUCKET + 4) \
102: : (MINBUCKET + 5) \
103: : (size) <= (MINALLOCSIZE * 64) \
104: ? (MINBUCKET + 6) \
105: : (MINBUCKET + 7) \
106: : (size) <= (MINALLOCSIZE * 2048) \
107: ? (size) <= (MINALLOCSIZE * 512) \
108: ? (size) <= (MINALLOCSIZE * 256) \
109: ? (MINBUCKET + 8) \
110: : (MINBUCKET + 9) \
111: : (size) <= (MINALLOCSIZE * 1024) \
112: ? (MINBUCKET + 10) \
113: : (MINBUCKET + 11) \
114: : (size) <= (MINALLOCSIZE * 8192) \
115: ? (size) <= (MINALLOCSIZE * 4096) \
116: ? (MINBUCKET + 12) \
117: : (MINBUCKET + 13) \
118: : (size) <= (MINALLOCSIZE * 16384) \
119: ? (MINBUCKET + 14) \
120: : (MINBUCKET + 15)
121:
122: /*
123: * Turn virtual addresses into kmem map indicies
124: */
125: #define kmemxtob(alloc) (kmembase + (alloc) * NBPG)
126: #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG)
127: #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
128:
129: /*
130: * Macro versions for the usual cases of malloc/free
131: */
132: #ifdef KMEMSTATS
133: #define MALLOC(space, cast, size, type, flags) \
134: (space) = (cast)malloc((u_long)(size), type, flags)
135: #define FREE(addr, type) free((caddr_t)(addr), type)
136:
137: #else /* do not collect statistics */
138: #define MALLOC(space, cast, size, type, flags) { \
139: register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
140: long s = splimp(); \
141: if (kbp->kb_next == NULL) { \
142: (space) = (cast)malloc((u_long)(size), type, flags); \
143: } else { \
144: (space) = (cast)kbp->kb_next; \
145: kbp->kb_next = *(caddr_t *)(space); \
146: } \
147: splx(s); \
148: }
149:
150: #define FREE(addr, type) { \
151: register struct kmembuckets *kbp; \
152: register struct kmemusage *kup = btokup(addr); \
153: long s = splimp(); \
154: if (1 << kup->ku_indx > MAXALLOCSAVE) { \
155: free((caddr_t)(addr), type); \
156: } else { \
157: kbp = &bucket[kup->ku_indx]; \
158: *(caddr_t *)(addr) = kbp->kb_next; \
159: kbp->kb_next = (caddr_t)(addr); \
160: } \
161: splx(s); \
162: }
163: #endif /* do not collect statistics */
164:
165: extern struct kmemstats kmemstats[];
166: extern struct kmemusage *kmemusage;
167: extern char kmembase[];
168: extern struct kmembuckets bucket[];
169: extern qaddr_t malloc();
170: extern void free();
171: #endif KERNEL
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.