Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * Hypervisor Software File: mallint.h | |
5 | * | |
6 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
7 | * | |
8 | * - Do no alter or remove copyright notices | |
9 | * | |
10 | * - Redistribution and use of this software in source and binary forms, with | |
11 | * or without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistribution of source code must retain the above copyright notice, | |
15 | * this list of conditions and the following disclaimer. | |
16 | * | |
17 | * - Redistribution in binary form must reproduce the above copyright notice, | |
18 | * this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * | |
21 | * Neither the name of Sun Microsystems, Inc. or the names of contributors | |
22 | * may be used to endorse or promote products derived from this software | |
23 | * without specific prior written permission. | |
24 | * | |
25 | * This software is provided "AS IS," without a warranty of any kind. | |
26 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
27 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
28 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
29 | * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
30 | * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
31 | * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
32 | * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
33 | * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
34 | * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
35 | * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
36 | * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
37 | * | |
38 | * You acknowledge that this software is not designed, licensed or | |
39 | * intended for use in the design, construction, operation or maintenance of | |
40 | * any nuclear facility. | |
41 | * | |
42 | * ========== Copyright Header End ============================================ | |
43 | */ | |
44 | /* | |
45 | * Copyright 2003 Sun Microsystems, Inc. All rights reserved. | |
46 | * Use is subject to license terms. | |
47 | */ | |
48 | ||
49 | #pragma ident "@(#)mallint.h 1.2 03/11/10 SMI" | |
50 | ||
51 | #include <sys/types.h> | |
52 | #ifdef _LP64 | |
53 | #define ALIGNSZ 16 | |
54 | #else | |
55 | #define ALIGNSZ 8 | |
56 | #endif | |
57 | ||
58 | /* | |
59 | * template for the header | |
60 | */ | |
61 | ||
62 | struct header { | |
63 | struct header *nextblk; | |
64 | struct header *nextfree; | |
65 | struct header *prevfree; | |
66 | struct header *__Pad; /* pad to a multiple of ALIGNSZ */ | |
67 | }; | |
68 | ||
69 | /* | |
70 | * template for a small block | |
71 | */ | |
72 | ||
73 | struct lblk { | |
74 | union { | |
75 | /* | |
76 | * the next free little block in this holding block. | |
77 | * This field is used when the block is free | |
78 | */ | |
79 | struct lblk *nextfree; | |
80 | /* | |
81 | * the holding block containing this little block. | |
82 | * This field is used when the block is allocated | |
83 | */ | |
84 | struct holdblk *holder; | |
85 | /* | |
86 | * Insure over head is multiple of ALIGNSZ | |
87 | * assumes ALIGNSZ >= sizeof pointer | |
88 | */ | |
89 | char __Overhead[ALIGNSZ]; | |
90 | } header; | |
91 | /* There is no telling how big this field really is. */ | |
92 | /* This must be on a ALIGNSZ boundary */ | |
93 | char byte; | |
94 | }; | |
95 | ||
96 | /* | |
97 | * template for holding block | |
98 | */ | |
99 | struct holdblk { | |
100 | struct holdblk *nexthblk; /* next holding block */ | |
101 | struct holdblk *prevhblk; /* previous holding block */ | |
102 | struct lblk *lfreeq; /* head of free queue within block */ | |
103 | struct lblk *unused; /* pointer to 1st little block never used */ | |
104 | long blksz; /* size of little blocks contained */ | |
105 | struct lblk *__Pad; /* pad to a multiple of ALIGNSZ */ | |
106 | char space[1]; /* start of space to allocate. */ | |
107 | /* This must be on a ALIGNSZ boundary */ | |
108 | }; | |
109 | ||
110 | /* | |
111 | * The following manipulate the free queue | |
112 | * | |
113 | * DELFREEQ will remove x from the free queue | |
114 | * ADDFREEQ will add an element to the head | |
115 | * of the free queue. | |
116 | * MOVEHEAD will move the free pointers so that | |
117 | * x is at the front of the queue | |
118 | */ | |
119 | #define ADDFREEQ(x) (x)->prevfree = &(freeptr[0]);\ | |
120 | (x)->nextfree = freeptr[0].nextfree;\ | |
121 | freeptr[0].nextfree->prevfree = (x);\ | |
122 | freeptr[0].nextfree = (x);\ | |
123 | assert((x)->nextfree != (x));\ | |
124 | assert((x)->prevfree != (x)); | |
125 | #define DELFREEQ(x) (x)->prevfree->nextfree = (x)->nextfree;\ | |
126 | (x)->nextfree->prevfree = (x)->prevfree;\ | |
127 | assert((x)->nextfree != (x));\ | |
128 | assert((x)->prevfree != (x)); | |
129 | #define MOVEHEAD(x) freeptr[1].prevfree->nextfree = freeptr[0].nextfree;\ | |
130 | freeptr[0].nextfree->prevfree = \ | |
131 | freeptr[1].prevfree;\ | |
132 | (x)->prevfree->nextfree = &(freeptr[1]);\ | |
133 | freeptr[1].prevfree = (x)->prevfree;\ | |
134 | (x)->prevfree = &(freeptr[0]);\ | |
135 | freeptr[0].nextfree = (x);\ | |
136 | assert((x)->nextfree != (x));\ | |
137 | assert((x)->prevfree != (x)); | |
138 | /* | |
139 | * The following manipulate the busy flag | |
140 | */ | |
141 | #define BUSY 1L | |
142 | #define SETBUSY(x) ((struct header *)((long)(x) | BUSY)) | |
143 | #define CLRBUSY(x) ((struct header *)((long)(x) & ~BUSY)) | |
144 | #define TESTBUSY(x) ((long)(x) & BUSY) | |
145 | /* | |
146 | * The following manipulate the small block flag | |
147 | */ | |
148 | #define SMAL 2L | |
149 | #define SETSMAL(x) ((struct lblk *)((long)(x) | SMAL)) | |
150 | #define CLRSMAL(x) ((struct lblk *)((long)(x) & ~SMAL)) | |
151 | #define TESTSMAL(x) ((long)(x) & SMAL) | |
152 | /* | |
153 | * The following manipulate both flags. They must be | |
154 | * type coerced | |
155 | */ | |
156 | #define SETALL(x) ((long)(x) | (SMAL | BUSY)) | |
157 | #define CLRALL(x) ((long)(x) & ~(SMAL | BUSY)) | |
158 | /* | |
159 | * Other useful constants | |
160 | */ | |
161 | #define TRUE 1 | |
162 | #define FALSE 0 | |
163 | #define HEADSZ sizeof (struct header) /* size of unallocated block header */ | |
164 | ||
165 | /* MINHEAD is the minimum size of an allocated block header */ | |
166 | #define MINHEAD ALIGNSZ | |
167 | ||
168 | /* min. block size must as big as HEADSZ */ | |
169 | #define MINBLKSZ HEADSZ | |
170 | ||
171 | /* memory is gotten from sbrk in multiples of BLOCKSZ */ | |
172 | #define BLOCKSZ 2048 /* ??? Too Small, ?? pagesize? */ | |
173 | ||
174 | #define GROUND (struct header *)0 | |
175 | #define LGROUND (struct lblk *)0 | |
176 | #define HGROUND (struct holdblk *)0 /* ground for the holding block queue */ | |
177 | #ifndef NULL | |
178 | #define NULL (char *)0 | |
179 | #endif | |
180 | /* | |
181 | * Structures and constants describing the holding blocks | |
182 | */ | |
183 | /* default number of small blocks per holding block */ | |
184 | #define NUMLBLKS 100 | |
185 | ||
186 | /* size of a holding block with small blocks of size blksz */ | |
187 | #define HOLDSZ(blksz) \ | |
188 | (sizeof (struct holdblk) - sizeof (struct lblk *) + blksz*numlblks) | |
189 | #define FASTCT 6 /* number of blocks that can be allocated quickly */ | |
190 | ||
191 | /* default maximum size block for fast allocation */ | |
192 | /* assumes initial value of grain == ALIGNSZ */ | |
193 | #define MAXFAST ALIGNSZ*FASTCT | |
194 | ||
195 | #ifdef debug | |
196 | #define CHECKQ checkq(); | |
197 | #else | |
198 | #define CHECKQ | |
199 | #endif | |
200 | ||
201 | #ifdef _REENTRANT | |
202 | ||
203 | #define mutex_lock(m) _mutex_lock(m) | |
204 | #define mutex_unlock(m) _mutex_unlock(m) | |
205 | ||
206 | #else | |
207 | ||
208 | #define mutex_lock(m) | |
209 | #define mutex_unlock(m) | |
210 | ||
211 | #endif _REENTRANT |