xref: /illumos-gate/usr/src/lib/libmalloc/common/mallint.h (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #ifndef _MALLINT_H
31 #define	_MALLINT_H
32 
33 /*	From:	SVr4.0	libmalloc:mallint.h	1.3		*/
34 
35 /*
36  * number of bytes to align to  (must be at least 4, because lower 2 bits
37  * are used for flags
38  *
39  * header and code assume ALIGNSZ is exact multiple of sizeof (struct header *)
40  * several places assume sizeof (long) == sizeof (struct holdblk *)
41  */
42 
43 #include <sys/types.h>
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 #ifdef _LP64
50 #define	ALIGNSZ	16
51 #else
52 #define	ALIGNSZ	8
53 #endif
54 
55 /*
56  *	template for the header
57  */
58 
59 struct header {
60 	struct header *nextblk;
61 	struct header *nextfree;
62 	struct header *prevfree;
63 	struct header *__Pad;	/* pad to a multiple of ALIGNSZ */
64 };
65 
66 /*
67  *	template for a small block
68  */
69 
70 struct lblk  {
71 	union {
72 		/*
73 		 * the next free little block in this holding block.
74 		 * This field is used when the block is free
75 		 */
76 		struct lblk *nextfree;
77 		/*
78 		 * the holding block containing this little block.
79 		 * This field is used when the block is allocated
80 		 */
81 		struct holdblk *holder;
82 		/*
83 		 * Insure over head is multiple of ALIGNSZ
84 		 * assumes  ALIGNSZ >= sizeof pointer
85 		 */
86 		char __Overhead[ALIGNSZ];
87 	}  header;
88 	/* There is no telling how big this field really is.  */
89 	/* This must be on a ALIGNSZ  boundary */
90 	char byte;
91 };
92 
93 /*
94  *	template for holding block
95  */
96 struct holdblk {
97 	struct holdblk *nexthblk;   /* next holding block */
98 	struct holdblk *prevhblk;   /* previous holding block */
99 	struct lblk *lfreeq;	/* head of free queue within block */
100 	struct lblk *unused;	/* pointer to 1st little block never used */
101 	long blksz;		/* size of little blocks contained */
102 	struct lblk *__Pad;	/* pad to a multiple of ALIGNSZ */
103 	char space[1];		/* start of space to allocate. */
104 				/* This must be on a ALIGNSZ boundary */
105 };
106 
107 /*
108  *	 The following manipulate the free queue
109  *
110  *		DELFREEQ will remove x from the free queue
111  *		ADDFREEQ will add an element to the head
112  *			 of the free queue.
113  *		MOVEHEAD will move the free pointers so that
114  *			 x is at the front of the queue
115  */
116 #define	ADDFREEQ(x)	(x)->prevfree = &(freeptr[0]);\
117 				(x)->nextfree = freeptr[0].nextfree;\
118 				freeptr[0].nextfree->prevfree = (x);\
119 				freeptr[0].nextfree = (x);\
120 				assert((x)->nextfree != (x));\
121 				assert((x)->prevfree != (x));
122 #define	DELFREEQ(x)	(x)->prevfree->nextfree = (x)->nextfree;\
123 				(x)->nextfree->prevfree = (x)->prevfree;\
124 				assert((x)->nextfree != (x));\
125 				assert((x)->prevfree != (x));
126 #define	MOVEHEAD(x)	freeptr[1].prevfree->nextfree = freeptr[0].nextfree;\
127 				freeptr[0].nextfree->prevfree = \
128 				    freeptr[1].prevfree;\
129 				(x)->prevfree->nextfree = &(freeptr[1]);\
130 				freeptr[1].prevfree = (x)->prevfree;\
131 				(x)->prevfree = &(freeptr[0]);\
132 				freeptr[0].nextfree = (x);\
133 				assert((x)->nextfree != (x));\
134 				assert((x)->prevfree != (x));
135 /*
136  *	The following manipulate the busy flag
137  */
138 #define	BUSY	1L
139 #define	SETBUSY(x)	((struct header *)((long)(x) | BUSY))
140 #define	CLRBUSY(x)	((struct header *)((long)(x) & ~BUSY))
141 #define	TESTBUSY(x)	((long)(x) & BUSY)
142 /*
143  *	The following manipulate the small block flag
144  */
145 #define	SMAL	2L
146 #define	SETSMAL(x)	((struct lblk *)((long)(x) | SMAL))
147 #define	CLRSMAL(x)	((struct lblk *)((long)(x) & ~SMAL))
148 #define	TESTSMAL(x)	((long)(x) & SMAL)
149 /*
150  *	The following manipulate both flags.  They must be
151  *	type coerced
152  */
153 #define	SETALL(x)	((long)(x) | (SMAL | BUSY))
154 #define	CLRALL(x)	((long)(x) & ~(SMAL | BUSY))
155 /*
156  *	Other useful constants
157  */
158 #define	TRUE	1
159 #define	FALSE	0
160 #define	HEADSZ	sizeof (struct header)	/* size of unallocated block header */
161 
162 /* MINHEAD is the minimum size of an allocated block header */
163 #define	MINHEAD	ALIGNSZ
164 
165 /* min. block size must as big as HEADSZ */
166 #define	MINBLKSZ	HEADSZ
167 
168 /* memory is gotten from sbrk in multiples of BLOCKSZ */
169 #define	BLOCKSZ		2048	/* ??? Too Small, ?? pagesize? */
170 
171 #define	GROUND	(struct header *)0
172 #define	LGROUND	(struct lblk *)0
173 #define	HGROUND	(struct holdblk *)0	/* ground for the holding block queue */
174 #ifndef	NULL
175 #define	NULL	(char *)0
176 #endif
177 /*
178  *	Structures and constants describing the holding blocks
179  */
180 /* default number of small blocks per holding block */
181 #define	NUMLBLKS	100
182 
183 /* size of a holding block with small blocks of size blksz */
184 #define	HOLDSZ(blksz)	\
185 	    (sizeof (struct holdblk) - sizeof (struct lblk *) + blksz*numlblks)
186 #define	FASTCT	6	/* number of blocks that can be allocated quickly */
187 
188 /* default maximum size block for fast allocation */
189 /* assumes initial value of grain == ALIGNSZ */
190 #define	MAXFAST	ALIGNSZ*FASTCT
191 
192 #ifdef	debug
193 #define	CHECKQ	checkq();
194 #else
195 #define	CHECKQ
196 #endif
197 
198 #ifdef	__cplusplus
199 }
200 #endif
201 
202 #endif	/* _MALLINT_H */
203