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.11 (Berkeley) 05/09/89 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_PGRP 17 /* process group header */ 49 #define M_SESSION 18 /* session header */ 50 #define M_IOV 19 /* large iov's */ 51 #define M_MOUNT 20 /* vfs mount struct */ 52 #define M_FHANDLE 21 /* network file handle */ 53 #define M_NFSREQ 22 /* NFS request header */ 54 #define M_NFSMNT 23 /* NFS mount structure */ 55 #define M_TEMP 49 /* misc temporary data buffers */ 56 #define M_LAST 50 57 58 struct kmemstats { 59 long ks_inuse; /* # of packets of this type currently in use */ 60 long ks_calls; /* total packets of this type ever allocated */ 61 long ks_memuse; /* total memory held in bytes */ 62 u_short ks_limblocks; /* number of times blocked for hitting limit */ 63 u_short ks_mapblocks; /* number of times blocked for kernel map */ 64 long ks_maxused; /* maximum number ever used */ 65 long ks_limit; /* most that are allowed to exist */ 66 }; 67 68 /* 69 * Array of descriptors that describe the contents of each page 70 */ 71 struct kmemusage { 72 short ku_indx; /* bucket index */ 73 union { 74 u_short freecnt;/* for small allocations, free pieces in page */ 75 u_short pagecnt;/* for large allocations, pages alloced */ 76 } ku_un; 77 }; 78 #define ku_freecnt ku_un.freecnt 79 #define ku_pagecnt ku_un.pagecnt 80 81 /* 82 * Set of buckets for each size of memory block that is retained 83 */ 84 struct kmembuckets { 85 caddr_t kb_next; /* list of free blocks */ 86 long kb_calls; /* total calls to allocate this size */ 87 long kb_total; /* total number of blocks allocated */ 88 long kb_totalfree; /* # of free elements in this bucket */ 89 long kb_elmpercl; /* # of elements in this sized allocation */ 90 long kb_highwat; /* high water mark */ 91 long kb_couldfree; /* over high water mark and could free */ 92 }; 93 94 #ifdef KERNEL 95 #define MINALLOCSIZE (1 << MINBUCKET) 96 #define BUCKETINDX(size) \ 97 (size) <= (MINALLOCSIZE * 128) \ 98 ? (size) <= (MINALLOCSIZE * 8) \ 99 ? (size) <= (MINALLOCSIZE * 2) \ 100 ? (size) <= (MINALLOCSIZE * 1) \ 101 ? (MINBUCKET + 0) \ 102 : (MINBUCKET + 1) \ 103 : (size) <= (MINALLOCSIZE * 4) \ 104 ? (MINBUCKET + 2) \ 105 : (MINBUCKET + 3) \ 106 : (size) <= (MINALLOCSIZE* 32) \ 107 ? (size) <= (MINALLOCSIZE * 16) \ 108 ? (MINBUCKET + 4) \ 109 : (MINBUCKET + 5) \ 110 : (size) <= (MINALLOCSIZE * 64) \ 111 ? (MINBUCKET + 6) \ 112 : (MINBUCKET + 7) \ 113 : (size) <= (MINALLOCSIZE * 2048) \ 114 ? (size) <= (MINALLOCSIZE * 512) \ 115 ? (size) <= (MINALLOCSIZE * 256) \ 116 ? (MINBUCKET + 8) \ 117 : (MINBUCKET + 9) \ 118 : (size) <= (MINALLOCSIZE * 1024) \ 119 ? (MINBUCKET + 10) \ 120 : (MINBUCKET + 11) \ 121 : (size) <= (MINALLOCSIZE * 8192) \ 122 ? (size) <= (MINALLOCSIZE * 4096) \ 123 ? (MINBUCKET + 12) \ 124 : (MINBUCKET + 13) \ 125 : (size) <= (MINALLOCSIZE * 16384) \ 126 ? (MINBUCKET + 14) \ 127 : (MINBUCKET + 15) 128 129 /* 130 * Turn virtual addresses into kmem map indicies 131 */ 132 #define kmemxtob(alloc) (kmembase + (alloc) * NBPG) 133 #define btokmemx(addr) (((caddr_t)(addr) - kmembase) / NBPG) 134 #define btokup(addr) (&kmemusage[((caddr_t)(addr) - kmembase) >> CLSHIFT]) 135 136 /* 137 * Macro versions for the usual cases of malloc/free 138 */ 139 #ifdef KMEMSTATS 140 #define MALLOC(space, cast, size, type, flags) \ 141 (space) = (cast)malloc((u_long)(size), type, flags) 142 #define FREE(addr, type) free((caddr_t)(addr), type) 143 144 #else /* do not collect statistics */ 145 #define MALLOC(space, cast, size, type, flags) { \ 146 register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \ 147 long s = splimp(); \ 148 if (kbp->kb_next == NULL) { \ 149 (space) = (cast)malloc((u_long)(size), type, flags); \ 150 } else { \ 151 (space) = (cast)kbp->kb_next; \ 152 kbp->kb_next = *(caddr_t *)(space); \ 153 } \ 154 splx(s); \ 155 } 156 157 #define FREE(addr, type) { \ 158 register struct kmembuckets *kbp; \ 159 register struct kmemusage *kup = btokup(addr); \ 160 long s = splimp(); \ 161 if (1 << kup->ku_indx > MAXALLOCSAVE) { \ 162 free((caddr_t)(addr), type); \ 163 } else { \ 164 kbp = &bucket[kup->ku_indx]; \ 165 *(caddr_t *)(addr) = kbp->kb_next; \ 166 kbp->kb_next = (caddr_t)(addr); \ 167 } \ 168 splx(s); \ 169 } 170 #endif /* do not collect statistics */ 171 172 extern struct kmemstats kmemstats[]; 173 extern struct kmemusage *kmemusage; 174 extern char kmembase[]; 175 extern struct kmembuckets bucket[]; 176 extern qaddr_t malloc(); 177 extern void free(); 178 #endif KERNEL 179