- Merge aicom-network-branch (still without the NDIS stuff)
[reactos.git] / reactos / lib / drivers / oskittcp / include / freebsd / src / sys / sys / malloc.h
1 /*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. 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 *
33 * @(#)malloc.h 8.3 (Berkeley) 1/12/94
34 */
35
36 #ifndef _SYS_MALLOC_H_
37 #define _SYS_MALLOC_H_
38
39 #if !defined(OSKIT) || defined(__REACTOS__)
40 #define KMEMSTATS
41 #endif
42
43 /*
44 * flags to malloc
45 */
46 #define M_WAITOK 0x0000
47 #define M_NOWAIT 0x0001
48 #define M_KERNEL 0x0002
49
50 /*
51 * Types of memory to be allocated
52 */
53 #define M_FREE 0 /* should be on free list */
54 #define M_MBUF 1 /* mbuf */
55 #define M_DEVBUF 2 /* device driver memory */
56 #define M_SOCKET 3 /* socket structure */
57 #define M_PCB 4 /* protocol control block */
58 #define M_RTABLE 5 /* routing tables */
59 #define M_HTABLE 6 /* IMP host tables */
60 #define M_FTABLE 7 /* fragment reassembly header */
61 #define M_ZOMBIE 8 /* zombie proc status */
62 #define M_IFADDR 9 /* interface address */
63 #define M_SOOPTS 10 /* socket options */
64 #define M_SONAME 11 /* socket name */
65 #define M_NAMEI 12 /* namei path name buffer */
66 #define M_GPROF 13 /* kernel profiling buffer */
67 #define M_IOCTLOPS 14 /* ioctl data buffer */
68 #define M_MAPMEM 15 /* mapped memory descriptors */
69 #define M_CRED 16 /* credentials */
70 #define M_PGRP 17 /* process group header */
71 #define M_SESSION 18 /* session header */
72 #define M_IOV 19 /* large iov's */
73 #define M_MOUNT 20 /* vfs mount struct */
74 #define M_FHANDLE 21 /* network file handle */
75 #define M_NFSREQ 22 /* NFS request header */
76 #define M_NFSMNT 23 /* NFS mount structure */
77 #define M_NFSNODE 24 /* NFS vnode private part */
78 #define M_VNODE 25 /* Dynamically allocated vnodes */
79 #define M_CACHE 26 /* Dynamically allocated cache entries */
80 #define M_DQUOT 27 /* UFS quota entries */
81 #define M_UFSMNT 28 /* UFS mount structure */
82 #define M_SHM 29 /* SVID compatible shared memory segments */
83 #define M_VMMAP 30 /* VM map structures */
84 #define M_VMMAPENT 31 /* VM map entry structures */
85 #define M_VMOBJ 32 /* VM object structure */
86 #define M_VMOBJHASH 33 /* VM object hash structure */
87 #define M_VMPMAP 34 /* VM pmap */
88 #define M_VMPVENT 35 /* VM phys-virt mapping entry */
89 #define M_VMPAGER 36 /* XXX: VM pager struct */
90 #define M_VMPGDATA 37 /* XXX: VM pager private data */
91 #define M_FILE 38 /* Open file structure */
92 #define M_FILEDESC 39 /* Open file descriptor table */
93 #define M_LOCKF 40 /* Byte-range locking structures */
94 #define M_PROC 41 /* Proc structures */
95 #define M_SUBPROC 42 /* Proc sub-structures */
96 #define M_SEGMENT 43 /* Segment for LFS */
97 #define M_LFSNODE 44 /* LFS vnode private part */
98 #define M_FFSNODE 45 /* FFS vnode private part */
99 #define M_MFSNODE 46 /* MFS vnode private part */
100 #define M_NQLEASE 47 /* Nqnfs lease */
101 #define M_NQMHOST 48 /* Nqnfs host address table */
102 #define M_NETADDR 49 /* Export host address structure */
103 #define M_NFSSVC 50 /* Nfs server structure */
104 #define M_NFSUID 51 /* Nfs uid mapping structure */
105 #define M_NFSD 52 /* Nfs server daemon structure */
106 #define M_IPMOPTS 53 /* internet multicast options */
107 #define M_IPMADDR 54 /* internet multicast address */
108 #define M_IFMADDR 55 /* link-level multicast address */
109 #define M_MRTABLE 56 /* multicast routing tables */
110 #define M_ISOFSMNT 57 /* ISOFS mount structure */
111 #define M_ISOFSNODE 58 /* ISOFS vnode private part */
112 #define M_MSDOSFSMNT 59 /* MSDOSFS mount structure */
113 #define M_MSDOSFSNODE 60 /* MSDOSFS vnode private part */
114 #define M_MSDOSFSFAT 61 /* MSDOSFS file allocation table */
115 #define M_DEVFSMNT 62 /* DEVFS mount structure */
116 #define M_DEVFSBACK 63 /* DEVFS Back node */
117 #define M_DEVFSFRONT 64 /* DEVFS Front node */
118 #define M_DEVFSNODE 65 /* DEVFS node */
119 #define M_TEMP 74 /* misc temporary data buffers */
120 #define M_TTYS 75 /* tty data structures */
121 #define M_GZIP 76 /* Gzip trees */
122 #define M_IPFW 77 /* IpFw/IpAcct chain's */
123 #define M_DEVL 78 /* isa_device lists in userconfig() */
124 #define M_LAST 79 /* Must be last type + 1 */
125
126 #define INITKMEMNAMES { \
127 "free", /* 0 M_FREE */ \
128 "mbuf", /* 1 M_MBUF */ \
129 "devbuf", /* 2 M_DEVBUF */ \
130 "socket", /* 3 M_SOCKET */ \
131 "pcb", /* 4 M_PCB */ \
132 "routetbl", /* 5 M_RTABLE */ \
133 "hosttbl", /* 6 M_HTABLE */ \
134 "fragtbl", /* 7 M_FTABLE */ \
135 "zombie", /* 8 M_ZOMBIE */ \
136 "ifaddr", /* 9 M_IFADDR */ \
137 "soopts", /* 10 M_SOOPTS */ \
138 "soname", /* 11 M_SONAME */ \
139 "namei", /* 12 M_NAMEI */ \
140 "gprof", /* 13 M_GPROF */ \
141 "ioctlops", /* 14 M_IOCTLOPS */ \
142 "mapmem", /* 15 M_MAPMEM */ \
143 "cred", /* 16 M_CRED */ \
144 "pgrp", /* 17 M_PGRP */ \
145 "session", /* 18 M_SESSION */ \
146 "iov", /* 19 M_IOV */ \
147 "mount", /* 20 M_MOUNT */ \
148 "fhandle", /* 21 M_FHANDLE */ \
149 "NFS req", /* 22 M_NFSREQ */ \
150 "NFS mount", /* 23 M_NFSMNT */ \
151 "NFS node", /* 24 M_NFSNODE */ \
152 "vnodes", /* 25 M_VNODE */ \
153 "namecache", /* 26 M_CACHE */ \
154 "UFS quota", /* 27 M_DQUOT */ \
155 "UFS mount", /* 28 M_UFSMNT */ \
156 "shm", /* 29 M_SHM */ \
157 "VM map", /* 30 M_VMMAP */ \
158 "VM mapent", /* 31 M_VMMAPENT */ \
159 "VM object", /* 32 M_VMOBJ */ \
160 "VM objhash", /* 33 M_VMOBJHASH */ \
161 "VM pmap", /* 34 M_VMPMAP */ \
162 "VM pvmap", /* 35 M_VMPVENT */ \
163 "VM pager", /* 36 M_VMPAGER */ \
164 "VM pgdata", /* 37 M_VMPGDATA */ \
165 "file", /* 38 M_FILE */ \
166 "file desc", /* 39 M_FILEDESC */ \
167 "lockf", /* 40 M_LOCKF */ \
168 "proc", /* 41 M_PROC */ \
169 "subproc", /* 42 M_SUBPROC */ \
170 "LFS segment", /* 43 M_SEGMENT */ \
171 "LFS node", /* 44 M_LFSNODE */ \
172 "FFS node", /* 45 M_FFSNODE */ \
173 "MFS node", /* 46 M_MFSNODE */ \
174 "NQNFS Lease", /* 47 M_NQLEASE */ \
175 "NQNFS Host", /* 48 M_NQMHOST */ \
176 "Export Host", /* 49 M_NETADDR */ \
177 "NFS srvsock", /* 50 M_NFSSVC */ \
178 "NFS uid", /* 51 M_NFSUID */ \
179 "NFS daemon", /* 52 M_NFSD */ \
180 "ip_moptions", /* 53 M_IPMOPTS */ \
181 "in_multi", /* 54 M_IPMADDR */ \
182 "ether_multi", /* 55 M_IFMADDR */ \
183 "mrt", /* 56 M_MRTABLE */ \
184 "ISOFS mount", /* 57 M_ISOFSMNT */ \
185 "ISOFS node", /* 58 M_ISOFSNODE */ \
186 "MSDOSFS mount",/* 59 M_MSDOSFSMNT */ \
187 "MSDOSFS node", /* 60 M_MSDOSFSNODE */ \
188 "MSDOSFS FAT", /* 61 M_MSDOSFSFAR */ \
189 "DEVFS mount", /* 62 M_DEVFSMNT */ \
190 "DEVFS back", /* 63 M_DEVFSBACK */ \
191 "DEVFS front", /* 64 M_DEVFSFRONT */ \
192 "DEVFS node", /* 65 M_DEVFSNODE */ \
193 NULL, \
194 NULL, NULL, NULL, NULL, NULL, \
195 NULL, NULL, \
196 "temp", /* 74 M_TEMP */ \
197 "ttys", /* 75 M_TTYS */ \
198 "Gzip trees", /* 76 M_GZIP */ \
199 "IpFw/IpAcct", /* 77 M_IPFW */ \
200 "isa_devlist", /* 78 M_DEVL */ \
201 }
202
203 struct kmemstats {
204 long ks_inuse; /* # of packets of this type currently in use */
205 long ks_calls; /* total packets of this type ever allocated */
206 long ks_memuse; /* total memory held in bytes */
207 u_short ks_limblocks; /* number of times blocked for hitting limit */
208 u_short ks_mapblocks; /* number of times blocked for kernel map */
209 long ks_maxused; /* maximum number ever used */
210 long ks_limit; /* most that are allowed to exist */
211 long ks_size; /* sizes of this thing that are allocated */
212 long ks_spare;
213 };
214
215 /*
216 * Array of descriptors that describe the contents of each page
217 */
218 struct kmemusage {
219 short ku_indx; /* bucket index */
220 union {
221 u_short freecnt;/* for small allocations, free pieces in page */
222 u_short pagecnt;/* for large allocations, pages alloced */
223 } ku_un;
224 };
225 #define ku_freecnt ku_un.freecnt
226 #define ku_pagecnt ku_un.pagecnt
227
228 /*
229 * Set of buckets for each size of memory block that is retained
230 */
231 struct kmembuckets {
232 caddr_t kb_next; /* list of free blocks */
233 caddr_t kb_last; /* last free block */
234 long kb_calls; /* total calls to allocate this size */
235 long kb_total; /* total number of blocks allocated */
236 long kb_totalfree; /* # of free elements in this bucket */
237 long kb_elmpercl; /* # of elements in this sized allocation */
238 long kb_highwat; /* high water mark */
239 long kb_couldfree; /* over high water mark and could free */
240 };
241
242 #ifdef KERNEL
243 #define MINALLOCSIZE (1 << MINBUCKET)
244 #define BUCKETINDX(size) \
245 (size) <= (MINALLOCSIZE * 128) \
246 ? (size) <= (MINALLOCSIZE * 8) \
247 ? (size) <= (MINALLOCSIZE * 2) \
248 ? (size) <= (MINALLOCSIZE * 1) \
249 ? (MINBUCKET + 0) \
250 : (MINBUCKET + 1) \
251 : (size) <= (MINALLOCSIZE * 4) \
252 ? (MINBUCKET + 2) \
253 : (MINBUCKET + 3) \
254 : (size) <= (MINALLOCSIZE* 32) \
255 ? (size) <= (MINALLOCSIZE * 16) \
256 ? (MINBUCKET + 4) \
257 : (MINBUCKET + 5) \
258 : (size) <= (MINALLOCSIZE * 64) \
259 ? (MINBUCKET + 6) \
260 : (MINBUCKET + 7) \
261 : (size) <= (MINALLOCSIZE * 2048) \
262 ? (size) <= (MINALLOCSIZE * 512) \
263 ? (size) <= (MINALLOCSIZE * 256) \
264 ? (MINBUCKET + 8) \
265 : (MINBUCKET + 9) \
266 : (size) <= (MINALLOCSIZE * 1024) \
267 ? (MINBUCKET + 10) \
268 : (MINBUCKET + 11) \
269 : (size) <= (MINALLOCSIZE * 8192) \
270 ? (size) <= (MINALLOCSIZE * 4096) \
271 ? (MINBUCKET + 12) \
272 : (MINBUCKET + 13) \
273 : (size) <= (MINALLOCSIZE * 16384) \
274 ? (MINBUCKET + 14) \
275 : (MINBUCKET + 15)
276
277 /*
278 * Turn virtual addresses into kmem map indicies
279 */
280 #define kmemxtob(alloc) (kmembase + (alloc) * NBPG)
281 #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG)
282 #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT])
283
284 /*
285 * Macro versions for the usual cases of malloc/free
286 */
287 #if defined(KMEMSTATS) || defined(DIAGNOSTIC)
288 #define MALLOC(space, cast, size, type, flags) \
289 (space) = (cast)fbsd_malloc((u_long)(size), __FILE__, __LINE__, type, flags)
290 #define FREE(addr, type) fbsd_free((caddr_t)(addr), __FILE__, __LINE__, type)
291 #else /* do not collect statistics */
292 #define MALLOC(space, cast, size, type, flags) { \
293 register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
294 long s = splimp(); \
295 if (kbp->kb_next == NULL) { \
296 (space) = (cast)fbsd_malloc((u_long)(size), __FILE__, __LINE__, type, flags); \
297 } else { \
298 (space) = (cast)kbp->kb_next; \
299 kbp->kb_next = *(caddr_t *)(space); \
300 } \
301 splx(s); \
302 }
303
304 #define FREE(addr, type) { \
305 register struct kmembuckets *kbp; \
306 register struct kmemusage *kup = btokup(addr); \
307 long s = splimp(); \
308 if (1 << kup->ku_indx > MAXALLOCSAVE) { \
309 fbsd_free((caddr_t)(addr), __FILE__, __LINE__, type); \
310 } else { \
311 kbp = &bucket[kup->ku_indx]; \
312 if (kbp->kb_next == NULL) \
313 kbp->kb_next = (caddr_t)(addr); \
314 else \
315 *(caddr_t *)(kbp->kb_last) = (caddr_t)(addr); \
316 *(caddr_t *)(addr) = NULL; \
317 kbp->kb_last = (caddr_t)(addr); \
318 } \
319 splx(s); \
320 }
321 #endif /* do not collect statistics */
322
323 extern struct kmemstats kmemstats[];
324 extern struct kmemusage *kmemusage;
325 extern char *kmembase;
326 extern struct kmembuckets bucket[];
327 #ifndef __REACTOS__
328 extern void *malloc __P((unsigned long size, ...));
329 extern void free __P((void *addr, ...));
330 #else
331 #define malloc(size, flags, id) fbsd_malloc(size, __FILE__, __LINE__)
332 #define free(area, flags) fbsd_free(area, __FILE__, __LINE__)
333 #endif
334 #endif /* KERNEL */
335 #endif /* !_SYS_MALLOC_H_ */