bcd3e8ce7ef794fa4c4edf5dcc1537830abcbb53
[dragonfly.git] / share / doc / papers / kernmalloc / appendix.t
1 .\" Copyright (c) 1988 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)appendix.t  5.1 (Berkeley) 4/16/91
33 .\"
34 .bp
35 .H 1 "Appendix A - Implementation Details"
36 .LP
37 .nf
38 .vS
39 /*
40  * Constants for setting the parameters of the kernel memory allocator.
41  *
42  * 2 ** MINBUCKET is the smallest unit of memory that will be
43  * allocated. It must be at least large enough to hold a pointer.
44  *
45  * Units of memory less or equal to MAXALLOCSAVE will permanently
46  * allocate physical memory; requests for these size pieces of memory
47  * are quite fast. Allocations greater than MAXALLOCSAVE must
48  * always allocate and free physical memory; requests for these size
49  * allocations should be done infrequently as they will be slow.
50  * Constraints: CLBYTES <= MAXALLOCSAVE <= 2 ** (MINBUCKET + 14)
51  * and MAXALLOCSIZE must be a power of two.
52  */
53 #define MINBUCKET       4               /* 4 => min allocation of 16 bytes */
54 #define MAXALLOCSAVE    (2 * CLBYTES)
55
56 /*
57  * Maximum amount of kernel dynamic memory.
58  * Constraints: must be a multiple of the pagesize.
59  */
60 #define MAXKMEM         (1024 * PAGESIZE)
61
62 /*
63  * Arena for all kernel dynamic memory allocation.
64  * This arena is known to start on a page boundary.
65  */
66 extern char kmembase[MAXKMEM];
67
68 /*
69  * Array of descriptors that describe the contents of each page
70  */
71 struct kmemsizes {
72         short   ks_indx;        /* bucket index, size of small allocations */
73         u_short ks_pagecnt;     /* for large allocations, pages allocated */
74 } kmemsizes[MAXKMEM / PAGESIZE];
75
76 /*
77  * Set of buckets for each size of memory block that is retained
78  */
79 struct kmembuckets {
80         caddr_t kb_next;        /* list of free blocks */
81 } bucket[MINBUCKET + 16];
82 .bp
83 /*
84  * Macro to convert a size to a bucket index. If the size is constant,
85  * this macro reduces to a compile time constant.
86  */
87 #define MINALLOCSIZE    (1 << MINBUCKET)
88 #define BUCKETINDX(size) \
89         (size) <= (MINALLOCSIZE * 128) \
90                 ? (size) <= (MINALLOCSIZE * 8) \
91                         ? (size) <= (MINALLOCSIZE * 2) \
92                                 ? (size) <= (MINALLOCSIZE * 1) \
93                                         ? (MINBUCKET + 0) \
94                                         : (MINBUCKET + 1) \
95                                 : (size) <= (MINALLOCSIZE * 4) \
96                                         ? (MINBUCKET + 2) \
97                                         : (MINBUCKET + 3) \
98                         : (size) <= (MINALLOCSIZE* 32) \
99                                 ? (size) <= (MINALLOCSIZE * 16) \
100                                         ? (MINBUCKET + 4) \
101                                         : (MINBUCKET + 5) \
102                                 : (size) <= (MINALLOCSIZE * 64) \
103                                         ? (MINBUCKET + 6) \
104                                         : (MINBUCKET + 7) \
105                 : (size) <= (MINALLOCSIZE * 2048) \
106                         /* etc ... */
107
108 /*
109  * Macro versions for the usual cases of malloc/free
110  */
111 #define MALLOC(space, cast, size, flags) { \
112         register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
113         long s = splimp(); \
114         if (kbp->kb_next == NULL) { \
115                 (space) = (cast)malloc(size, flags); \
116         } else { \
117                 (space) = (cast)kbp->kb_next; \
118                 kbp->kb_next = *(caddr_t *)(space); \
119         } \
120         splx(s); \
121 }
122
123 #define FREE(addr) { \
124         register struct kmembuckets *kbp; \
125         register struct kmemsizes *ksp = \
126                 &kmemsizes[((addr) - kmembase) / PAGESIZE]; \
127         long s = splimp(); \
128         if (1 << ksp->ks_indx > MAXALLOCSAVE) { \
129                 free(addr); \
130         } else { \
131                 kbp = &bucket[ksp->ks_indx]; \
132                 *(caddr_t *)(addr) = kbp->kb_next; \
133                 kbp->kb_next = (caddr_t)(addr); \
134         } \
135         splx(s); \
136 }
137 .vE