11 'ds / \\h'\\w' 'u-\\w'/'u'/
12 'ds /* \\h'\\w' 'u-\\w'/'u'/*
21 'tl '\s14\f3\\*(=F\fP\s0'\\*(=H'\f3\s14\\*(=F\fP\s0'
24 \f2\s12\h'\\n(.lu-\w'\\*(=f'u'\\*(=f\fP\s0\h'|0u'
32 'ie o 'tl '\f2\\*(=M''Page % of \\*(=G\fP'
33 'el 'tl '\f2Page % of \\*(=G''\\*(=M\fP'
60 \f2\s14\h'\\n(.lu-\w'\\$1'u'\\$1\fP\s0\h'|0u'\c
61 .if r x .if \\nx .if d =F .tm \\$1 \\*(=F \\n%
65 .if r x .if \\nx .if d =F .tm \\$1 \\*(=F \\n%
74 .\" Copyright (c) 1988 The Regents of the University of California.
75 .\" All rights reserved.
77 .\" Redistribution and use in source and binary forms, with or without
78 .\" modification, are permitted provided that the following conditions
80 .\" 1. Redistributions of source code must retain the above copyright
81 .\" notice, this list of conditions and the following disclaimer.
82 .\" 2. Redistributions in binary form must reproduce the above copyright
83 .\" notice, this list of conditions and the following disclaimer in the
84 .\" documentation and/or other materials provided with the distribution.
85 .\" 3. All advertising materials mentioning features or use of this software
86 .\" must display the following acknowledgement:
87 .\" This product includes software developed by the University of
88 .\" California, Berkeley and its contributors.
89 .\" 4. Neither the name of the University nor the names of its contributors
90 .\" may be used to endorse or promote products derived from this software
91 .\" without specific prior written permission.
93 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
97 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105 .\" @(#)appendix.t 5.1 (Berkeley) 4/16/91
108 .H 1 "Appendix A - Implementation Details"
112 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
115 \fI*\fP Constants for setting the parameters of the kernel memory allocator\&.
117 \fI*\fP 2 \fI*\fP\fI*\fP MINBUCKET is the smallest unit of memory that will be
118 \fI*\fP allocated\&. It must be at least large enough to hold a pointer\&.
120 \fI*\fP Units of memory less or equal to MAXALLOCSAVE will permanently
121 \fI*\fP allocate physical memory; requests for these size pieces of memory
122 \fI*\fP are quite fast\&. Allocations greater than MAXALLOCSAVE must
123 \fI*\fP always allocate and free physical memory; requests for these size
124 \fI*\fP allocations should be done infrequently as they will be slow\&.
125 \fI*\fP Constraints: CLBYTES <= MAXALLOCSAVE <= 2 \fI*\fP\fI*\fP (MINBUCKET + 14)
126 \fI*\fP and MAXALLOCSIZE must be a power of two\&.
127 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
130 \*(+K#define\*(-K MINBUCKET\h'|31n'4\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
132 4 => min allocation of 16 bytes \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
136 \*(+K#define\*(-K MAXALLOCSAVE\h'|31n'(2 \fI*\fP CLBYTES)
138 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
141 \fI*\fP Maximum amount of kernel dynamic memory\&.
142 \fI*\fP Constraints: must be a multiple of the pagesize\&.
143 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
147 \*(+K#define\*(-K MAXKMEM\h'|31n'(1024 \fI*\fP PAGESIZE)
149 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
152 \fI*\fP Arena for all kernel dynamic memory allocation\&.
153 \fI*\fP This arena is known to start on a page boundary\&.
154 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
157 \*(+Kextern\*(-K \*(+Kchar\*(-K kmembase[MAXKMEM];
159 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
162 \fI*\fP Array of descriptors that describe the contents of each page
163 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
166 \*(+Kstruct\*(-K kmemsizes \*(+K{\*(-K
167 \h'|11n'\*(+Kshort\*(-K\h'|21n'ks\*_indx;\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
169 bucket index, size of small allocations \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
172 \h'|11n'u\*_short\h'|21n'ks\*_pagecnt;\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
174 for large allocations, pages allocated \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
179 kmemsizes[MAXKMEM \fI\h'\w' 'u-\w'/'u'/\fP PAGESIZE];
182 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
185 \fI*\fP Set of buckets for each size of memory block that is retained
186 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
189 \*(+Kstruct\*(-K kmembuckets \*(+K{\*(-K
190 \h'|11n'caddr\*_t kb\*_next;\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
192 list of free blocks \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
197 bucket[MINBUCKET + 16];
199 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
202 \fI*\fP Macro to convert a size to a bucket index\&. If the size is constant,
203 \fI*\fP this macro reduces to a compile time constant\&.
204 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
208 \*(+K#define\*(-K MINALLOCSIZE\h'|31n'(1 << MINBUCKET)
210 \*(+K#define\*(-K BUCKETINDX(size) \e
211 \h'|11n'(size) <= (MINALLOCSIZE \fI*\fP 128) \e
212 \h'|21n'? (size) <= (MINALLOCSIZE \fI*\fP 8) \e
213 \h'|31n'? (size) <= (MINALLOCSIZE \fI*\fP 2) \e
214 \h'|41n'? (size) <= (MINALLOCSIZE \fI*\fP 1) \e
215 \h'|51n'? (MINBUCKET + 0) \e
216 \h'|51n': (MINBUCKET + 1) \e
217 \h'|41n': (size) <= (MINALLOCSIZE \fI*\fP 4) \e
218 \h'|51n'? (MINBUCKET + 2) \e
219 \h'|51n': (MINBUCKET + 3) \e
220 \h'|31n': (size) <= (MINALLOCSIZE\fI*\fP 32) \e
221 \h'|41n'? (size) <= (MINALLOCSIZE \fI*\fP 16) \e
222 \h'|51n'? (MINBUCKET + 4) \e
223 \h'|51n': (MINBUCKET + 5) \e
224 \h'|41n': (size) <= (MINALLOCSIZE \fI*\fP 64) \e
225 \h'|51n'? (MINBUCKET + 6) \e
226 \h'|51n': (MINBUCKET + 7) \e
227 \h'|21n': (size) <= (MINALLOCSIZE \fI*\fP 2048) \e
228 \h'|31n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
230 etc \&.\&.\&. \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
234 \fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
237 \fI*\fP Macro versions for the usual cases of malloc\fI\h'\w' 'u-\w'/'u'/\fPfree
238 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
242 \*(+K#define\*(-K MALLOC(space, cast, size, flags) \*(+K{\*(-K \e
243 \h'|11n'\*(+Kregister\*(-K \*(+Kstruct\*(-K kmembuckets \fI*\fPkbp = &bucket[BUCKETINDX(size)]; \e
244 \h'|11n'\*(+Klong\*(-K s = splimp(); \e
245 \h'|11n'\*(+Kif\*(-K (kbp\*->kb\*_next == NULL) \*(+K{\*(-K \e
246 \h'|21n'(space) = (cast)malloc(size, flags); \e
247 \h'|11n'\*(+K}\*(-K \*(+Kelse\*(-K \*(+K{\*(-K \e
248 \h'|21n'(space) = (cast)kbp\*->kb\*_next; \e
249 \h'|21n'kbp\*->kb\*_next = \fI*\fP(caddr\*_t \fI*\fP)(space); \e
250 \h'|11n'\*(+K}\*(-K \e
258 \*(+K#define\*(-K FREE(addr) \*(+K{\*(-K \e
259 \h'|11n'\*(+Kregister\*(-K \*(+Kstruct\*(-K kmembuckets \fI*\fPkbp; \e
260 \h'|11n'\*(+Kregister\*(-K \*(+Kstruct\*(-K kmemsizes \fI*\fPksp = \e
261 \h'|21n'&kmemsizes[((addr) \*- kmembase) \fI\h'\w' 'u-\w'/'u'/\fP PAGESIZE]; \e
262 \h'|11n'\*(+Klong\*(-K s = splimp(); \e
263 \h'|11n'\*(+Kif\*(-K (1 << ksp\*->ks\*_indx > MAXALLOCSAVE) \*(+K{\*(-K \e
264 \h'|21n'free(addr); \e
265 \h'|11n'\*(+K}\*(-K \*(+Kelse\*(-K \*(+K{\*(-K \e
266 \h'|21n'kbp = &bucket[ksp\*->ks\*_indx]; \e
267 \h'|21n'\fI*\fP(caddr\*_t \fI*\fP)(addr) = kbp\*->kb\*_next; \e
268 \h'|21n'kbp\*->kb\*_next = (caddr\*_t)(addr); \e
269 \h'|11n'\*(+K}\*(-K \e