1 .\" $NetBSD: appendix.t,v 1.2 1998/01/09 06:41:11 perry Exp $
3 .\" Copyright (c) 1988 The Regents of the University of California.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)appendix.t 5.1 (Berkeley) 4/16/91
33 .H 1 "Appendix A - Implementation Details"
38 * Constants for setting the parameters of the kernel memory allocator.
40 * 2 ** MINBUCKET is the smallest unit of memory that will be
41 * allocated. It must be at least large enough to hold a pointer.
43 * Units of memory less or equal to MAXALLOCSAVE will permanently
44 * allocate physical memory; requests for these size pieces of memory
45 * are quite fast. Allocations greater than MAXALLOCSAVE must
46 * always allocate and free physical memory; requests for these size
47 * allocations should be done infrequently as they will be slow.
48 * Constraints: CLBYTES <= MAXALLOCSAVE <= 2 ** (MINBUCKET + 14)
49 * and MAXALLOCSIZE must be a power of two.
51 #define MINBUCKET 4 /* 4 => min allocation of 16 bytes */
52 #define MAXALLOCSAVE (2 * CLBYTES)
55 * Maximum amount of kernel dynamic memory.
56 * Constraints: must be a multiple of the pagesize.
58 #define MAXKMEM (1024 * PAGESIZE)
61 * Arena for all kernel dynamic memory allocation.
62 * This arena is known to start on a page boundary.
64 extern char kmembase[MAXKMEM];
67 * Array of descriptors that describe the contents of each page
70 short ks_indx; /* bucket index, size of small allocations */
71 u_short ks_pagecnt; /* for large allocations, pages allocated */
72 } kmemsizes[MAXKMEM / PAGESIZE];
75 * Set of buckets for each size of memory block that is retained
78 caddr_t kb_next; /* list of free blocks */
79 } bucket[MINBUCKET + 16];
82 * Macro to convert a size to a bucket index. If the size is constant,
83 * this macro reduces to a compile time constant.
85 #define MINALLOCSIZE (1 << MINBUCKET)
86 #define BUCKETINDX(size) \
87 (size) <= (MINALLOCSIZE * 128) \
88 ? (size) <= (MINALLOCSIZE * 8) \
89 ? (size) <= (MINALLOCSIZE * 2) \
90 ? (size) <= (MINALLOCSIZE * 1) \
93 : (size) <= (MINALLOCSIZE * 4) \
96 : (size) <= (MINALLOCSIZE* 32) \
97 ? (size) <= (MINALLOCSIZE * 16) \
100 : (size) <= (MINALLOCSIZE * 64) \
103 : (size) <= (MINALLOCSIZE * 2048) \
107 * Macro versions for the usual cases of malloc/free
109 #define MALLOC(space, cast, size, flags) { \
110 register struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
112 if (kbp->kb_next == NULL) { \
113 (space) = (cast)malloc(size, flags); \
115 (space) = (cast)kbp->kb_next; \
116 kbp->kb_next = *(caddr_t *)(space); \
121 #define FREE(addr) { \
122 register struct kmembuckets *kbp; \
123 register struct kmemsizes *ksp = \
124 &kmemsizes[((addr) - kmembase) / PAGESIZE]; \
126 if (1 << ksp->ks_indx > MAXALLOCSAVE) { \
129 kbp = &bucket[ksp->ks_indx]; \
130 *(caddr_t *)(addr) = kbp->kb_next; \
131 kbp->kb_next = (caddr_t)(addr); \