1 /* 2 * Copyright (c) 1987, 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)kern_malloc.c 7.25 (Berkeley) 05/08/91 8 */ 9 10 #include "param.h" 11 #include "proc.h" 12 #include "map.h" 13 #include "kernel.h" 14 #include "malloc.h" 15 #include "vm/vm.h" 16 #include "vm/vm_kern.h" 17 18 struct kmembuckets bucket[MINBUCKET + 16]; 19 struct kmemstats kmemstats[M_LAST]; 20 struct kmemusage *kmemusage; 21 char *kmembase, *kmemlimit; 22 char *memname[] = INITKMEMNAMES; 23 24 /* 25 * Allocate a block of memory 26 */ 27 void * 28 malloc(size, type, flags) 29 unsigned long size; 30 int type, flags; 31 { 32 register struct kmembuckets *kbp; 33 register struct kmemusage *kup; 34 long indx, npg, alloc, allocsize; 35 int s; 36 caddr_t va, cp, savedlist; 37 #ifdef KMEMSTATS 38 register struct kmemstats *ksp = &kmemstats[type]; 39 40 if (((unsigned long)type) > M_LAST) 41 panic("malloc - bogus type"); 42 #endif 43 44 indx = BUCKETINDX(size); 45 kbp = &bucket[indx]; 46 s = splimp(); 47 #ifdef KMEMSTATS 48 while (ksp->ks_memuse >= ksp->ks_limit) { 49 if (flags & M_NOWAIT) { 50 splx(s); 51 return ((void *) NULL); 52 } 53 if (ksp->ks_limblocks < 65535) 54 ksp->ks_limblocks++; 55 tsleep((caddr_t)ksp, PSWP+2, memname[type], 0); 56 } 57 #endif 58 if (kbp->kb_next == NULL) { 59 if (size > MAXALLOCSAVE) 60 allocsize = roundup(size, CLBYTES); 61 else 62 allocsize = 1 << indx; 63 npg = clrnd(btoc(allocsize)); 64 va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), 65 !(flags & M_NOWAIT)); 66 if (va == NULL) { 67 splx(s); 68 return ((void *) NULL); 69 } 70 #ifdef KMEMSTATS 71 kbp->kb_total += kbp->kb_elmpercl; 72 #endif 73 kup = btokup(va); 74 kup->ku_indx = indx; 75 if (allocsize > MAXALLOCSAVE) { 76 if (npg > 65535) 77 panic("malloc: allocation too large"); 78 kup->ku_pagecnt = npg; 79 #ifdef KMEMSTATS 80 ksp->ks_memuse += allocsize; 81 #endif 82 goto out; 83 } 84 #ifdef KMEMSTATS 85 kup->ku_freecnt = kbp->kb_elmpercl; 86 kbp->kb_totalfree += kbp->kb_elmpercl; 87 #endif 88 /* 89 * Just in case we blocked while allocating memory, 90 * and someone else also allocated memory for this 91 * bucket, don't assume the list is still empty. 92 */ 93 savedlist = kbp->kb_next; 94 kbp->kb_next = va + (npg * NBPG) - allocsize; 95 for (cp = kbp->kb_next; cp > va; cp -= allocsize) 96 *(caddr_t *)cp = cp - allocsize; 97 *(caddr_t *)cp = savedlist; 98 } 99 va = kbp->kb_next; 100 kbp->kb_next = *(caddr_t *)va; 101 #ifdef KMEMSTATS 102 kup = btokup(va); 103 if (kup->ku_indx != indx) 104 panic("malloc: wrong bucket"); 105 if (kup->ku_freecnt == 0) 106 panic("malloc: lost data"); 107 kup->ku_freecnt--; 108 kbp->kb_totalfree--; 109 ksp->ks_memuse += 1 << indx; 110 out: 111 kbp->kb_calls++; 112 ksp->ks_inuse++; 113 ksp->ks_calls++; 114 if (ksp->ks_memuse > ksp->ks_maxused) 115 ksp->ks_maxused = ksp->ks_memuse; 116 #else 117 out: 118 #endif 119 splx(s); 120 return ((void *) va); 121 } 122 123 #ifdef DIAGNOSTIC 124 long addrmask[] = { 0x00000000, 125 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 126 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 127 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 128 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 129 }; 130 #endif /* DIAGNOSTIC */ 131 132 /* 133 * Free a block of memory allocated by malloc. 134 */ 135 void 136 free(addr, type) 137 void *addr; 138 int type; 139 { 140 register struct kmembuckets *kbp; 141 register struct kmemusage *kup; 142 long alloc, size; 143 int s; 144 #ifdef KMEMSTATS 145 register struct kmemstats *ksp = &kmemstats[type]; 146 #endif 147 148 kup = btokup(addr); 149 size = 1 << kup->ku_indx; 150 #ifdef DIAGNOSTIC 151 if (size > NBPG * CLSIZE) 152 alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)]; 153 else 154 alloc = addrmask[kup->ku_indx]; 155 if (((u_long)addr & alloc) != 0) { 156 printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n", 157 addr, size, type, alloc); 158 panic("free: unaligned addr"); 159 } 160 #endif /* DIAGNOSTIC */ 161 kbp = &bucket[kup->ku_indx]; 162 s = splimp(); 163 if (size > MAXALLOCSAVE) { 164 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt)); 165 #ifdef KMEMSTATS 166 size = kup->ku_pagecnt << PGSHIFT; 167 ksp->ks_memuse -= size; 168 kup->ku_indx = 0; 169 kup->ku_pagecnt = 0; 170 if (ksp->ks_memuse + size >= ksp->ks_limit && 171 ksp->ks_memuse < ksp->ks_limit) 172 wakeup((caddr_t)ksp); 173 ksp->ks_inuse--; 174 kbp->kb_total -= 1; 175 #endif 176 splx(s); 177 return; 178 } 179 #ifdef KMEMSTATS 180 kup->ku_freecnt++; 181 if (kup->ku_freecnt >= kbp->kb_elmpercl) 182 if (kup->ku_freecnt > kbp->kb_elmpercl) 183 panic("free: multiple frees"); 184 else if (kbp->kb_totalfree > kbp->kb_highwat) 185 kbp->kb_couldfree++; 186 kbp->kb_totalfree++; 187 ksp->ks_memuse -= size; 188 if (ksp->ks_memuse + size >= ksp->ks_limit && 189 ksp->ks_memuse < ksp->ks_limit) 190 wakeup((caddr_t)ksp); 191 ksp->ks_inuse--; 192 #endif 193 *(caddr_t *)addr = kbp->kb_next; 194 kbp->kb_next = addr; 195 splx(s); 196 } 197 198 /* 199 * Initialize the kernel memory allocator 200 */ 201 kmeminit() 202 { 203 register long indx; 204 int npg; 205 206 #if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0) 207 ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2 208 #endif 209 #if (MAXALLOCSAVE > MINALLOCSIZE * 32768) 210 ERROR!_kmeminit:_MAXALLOCSAVE_too_big 211 #endif 212 #if (MAXALLOCSAVE < CLBYTES) 213 ERROR!_kmeminit:_MAXALLOCSAVE_too_small 214 #endif 215 npg = VM_KMEM_SIZE/ NBPG; 216 kmemusage = (struct kmemusage *) kmem_alloc(kernel_map, 217 (vm_size_t)(npg * sizeof(struct kmemusage))); 218 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t)&kmembase, 219 (vm_offset_t)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE); 220 #ifdef KMEMSTATS 221 for (indx = 0; indx < MINBUCKET + 16; indx++) { 222 if (1 << indx >= CLBYTES) 223 bucket[indx].kb_elmpercl = 1; 224 else 225 bucket[indx].kb_elmpercl = CLBYTES / (1 << indx); 226 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl; 227 } 228 for (indx = 0; indx < M_LAST; indx++) 229 kmemstats[indx].ks_limit = npg * NBPG * 6 / 10; 230 #endif 231 } 232