1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #if 1
30*0Sstevel@tonic-gate #undef DEBUG
31*0Sstevel@tonic-gate #endif
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate /* #define DEBUG ON */
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate * Conditions on use:
37*0Sstevel@tonic-gate * kmem_alloc and kmem_free must not be called from interrupt level,
38*0Sstevel@tonic-gate * except from software interrupt level. This is because they are
39*0Sstevel@tonic-gate * not reentrant, and only block out software interrupts. They take
40*0Sstevel@tonic-gate * too long to block any real devices. There is a routine
41*0Sstevel@tonic-gate * kmem_free_intr that can be used to free blocks at interrupt level,
42*0Sstevel@tonic-gate * but only up to splimp, not higher. This is because kmem_free_intr
43*0Sstevel@tonic-gate * only spl's to splimp.
44*0Sstevel@tonic-gate *
45*0Sstevel@tonic-gate * Also, these routines are not that fast, so they should not be used
46*0Sstevel@tonic-gate * in very frequent operations (e.g. operations that happen more often
47*0Sstevel@tonic-gate * than, say, once every few seconds).
48*0Sstevel@tonic-gate */
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate * description:
52*0Sstevel@tonic-gate * Yet another memory allocator, this one based on a method
53*0Sstevel@tonic-gate * described in C.J. Stephenson, "Fast Fits", IBM Sys. Journal
54*0Sstevel@tonic-gate *
55*0Sstevel@tonic-gate * The basic data structure is a "Cartesian" binary tree, in which
56*0Sstevel@tonic-gate * nodes are ordered by ascending addresses (thus minimizing free
57*0Sstevel@tonic-gate * list insertion time) and block sizes decrease with depth in the
58*0Sstevel@tonic-gate * tree (thus minimizing search time for a block of a given size).
59*0Sstevel@tonic-gate *
60*0Sstevel@tonic-gate * In other words, for any node s, letting D(s) denote
61*0Sstevel@tonic-gate * the set of descendents of s, we have:
62*0Sstevel@tonic-gate *
63*0Sstevel@tonic-gate * a. addr(D(left(s))) < addr(s) < addr(D(right(s)))
64*0Sstevel@tonic-gate * b. len(D(left(s))) <= len(s) >= len(D(right(s)))
65*0Sstevel@tonic-gate */
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate #include <sys/param.h>
68*0Sstevel@tonic-gate #include <sys/sysmacros.h>
69*0Sstevel@tonic-gate #include <sys/salib.h>
70*0Sstevel@tonic-gate #include <sys/saio.h>
71*0Sstevel@tonic-gate #include <sys/promif.h>
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate * The node header structure.
75*0Sstevel@tonic-gate *
76*0Sstevel@tonic-gate * To reduce storage consumption, a header block is associated with
77*0Sstevel@tonic-gate * free blocks only, not allocated blocks.
78*0Sstevel@tonic-gate * When a free block is allocated, its header block is put on
79*0Sstevel@tonic-gate * a free header block list.
80*0Sstevel@tonic-gate *
81*0Sstevel@tonic-gate * This creates a header space and a free block space.
82*0Sstevel@tonic-gate * The left pointer of a header blocks is used to chain free header
83*0Sstevel@tonic-gate * blocks together.
84*0Sstevel@tonic-gate */
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate typedef enum {false, true} bool;
87*0Sstevel@tonic-gate typedef struct freehdr *Freehdr;
88*0Sstevel@tonic-gate typedef struct dblk *Dblk;
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate * Description of a header for a free block
92*0Sstevel@tonic-gate * Only free blocks have such headers.
93*0Sstevel@tonic-gate */
94*0Sstevel@tonic-gate struct freehdr {
95*0Sstevel@tonic-gate Freehdr left; /* Left tree pointer */
96*0Sstevel@tonic-gate Freehdr right; /* Right tree pointer */
97*0Sstevel@tonic-gate Dblk block; /* Ptr to the data block */
98*0Sstevel@tonic-gate size_t size; /* Size of the data block */
99*0Sstevel@tonic-gate };
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate #define NIL ((Freehdr) 0)
102*0Sstevel@tonic-gate #define WORDSIZE sizeof (int)
103*0Sstevel@tonic-gate #define SMALLEST_BLK 1 /* Size of smallest block */
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate * Description of a data block.
107*0Sstevel@tonic-gate */
108*0Sstevel@tonic-gate struct dblk {
109*0Sstevel@tonic-gate char data[1]; /* Addr returned to the caller */
110*0Sstevel@tonic-gate };
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate * weight(x) is the size of a block, in bytes; or 0 if and only if x
114*0Sstevel@tonic-gate * is a null pointer. It is the responsibility of kmem_alloc() and
115*0Sstevel@tonic-gate * kmem_free() to keep zero-length blocks out of the arena.
116*0Sstevel@tonic-gate */
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate #define weight(x) ((x) == NIL? 0: (x->size))
119*0Sstevel@tonic-gate #define nextblk(p, size) ((Dblk) ((char *)(p) + (size)))
120*0Sstevel@tonic-gate #define max(a, b) ((a) < (b)? (b): (a))
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate void *kmem_alloc(size_t, int);
123*0Sstevel@tonic-gate void kmem_free(void *ptr, size_t nbytes);
124*0Sstevel@tonic-gate Freehdr getfreehdr(void);
125*0Sstevel@tonic-gate static bool morecore(size_t);
126*0Sstevel@tonic-gate void insert(Dblk p, size_t len, Freehdr *tree);
127*0Sstevel@tonic-gate void freehdr(Freehdr p);
128*0Sstevel@tonic-gate void delete(Freehdr *p);
129*0Sstevel@tonic-gate static void check_need_to_free(void);
130*0Sstevel@tonic-gate extern caddr_t resalloc(enum RESOURCES, size_t, caddr_t, int);
131*0Sstevel@tonic-gate #ifdef __sparc
132*0Sstevel@tonic-gate extern void resalloc_init(void);
133*0Sstevel@tonic-gate #endif
134*0Sstevel@tonic-gate extern int splnet(void);
135*0Sstevel@tonic-gate extern int splimp(void);
136*0Sstevel@tonic-gate extern void splx(int);
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate /*
139*0Sstevel@tonic-gate * Structure containing various info about allocated memory.
140*0Sstevel@tonic-gate */
141*0Sstevel@tonic-gate #define NEED_TO_FREE_SIZE 5
142*0Sstevel@tonic-gate struct kmem_info {
143*0Sstevel@tonic-gate Freehdr free_root;
144*0Sstevel@tonic-gate Freehdr free_hdr_list;
145*0Sstevel@tonic-gate struct map *map;
146*0Sstevel@tonic-gate struct pte *pte;
147*0Sstevel@tonic-gate caddr_t vaddr;
148*0Sstevel@tonic-gate struct need_to_free {
149*0Sstevel@tonic-gate caddr_t addr;
150*0Sstevel@tonic-gate size_t nbytes;
151*0Sstevel@tonic-gate } need_to_free_list, need_to_free[NEED_TO_FREE_SIZE];
152*0Sstevel@tonic-gate } kmem_info;
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate struct map *kernelmap;
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate #ifdef DEBUG
158*0Sstevel@tonic-gate static void prtree(Freehdr, char *);
159*0Sstevel@tonic-gate #endif
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate /*
162*0Sstevel@tonic-gate * Initialize kernel memory allocator
163*0Sstevel@tonic-gate */
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate void
kmem_init(void)166*0Sstevel@tonic-gate kmem_init(void)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate int i;
169*0Sstevel@tonic-gate struct need_to_free *ntf;
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate #ifdef DEBUG
172*0Sstevel@tonic-gate printf("kmem_init entered\n");
173*0Sstevel@tonic-gate #endif
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate #ifdef __sparc
176*0Sstevel@tonic-gate resalloc_init();
177*0Sstevel@tonic-gate #endif
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate kmem_info.free_root = NIL;
180*0Sstevel@tonic-gate kmem_info.free_hdr_list = NULL;
181*0Sstevel@tonic-gate kmem_info.map = kernelmap;
182*0Sstevel@tonic-gate kmem_info.need_to_free_list.addr = 0;
183*0Sstevel@tonic-gate ntf = kmem_info.need_to_free;
184*0Sstevel@tonic-gate for (i = 0; i < NEED_TO_FREE_SIZE; i++) {
185*0Sstevel@tonic-gate ntf[i].addr = 0;
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate #ifdef DEBUG
188*0Sstevel@tonic-gate printf("kmem_init returning\n");
189*0Sstevel@tonic-gate prtree(kmem_info.free_root, "kmem_init");
190*0Sstevel@tonic-gate #endif
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate * Insert a new node in a cartesian tree or subtree, placing it
195*0Sstevel@tonic-gate * in the correct position with respect to the existing nodes.
196*0Sstevel@tonic-gate *
197*0Sstevel@tonic-gate * algorithm:
198*0Sstevel@tonic-gate * Starting from the root, a binary search is made for the new
199*0Sstevel@tonic-gate * node. If this search were allowed to continue, it would
200*0Sstevel@tonic-gate * eventually fail (since there cannot already be a node at the
201*0Sstevel@tonic-gate * given address); but in fact it stops when it reaches a node in
202*0Sstevel@tonic-gate * the tree which has a length less than that of the new node (or
203*0Sstevel@tonic-gate * when it reaches a null tree pointer). The new node is then
204*0Sstevel@tonic-gate * inserted at the root of the subtree for which the shorter node
205*0Sstevel@tonic-gate * forms the old root (or in place of the null pointer).
206*0Sstevel@tonic-gate */
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate void
insert(Dblk p,size_t len,Freehdr * tree)210*0Sstevel@tonic-gate insert(Dblk p, /* Ptr to the block to insert */
211*0Sstevel@tonic-gate size_t len, /* Length of new node */
212*0Sstevel@tonic-gate Freehdr *tree) /* Address of ptr to root */
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate Freehdr x;
215*0Sstevel@tonic-gate Freehdr *left_hook; /* Temp for insertion */
216*0Sstevel@tonic-gate Freehdr *right_hook; /* Temp for insertion */
217*0Sstevel@tonic-gate Freehdr newhdr;
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate x = *tree;
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate * Search for the first node which has a weight less
222*0Sstevel@tonic-gate * than that of the new node; this will be the
223*0Sstevel@tonic-gate * point at which we insert the new node.
224*0Sstevel@tonic-gate */
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate while (weight(x) >= len) {
227*0Sstevel@tonic-gate if (p < x->block)
228*0Sstevel@tonic-gate tree = &x->left;
229*0Sstevel@tonic-gate else
230*0Sstevel@tonic-gate tree = &x->right;
231*0Sstevel@tonic-gate x = *tree;
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate * Perform root insertion. The variable x traces a path through
236*0Sstevel@tonic-gate * the tree, and with the help of left_hook and right_hook,
237*0Sstevel@tonic-gate * rewrites all links that cross the territory occupied
238*0Sstevel@tonic-gate * by p. Note that this improves performance under
239*0Sstevel@tonic-gate * paging.
240*0Sstevel@tonic-gate */
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate newhdr = getfreehdr();
243*0Sstevel@tonic-gate *tree = newhdr;
244*0Sstevel@tonic-gate left_hook = &newhdr->left;
245*0Sstevel@tonic-gate right_hook = &newhdr->right;
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate newhdr->left = NIL;
248*0Sstevel@tonic-gate newhdr->right = NIL;
249*0Sstevel@tonic-gate newhdr->block = p;
250*0Sstevel@tonic-gate newhdr->size = len;
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate while (x != NIL) {
253*0Sstevel@tonic-gate /*
254*0Sstevel@tonic-gate * Remark:
255*0Sstevel@tonic-gate * The name 'left_hook' is somewhat confusing, since
256*0Sstevel@tonic-gate * it is always set to the address of a .right link
257*0Sstevel@tonic-gate * field. However, its value is always an address
258*0Sstevel@tonic-gate * below (i.e., to the left of) p. Similarly
259*0Sstevel@tonic-gate * for right_hook. The values of left_hook and
260*0Sstevel@tonic-gate * right_hook converge toward the value of p,
261*0Sstevel@tonic-gate * as in a classical binary search.
262*0Sstevel@tonic-gate */
263*0Sstevel@tonic-gate if (x->block < p) {
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate * rewrite link crossing from the left
266*0Sstevel@tonic-gate */
267*0Sstevel@tonic-gate *left_hook = x;
268*0Sstevel@tonic-gate left_hook = &x->right;
269*0Sstevel@tonic-gate x = x->right;
270*0Sstevel@tonic-gate } else {
271*0Sstevel@tonic-gate /*
272*0Sstevel@tonic-gate * rewrite link crossing from the right
273*0Sstevel@tonic-gate */
274*0Sstevel@tonic-gate *right_hook = x;
275*0Sstevel@tonic-gate right_hook = &x->left;
276*0Sstevel@tonic-gate x = x->left;
277*0Sstevel@tonic-gate } /* else */
278*0Sstevel@tonic-gate } /* while */
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate *left_hook = *right_hook = NIL; /* clear remaining hooks */
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate } /* insert */
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate /*
286*0Sstevel@tonic-gate * Delete a node from a cartesian tree. p is the address of
287*0Sstevel@tonic-gate * a pointer to the node which is to be deleted.
288*0Sstevel@tonic-gate *
289*0Sstevel@tonic-gate * algorithm:
290*0Sstevel@tonic-gate * The left and right sons of the node to be deleted define two
291*0Sstevel@tonic-gate * subtrees which are to be merged and attached in place of the
292*0Sstevel@tonic-gate * deleted node. Each node on the inside edges of these two
293*0Sstevel@tonic-gate * subtrees is examined and longer nodes are placed above the
294*0Sstevel@tonic-gate * shorter ones.
295*0Sstevel@tonic-gate *
296*0Sstevel@tonic-gate * On entry:
297*0Sstevel@tonic-gate * *p is assumed to be non-null.
298*0Sstevel@tonic-gate */
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate void
delete(Freehdr * p)301*0Sstevel@tonic-gate delete(Freehdr *p)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate Freehdr x;
304*0Sstevel@tonic-gate Freehdr left_branch; /* left subtree of deleted node */
305*0Sstevel@tonic-gate Freehdr right_branch; /* right subtree of deleted node */
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate x = *p;
308*0Sstevel@tonic-gate left_branch = x->left;
309*0Sstevel@tonic-gate right_branch = x->right;
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate while (left_branch != right_branch) {
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate * iterate until left branch and right branch are
314*0Sstevel@tonic-gate * both NIL.
315*0Sstevel@tonic-gate */
316*0Sstevel@tonic-gate if (weight(left_branch) >= weight(right_branch)) {
317*0Sstevel@tonic-gate /*
318*0Sstevel@tonic-gate * promote the left branch
319*0Sstevel@tonic-gate */
320*0Sstevel@tonic-gate *p = left_branch;
321*0Sstevel@tonic-gate p = &left_branch->right;
322*0Sstevel@tonic-gate left_branch = left_branch->right;
323*0Sstevel@tonic-gate } else {
324*0Sstevel@tonic-gate /*
325*0Sstevel@tonic-gate * promote the right branch
326*0Sstevel@tonic-gate */
327*0Sstevel@tonic-gate *p = right_branch;
328*0Sstevel@tonic-gate p = &right_branch->left;
329*0Sstevel@tonic-gate right_branch = right_branch->left;
330*0Sstevel@tonic-gate } /* else */
331*0Sstevel@tonic-gate } /* while */
332*0Sstevel@tonic-gate *p = NIL;
333*0Sstevel@tonic-gate freehdr(x);
334*0Sstevel@tonic-gate } /* delete */
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate /*
338*0Sstevel@tonic-gate * Demote a node in a cartesian tree, if necessary, to establish
339*0Sstevel@tonic-gate * the required vertical ordering.
340*0Sstevel@tonic-gate *
341*0Sstevel@tonic-gate * algorithm:
342*0Sstevel@tonic-gate * The left and right subtrees of the node to be demoted are to
343*0Sstevel@tonic-gate * be partially merged and attached in place of the demoted node.
344*0Sstevel@tonic-gate * The nodes on the inside edges of these two subtrees are
345*0Sstevel@tonic-gate * examined and the longer nodes are placed above the shorter
346*0Sstevel@tonic-gate * ones, until a node is reached which has a length no greater
347*0Sstevel@tonic-gate * than that of the node being demoted (or until a null pointer
348*0Sstevel@tonic-gate * is reached). The node is then attached at this point, and
349*0Sstevel@tonic-gate * the remaining subtrees (if any) become its descendants.
350*0Sstevel@tonic-gate *
351*0Sstevel@tonic-gate * on entry:
352*0Sstevel@tonic-gate * a. All the nodes in the tree, including the one to be demoted,
353*0Sstevel@tonic-gate * must be correctly ordered horizontally;
354*0Sstevel@tonic-gate * b. All the nodes except the one to be demoted must also be
355*0Sstevel@tonic-gate * correctly positioned vertically. The node to be demoted
356*0Sstevel@tonic-gate * may be already correctly positioned vertically, or it may
357*0Sstevel@tonic-gate * have a length which is less than that of one or both of
358*0Sstevel@tonic-gate * its progeny.
359*0Sstevel@tonic-gate * c. *p is non-null
360*0Sstevel@tonic-gate */
361*0Sstevel@tonic-gate
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate static void
demote(Freehdr * p)364*0Sstevel@tonic-gate demote(Freehdr *p)
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate Freehdr x; /* addr of node to be demoted */
367*0Sstevel@tonic-gate Freehdr left_branch;
368*0Sstevel@tonic-gate Freehdr right_branch;
369*0Sstevel@tonic-gate size_t wx;
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate x = *p;
372*0Sstevel@tonic-gate left_branch = x->left;
373*0Sstevel@tonic-gate right_branch = x->right;
374*0Sstevel@tonic-gate wx = weight(x);
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate while (weight(left_branch) > wx || weight(right_branch) > wx) {
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate * select a descendant branch for promotion
379*0Sstevel@tonic-gate */
380*0Sstevel@tonic-gate if (weight(left_branch) >= weight(right_branch)) {
381*0Sstevel@tonic-gate /*
382*0Sstevel@tonic-gate * promote the left branch
383*0Sstevel@tonic-gate */
384*0Sstevel@tonic-gate *p = left_branch;
385*0Sstevel@tonic-gate p = &left_branch->right;
386*0Sstevel@tonic-gate left_branch = *p;
387*0Sstevel@tonic-gate } else {
388*0Sstevel@tonic-gate /*
389*0Sstevel@tonic-gate * promote the right branch
390*0Sstevel@tonic-gate */
391*0Sstevel@tonic-gate *p = right_branch;
392*0Sstevel@tonic-gate p = &right_branch->left;
393*0Sstevel@tonic-gate right_branch = *p;
394*0Sstevel@tonic-gate } /* else */
395*0Sstevel@tonic-gate } /* while */
396*0Sstevel@tonic-gate
397*0Sstevel@tonic-gate *p = x; /* attach demoted node here */
398*0Sstevel@tonic-gate x->left = left_branch;
399*0Sstevel@tonic-gate x->right = right_branch;
400*0Sstevel@tonic-gate } /* demote */
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gate /*
403*0Sstevel@tonic-gate * Allocate a block of storage
404*0Sstevel@tonic-gate *
405*0Sstevel@tonic-gate * algorithm:
406*0Sstevel@tonic-gate * The freelist is searched by descending the tree from the root
407*0Sstevel@tonic-gate * so that at each decision point the "better fitting" child node
408*0Sstevel@tonic-gate * is chosen (i.e., the shorter one, if it is long enough, or
409*0Sstevel@tonic-gate * the longer one, otherwise). The descent stops when both
410*0Sstevel@tonic-gate * child nodes are too short.
411*0Sstevel@tonic-gate *
412*0Sstevel@tonic-gate * function result:
413*0Sstevel@tonic-gate * kmem_alloc returns a pointer to the allocated block; a null
414*0Sstevel@tonic-gate * pointer indicates storage could not be allocated.
415*0Sstevel@tonic-gate */
416*0Sstevel@tonic-gate /*
417*0Sstevel@tonic-gate * We need to return blocks that are on word boundaries so that callers
418*0Sstevel@tonic-gate * that are putting int's into the area will work. Since we allow
419*0Sstevel@tonic-gate * arbitrary free'ing, we need a weight function that considers
420*0Sstevel@tonic-gate * free blocks starting on an odd boundary special. Allocation is
421*0Sstevel@tonic-gate * aligned to 8 byte boundaries (ALIGN).
422*0Sstevel@tonic-gate */
423*0Sstevel@tonic-gate #define ALIGN 8 /* doubleword aligned .. */
424*0Sstevel@tonic-gate #define ALIGNMASK (ALIGN-1)
425*0Sstevel@tonic-gate #define ALIGNMORE(addr) (ALIGN - ((uintptr_t)(addr) & ALIGNMASK))
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate * If it is empty then weight == 0
429*0Sstevel@tonic-gate * If it is aligned then weight == size
430*0Sstevel@tonic-gate * If it is unaligned
431*0Sstevel@tonic-gate * if not enough room to align then weight == 0
432*0Sstevel@tonic-gate * else weight == aligned size
433*0Sstevel@tonic-gate */
434*0Sstevel@tonic-gate #define mweight(x) ((x) == NIL ? 0 : \
435*0Sstevel@tonic-gate ((((uintptr_t)(x)->block) & ALIGNMASK) == 0 ? (x)->size : \
436*0Sstevel@tonic-gate (((x)->size <= ALIGNMORE((x)->block)) ? 0 : \
437*0Sstevel@tonic-gate (x)->size - ALIGNMORE((x)->block))))
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gate /*ARGSUSED1*/
440*0Sstevel@tonic-gate void *
kmem_alloc(size_t nbytes,int kmflag)441*0Sstevel@tonic-gate kmem_alloc(size_t nbytes, int kmflag)
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate Freehdr a; /* ptr to node to be allocated */
444*0Sstevel@tonic-gate Freehdr *p; /* address of ptr to node */
445*0Sstevel@tonic-gate size_t left_weight;
446*0Sstevel@tonic-gate size_t right_weight;
447*0Sstevel@tonic-gate Freehdr left_son;
448*0Sstevel@tonic-gate Freehdr right_son;
449*0Sstevel@tonic-gate char *retblock; /* Address returned to the user */
450*0Sstevel@tonic-gate int s;
451*0Sstevel@tonic-gate #ifdef DEBUG
452*0Sstevel@tonic-gate printf("kmem_alloc(nbytes 0x%lx)\n", nbytes);
453*0Sstevel@tonic-gate #endif /* DEBUG */
454*0Sstevel@tonic-gate
455*0Sstevel@tonic-gate if (nbytes == 0) {
456*0Sstevel@tonic-gate return (NULL);
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate s = splnet();
459*0Sstevel@tonic-gate
460*0Sstevel@tonic-gate if (nbytes < SMALLEST_BLK) {
461*0Sstevel@tonic-gate printf("illegal kmem_alloc call for %lx bytes\n", nbytes);
462*0Sstevel@tonic-gate prom_panic("kmem_alloc");
463*0Sstevel@tonic-gate }
464*0Sstevel@tonic-gate check_need_to_free();
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gate /*
467*0Sstevel@tonic-gate * ensure that at least one block is big enough to satisfy
468*0Sstevel@tonic-gate * the request.
469*0Sstevel@tonic-gate */
470*0Sstevel@tonic-gate
471*0Sstevel@tonic-gate if (mweight(kmem_info.free_root) <= nbytes) {
472*0Sstevel@tonic-gate /*
473*0Sstevel@tonic-gate * the largest block is not enough.
474*0Sstevel@tonic-gate */
475*0Sstevel@tonic-gate if (!morecore(nbytes)) {
476*0Sstevel@tonic-gate printf("kmem_alloc failed, nbytes %lx\n", nbytes);
477*0Sstevel@tonic-gate prom_panic("kmem_alloc");
478*0Sstevel@tonic-gate }
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate * search down through the tree until a suitable block is
483*0Sstevel@tonic-gate * found. At each decision point, select the better
484*0Sstevel@tonic-gate * fitting node.
485*0Sstevel@tonic-gate */
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate p = (Freehdr *) &kmem_info.free_root;
488*0Sstevel@tonic-gate a = *p;
489*0Sstevel@tonic-gate left_son = a->left;
490*0Sstevel@tonic-gate right_son = a->right;
491*0Sstevel@tonic-gate left_weight = mweight(left_son);
492*0Sstevel@tonic-gate right_weight = mweight(right_son);
493*0Sstevel@tonic-gate
494*0Sstevel@tonic-gate while (left_weight >= nbytes || right_weight >= nbytes) {
495*0Sstevel@tonic-gate if (left_weight <= right_weight) {
496*0Sstevel@tonic-gate if (left_weight >= nbytes) {
497*0Sstevel@tonic-gate p = &a->left;
498*0Sstevel@tonic-gate a = left_son;
499*0Sstevel@tonic-gate } else {
500*0Sstevel@tonic-gate p = &a->right;
501*0Sstevel@tonic-gate a = right_son;
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate } else {
504*0Sstevel@tonic-gate if (right_weight >= nbytes) {
505*0Sstevel@tonic-gate p = &a->right;
506*0Sstevel@tonic-gate a = right_son;
507*0Sstevel@tonic-gate } else {
508*0Sstevel@tonic-gate p = &a->left;
509*0Sstevel@tonic-gate a = left_son;
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate left_son = a->left;
513*0Sstevel@tonic-gate right_son = a->right;
514*0Sstevel@tonic-gate left_weight = mweight(left_son);
515*0Sstevel@tonic-gate right_weight = mweight(right_son);
516*0Sstevel@tonic-gate } /* while */
517*0Sstevel@tonic-gate
518*0Sstevel@tonic-gate /*
519*0Sstevel@tonic-gate * allocate storage from the selected node.
520*0Sstevel@tonic-gate */
521*0Sstevel@tonic-gate
522*0Sstevel@tonic-gate if (a->size - nbytes < SMALLEST_BLK) {
523*0Sstevel@tonic-gate /*
524*0Sstevel@tonic-gate * not big enough to split; must leave at least
525*0Sstevel@tonic-gate * a dblk's worth of space.
526*0Sstevel@tonic-gate */
527*0Sstevel@tonic-gate retblock = a->block->data;
528*0Sstevel@tonic-gate delete(p);
529*0Sstevel@tonic-gate } else {
530*0Sstevel@tonic-gate
531*0Sstevel@tonic-gate /*
532*0Sstevel@tonic-gate * split the node, allocating nbytes from the top.
533*0Sstevel@tonic-gate * Remember we've already accounted for the
534*0Sstevel@tonic-gate * allocated node's header space.
535*0Sstevel@tonic-gate */
536*0Sstevel@tonic-gate Freehdr x;
537*0Sstevel@tonic-gate x = getfreehdr();
538*0Sstevel@tonic-gate if ((uintptr_t)a->block->data & ALIGNMASK) {
539*0Sstevel@tonic-gate size_t size;
540*0Sstevel@tonic-gate if (a->size <= ALIGNMORE(a->block->data))
541*0Sstevel@tonic-gate prom_panic("kmem_alloc: short block allocated");
542*0Sstevel@tonic-gate size = nbytes + ALIGNMORE(a->block->data);
543*0Sstevel@tonic-gate x->block = a->block;
544*0Sstevel@tonic-gate x->size = ALIGNMORE(a->block->data);
545*0Sstevel@tonic-gate x->left = a->left;
546*0Sstevel@tonic-gate x->right = a->right;
547*0Sstevel@tonic-gate /*
548*0Sstevel@tonic-gate * the node pointed to by *p has become smaller;
549*0Sstevel@tonic-gate * move it down to its appropriate place in
550*0Sstevel@tonic-gate * the tree.
551*0Sstevel@tonic-gate */
552*0Sstevel@tonic-gate *p = x;
553*0Sstevel@tonic-gate demote(p);
554*0Sstevel@tonic-gate retblock = a->block->data + ALIGNMORE(a->block->data);
555*0Sstevel@tonic-gate if (a->size > size) {
556*0Sstevel@tonic-gate kmem_free((caddr_t)nextblk(a->block, size),
557*0Sstevel@tonic-gate (size_t)(a->size - size));
558*0Sstevel@tonic-gate }
559*0Sstevel@tonic-gate freehdr(a);
560*0Sstevel@tonic-gate } else {
561*0Sstevel@tonic-gate x->block = nextblk(a->block, nbytes);
562*0Sstevel@tonic-gate x->size = a->size - nbytes;
563*0Sstevel@tonic-gate x->left = a->left;
564*0Sstevel@tonic-gate x->right = a->right;
565*0Sstevel@tonic-gate /*
566*0Sstevel@tonic-gate * the node pointed to by *p has become smaller;
567*0Sstevel@tonic-gate * move it down to its appropriate place in
568*0Sstevel@tonic-gate * the tree.
569*0Sstevel@tonic-gate */
570*0Sstevel@tonic-gate *p = x;
571*0Sstevel@tonic-gate demote(p);
572*0Sstevel@tonic-gate retblock = a->block->data;
573*0Sstevel@tonic-gate freehdr(a);
574*0Sstevel@tonic-gate }
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate #ifdef DEBUG
577*0Sstevel@tonic-gate prtree(kmem_info.free_root, "kmem_alloc");
578*0Sstevel@tonic-gate #endif
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gate splx(s);
581*0Sstevel@tonic-gate bzero(retblock, nbytes);
582*0Sstevel@tonic-gate #ifdef DEBUG
583*0Sstevel@tonic-gate printf("kmem_alloc bzero complete - returning %p\n", retblock);
584*0Sstevel@tonic-gate #endif
585*0Sstevel@tonic-gate return (retblock);
586*0Sstevel@tonic-gate
587*0Sstevel@tonic-gate } /* kmem_alloc */
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate /*
590*0Sstevel@tonic-gate * Return a block to the free space tree.
591*0Sstevel@tonic-gate *
592*0Sstevel@tonic-gate * algorithm:
593*0Sstevel@tonic-gate * Starting at the root, search for and coalesce free blocks
594*0Sstevel@tonic-gate * adjacent to one given. When the appropriate place in the
595*0Sstevel@tonic-gate * tree is found, insert the given block.
596*0Sstevel@tonic-gate *
597*0Sstevel@tonic-gate * Do some sanity checks to avoid total confusion in the tree.
598*0Sstevel@tonic-gate * If the block has already been freed, prom_panic.
599*0Sstevel@tonic-gate * If the ptr is not from the arena, prom_panic.
600*0Sstevel@tonic-gate */
601*0Sstevel@tonic-gate void
kmem_free(void * ptr,size_t nbytes)602*0Sstevel@tonic-gate kmem_free(void *ptr, size_t nbytes)
603*0Sstevel@tonic-gate {
604*0Sstevel@tonic-gate Freehdr *np; /* For deletion from free list */
605*0Sstevel@tonic-gate Freehdr neighbor; /* Node to be coalesced */
606*0Sstevel@tonic-gate char *neigh_block; /* Ptr to potential neighbor */
607*0Sstevel@tonic-gate size_t neigh_size; /* Size of potential neighbor */
608*0Sstevel@tonic-gate int s;
609*0Sstevel@tonic-gate
610*0Sstevel@tonic-gate #ifdef DEBUG
611*0Sstevel@tonic-gate printf("kmem_free (ptr %p nbytes %lx)\n", ptr, nbytes);
612*0Sstevel@tonic-gate prtree(kmem_info.free_root, "kmem_free");
613*0Sstevel@tonic-gate #endif
614*0Sstevel@tonic-gate
615*0Sstevel@tonic-gate #ifdef lint
616*0Sstevel@tonic-gate neigh_block = bkmem_zalloc(nbytes);
617*0Sstevel@tonic-gate neigh_block = neigh_block;
618*0Sstevel@tonic-gate #endif
619*0Sstevel@tonic-gate if (nbytes == 0) {
620*0Sstevel@tonic-gate return;
621*0Sstevel@tonic-gate }
622*0Sstevel@tonic-gate
623*0Sstevel@tonic-gate if (ptr == 0) {
624*0Sstevel@tonic-gate prom_panic("kmem_free of 0");
625*0Sstevel@tonic-gate }
626*0Sstevel@tonic-gate s = splnet();
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate /*
629*0Sstevel@tonic-gate * Search the tree for the correct insertion point for this
630*0Sstevel@tonic-gate * node, coalescing adjacent free blocks along the way.
631*0Sstevel@tonic-gate */
632*0Sstevel@tonic-gate np = &kmem_info.free_root;
633*0Sstevel@tonic-gate neighbor = *np;
634*0Sstevel@tonic-gate while (neighbor != NIL) {
635*0Sstevel@tonic-gate neigh_block = (char *)neighbor->block;
636*0Sstevel@tonic-gate neigh_size = neighbor->size;
637*0Sstevel@tonic-gate if ((char *)ptr < neigh_block) {
638*0Sstevel@tonic-gate if ((char *)ptr + nbytes == neigh_block) {
639*0Sstevel@tonic-gate /*
640*0Sstevel@tonic-gate * Absorb and delete right neighbor
641*0Sstevel@tonic-gate */
642*0Sstevel@tonic-gate nbytes += neigh_size;
643*0Sstevel@tonic-gate delete(np);
644*0Sstevel@tonic-gate } else if ((char *)ptr + nbytes > neigh_block) {
645*0Sstevel@tonic-gate /*
646*0Sstevel@tonic-gate * The block being freed overlaps
647*0Sstevel@tonic-gate * another block in the tree. This
648*0Sstevel@tonic-gate * is bad news.
649*0Sstevel@tonic-gate */
650*0Sstevel@tonic-gate printf("kmem_free: free block overlap %p+%lx"
651*0Sstevel@tonic-gate " over %p\n", (void *)ptr, nbytes,
652*0Sstevel@tonic-gate (void *)neigh_block);
653*0Sstevel@tonic-gate prom_panic("kmem_free: free block overlap");
654*0Sstevel@tonic-gate } else {
655*0Sstevel@tonic-gate /*
656*0Sstevel@tonic-gate * Search to the left
657*0Sstevel@tonic-gate */
658*0Sstevel@tonic-gate np = &neighbor->left;
659*0Sstevel@tonic-gate }
660*0Sstevel@tonic-gate } else if ((char *)ptr > neigh_block) {
661*0Sstevel@tonic-gate if (neigh_block + neigh_size == ptr) {
662*0Sstevel@tonic-gate /*
663*0Sstevel@tonic-gate * Absorb and delete left neighbor
664*0Sstevel@tonic-gate */
665*0Sstevel@tonic-gate ptr = neigh_block;
666*0Sstevel@tonic-gate nbytes += neigh_size;
667*0Sstevel@tonic-gate delete(np);
668*0Sstevel@tonic-gate } else if (neigh_block + neigh_size > (char *)ptr) {
669*0Sstevel@tonic-gate /*
670*0Sstevel@tonic-gate * This block has already been freed
671*0Sstevel@tonic-gate */
672*0Sstevel@tonic-gate prom_panic("kmem_free block already free");
673*0Sstevel@tonic-gate } else {
674*0Sstevel@tonic-gate /*
675*0Sstevel@tonic-gate * search to the right
676*0Sstevel@tonic-gate */
677*0Sstevel@tonic-gate np = &neighbor->right;
678*0Sstevel@tonic-gate }
679*0Sstevel@tonic-gate } else {
680*0Sstevel@tonic-gate /*
681*0Sstevel@tonic-gate * This block has already been freed
682*0Sstevel@tonic-gate * as "ptr == neigh_block"
683*0Sstevel@tonic-gate */
684*0Sstevel@tonic-gate prom_panic("kmem_free: block already free as neighbor");
685*0Sstevel@tonic-gate return;
686*0Sstevel@tonic-gate } /* else */
687*0Sstevel@tonic-gate neighbor = *np;
688*0Sstevel@tonic-gate } /* while */
689*0Sstevel@tonic-gate
690*0Sstevel@tonic-gate /*
691*0Sstevel@tonic-gate * Insert the new node into the free space tree
692*0Sstevel@tonic-gate */
693*0Sstevel@tonic-gate insert((Dblk) ptr, nbytes, &kmem_info.free_root);
694*0Sstevel@tonic-gate #ifdef DEBUG
695*0Sstevel@tonic-gate printf("exiting kmem_free\n");
696*0Sstevel@tonic-gate prtree(kmem_info.free_root, "kmem_free");
697*0Sstevel@tonic-gate #endif
698*0Sstevel@tonic-gate splx(s);
699*0Sstevel@tonic-gate } /* kmem_free */
700*0Sstevel@tonic-gate
701*0Sstevel@tonic-gate /*
702*0Sstevel@tonic-gate * Sigh. We include a header file which the kernel
703*0Sstevel@tonic-gate * uses to declare (one of its many) kmem_free prototypes.
704*0Sstevel@tonic-gate * In order not to use the kernel's namespace, then, we must
705*0Sstevel@tonic-gate * define another name here for use by boot.
706*0Sstevel@tonic-gate */
707*0Sstevel@tonic-gate void *
bkmem_alloc(size_t size)708*0Sstevel@tonic-gate bkmem_alloc(size_t size)
709*0Sstevel@tonic-gate {
710*0Sstevel@tonic-gate return (kmem_alloc(size, 0));
711*0Sstevel@tonic-gate }
712*0Sstevel@tonic-gate
713*0Sstevel@tonic-gate /*
714*0Sstevel@tonic-gate * Boot's kmem_alloc is really kmem_zalloc().
715*0Sstevel@tonic-gate */
716*0Sstevel@tonic-gate void *
bkmem_zalloc(size_t size)717*0Sstevel@tonic-gate bkmem_zalloc(size_t size)
718*0Sstevel@tonic-gate {
719*0Sstevel@tonic-gate return (kmem_alloc(size, 0));
720*0Sstevel@tonic-gate }
721*0Sstevel@tonic-gate
722*0Sstevel@tonic-gate void
bkmem_free(void * p,size_t bytes)723*0Sstevel@tonic-gate bkmem_free(void *p, size_t bytes)
724*0Sstevel@tonic-gate {
725*0Sstevel@tonic-gate kmem_free(p, bytes);
726*0Sstevel@tonic-gate }
727*0Sstevel@tonic-gate
728*0Sstevel@tonic-gate static void
check_need_to_free(void)729*0Sstevel@tonic-gate check_need_to_free(void)
730*0Sstevel@tonic-gate {
731*0Sstevel@tonic-gate int i;
732*0Sstevel@tonic-gate struct need_to_free *ntf;
733*0Sstevel@tonic-gate caddr_t addr;
734*0Sstevel@tonic-gate size_t nbytes;
735*0Sstevel@tonic-gate int s;
736*0Sstevel@tonic-gate
737*0Sstevel@tonic-gate again:
738*0Sstevel@tonic-gate s = splimp();
739*0Sstevel@tonic-gate ntf = &kmem_info.need_to_free_list;
740*0Sstevel@tonic-gate if (ntf->addr) {
741*0Sstevel@tonic-gate addr = ntf->addr;
742*0Sstevel@tonic-gate nbytes = ntf->nbytes;
743*0Sstevel@tonic-gate *ntf = *(struct need_to_free *)ntf->addr;
744*0Sstevel@tonic-gate splx(s);
745*0Sstevel@tonic-gate kmem_free(addr, nbytes);
746*0Sstevel@tonic-gate goto again;
747*0Sstevel@tonic-gate }
748*0Sstevel@tonic-gate ntf = kmem_info.need_to_free;
749*0Sstevel@tonic-gate for (i = 0; i < NEED_TO_FREE_SIZE; i++) {
750*0Sstevel@tonic-gate if (ntf[i].addr) {
751*0Sstevel@tonic-gate addr = ntf[i].addr;
752*0Sstevel@tonic-gate nbytes = ntf[i].nbytes;
753*0Sstevel@tonic-gate ntf[i].addr = 0;
754*0Sstevel@tonic-gate splx(s);
755*0Sstevel@tonic-gate kmem_free(addr, nbytes);
756*0Sstevel@tonic-gate goto again;
757*0Sstevel@tonic-gate }
758*0Sstevel@tonic-gate }
759*0Sstevel@tonic-gate splx(s);
760*0Sstevel@tonic-gate }
761*0Sstevel@tonic-gate
762*0Sstevel@tonic-gate /*
763*0Sstevel@tonic-gate * Add a block of at least nbytes to the free space tree.
764*0Sstevel@tonic-gate *
765*0Sstevel@tonic-gate * return value:
766*0Sstevel@tonic-gate * true if at least nbytes can be allocated
767*0Sstevel@tonic-gate * false otherwise
768*0Sstevel@tonic-gate *
769*0Sstevel@tonic-gate * remark:
770*0Sstevel@tonic-gate * free space (delimited by the static variable ubound) is
771*0Sstevel@tonic-gate * extended by an amount determined by rounding nbytes up to
772*0Sstevel@tonic-gate * a multiple of the system page size.
773*0Sstevel@tonic-gate */
774*0Sstevel@tonic-gate
775*0Sstevel@tonic-gate static bool
morecore(size_t nbytes)776*0Sstevel@tonic-gate morecore(size_t nbytes)
777*0Sstevel@tonic-gate {
778*0Sstevel@tonic-gate #ifdef __sparc
779*0Sstevel@tonic-gate enum RESOURCES type = RES_BOOTSCRATCH_NOFAIL;
780*0Sstevel@tonic-gate #else
781*0Sstevel@tonic-gate enum RESOURCES type = RES_BOOTSCRATCH;
782*0Sstevel@tonic-gate #endif
783*0Sstevel@tonic-gate Dblk p;
784*0Sstevel@tonic-gate #ifdef DEBUG
785*0Sstevel@tonic-gate printf("morecore(nbytes 0x%lx)\n", nbytes);
786*0Sstevel@tonic-gate #endif /* DEBUG */
787*0Sstevel@tonic-gate
788*0Sstevel@tonic-gate
789*0Sstevel@tonic-gate nbytes = roundup(nbytes, PAGESIZE);
790*0Sstevel@tonic-gate p = (Dblk) resalloc(type, nbytes, (caddr_t)0, 0);
791*0Sstevel@tonic-gate if (p == 0) {
792*0Sstevel@tonic-gate return (false);
793*0Sstevel@tonic-gate }
794*0Sstevel@tonic-gate kmem_free((caddr_t)p, nbytes);
795*0Sstevel@tonic-gate #ifdef DEBUG
796*0Sstevel@tonic-gate printf("morecore() returing, p = %p\n", p);
797*0Sstevel@tonic-gate #endif
798*0Sstevel@tonic-gate return (true);
799*0Sstevel@tonic-gate
800*0Sstevel@tonic-gate } /* morecore */
801*0Sstevel@tonic-gate
802*0Sstevel@tonic-gate /*
803*0Sstevel@tonic-gate * Get a free block header
804*0Sstevel@tonic-gate * There is a list of available free block headers.
805*0Sstevel@tonic-gate * When the list is empty, allocate another pagefull.
806*0Sstevel@tonic-gate */
807*0Sstevel@tonic-gate Freehdr
getfreehdr(void)808*0Sstevel@tonic-gate getfreehdr(void)
809*0Sstevel@tonic-gate {
810*0Sstevel@tonic-gate Freehdr r;
811*0Sstevel@tonic-gate int n = 0;
812*0Sstevel@tonic-gate #ifdef DEBUG
813*0Sstevel@tonic-gate printf("getfreehdr()\n");
814*0Sstevel@tonic-gate #endif /* DEBUG */
815*0Sstevel@tonic-gate
816*0Sstevel@tonic-gate if (kmem_info.free_hdr_list != NIL) {
817*0Sstevel@tonic-gate r = kmem_info.free_hdr_list;
818*0Sstevel@tonic-gate kmem_info.free_hdr_list = kmem_info.free_hdr_list->left;
819*0Sstevel@tonic-gate } else {
820*0Sstevel@tonic-gate r = (Freehdr)resalloc(RES_BOOTSCRATCH, PAGESIZE, (caddr_t)0, 0);
821*0Sstevel@tonic-gate if (r == 0) {
822*0Sstevel@tonic-gate prom_panic("getfreehdr");
823*0Sstevel@tonic-gate }
824*0Sstevel@tonic-gate for (n = 1; n < PAGESIZE / sizeof (*r); n++) {
825*0Sstevel@tonic-gate freehdr(&r[n]);
826*0Sstevel@tonic-gate }
827*0Sstevel@tonic-gate }
828*0Sstevel@tonic-gate #ifdef DEBUG
829*0Sstevel@tonic-gate printf("getfreehdr: freed %x headers\n", n);
830*0Sstevel@tonic-gate printf("getfreehdr: returning %p\n", r);
831*0Sstevel@tonic-gate #endif /* DEBUG */
832*0Sstevel@tonic-gate return (r);
833*0Sstevel@tonic-gate }
834*0Sstevel@tonic-gate
835*0Sstevel@tonic-gate /*
836*0Sstevel@tonic-gate * Free a free block header
837*0Sstevel@tonic-gate * Add it to the list of available headers.
838*0Sstevel@tonic-gate */
839*0Sstevel@tonic-gate
840*0Sstevel@tonic-gate void
freehdr(Freehdr p)841*0Sstevel@tonic-gate freehdr(Freehdr p)
842*0Sstevel@tonic-gate {
843*0Sstevel@tonic-gate #ifdef DEBUG
844*0Sstevel@tonic-gate printf("freehdr(%p)\n", p);
845*0Sstevel@tonic-gate #endif /* DEBUG */
846*0Sstevel@tonic-gate p->left = kmem_info.free_hdr_list;
847*0Sstevel@tonic-gate p->right = NIL;
848*0Sstevel@tonic-gate p->block = NULL;
849*0Sstevel@tonic-gate kmem_info.free_hdr_list = p;
850*0Sstevel@tonic-gate }
851*0Sstevel@tonic-gate
852*0Sstevel@tonic-gate #ifdef DEBUG
853*0Sstevel@tonic-gate /*
854*0Sstevel@tonic-gate * Diagnostic routines
855*0Sstevel@tonic-gate */
856*0Sstevel@tonic-gate static int depth = 0;
857*0Sstevel@tonic-gate
858*0Sstevel@tonic-gate static void
prtree(Freehdr p,char * cp)859*0Sstevel@tonic-gate prtree(Freehdr p, char *cp)
860*0Sstevel@tonic-gate {
861*0Sstevel@tonic-gate int n;
862*0Sstevel@tonic-gate if (depth == 0) {
863*0Sstevel@tonic-gate printf("prtree(p %p cp %s)\n", p, cp);
864*0Sstevel@tonic-gate }
865*0Sstevel@tonic-gate if (p != NIL) {
866*0Sstevel@tonic-gate depth++;
867*0Sstevel@tonic-gate prtree(p->left, (char *)NULL);
868*0Sstevel@tonic-gate depth--;
869*0Sstevel@tonic-gate
870*0Sstevel@tonic-gate for (n = 0; n < depth; n++) {
871*0Sstevel@tonic-gate printf(" ");
872*0Sstevel@tonic-gate }
873*0Sstevel@tonic-gate printf(
874*0Sstevel@tonic-gate "(%p): (left = %p, right = %p, block = %p, size = %lx)\n",
875*0Sstevel@tonic-gate p, p->left, p->right, p->block, p->size);
876*0Sstevel@tonic-gate
877*0Sstevel@tonic-gate depth++;
878*0Sstevel@tonic-gate prtree(p->right, (char *)NULL);
879*0Sstevel@tonic-gate depth--;
880*0Sstevel@tonic-gate }
881*0Sstevel@tonic-gate }
882*0Sstevel@tonic-gate #endif /* DEBUG */
883