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