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