summaryrefslogtreecommitdiff
path: root/usr/src/lib/libmalloc/common/mallint.h
blob: f2c0f2cfae038ebb5ee35cdb1e6f94c5f3f67d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

#ifndef _MALLINT_H
#define	_MALLINT_H

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*	From:	SVr4.0	libmalloc:mallint.h	1.3		*/

/*
 * number of bytes to align to  (must be at least 4, because lower 2 bits
 * are used for flags
 *
 * header and code assume ALIGNSZ is exact multiple of sizeof (struct header *)
 * several places assume sizeof (long) == sizeof (struct holdblk *)
 */

#include <sys/types.h>

#ifdef	__cplusplus
extern "C" {
#endif

#ifdef _LP64
#define	ALIGNSZ	16
#else
#define	ALIGNSZ	8
#endif

/*
 *	template for the header
 */

struct header {
	struct header *nextblk;
	struct header *nextfree;
	struct header *prevfree;
	struct header *__Pad;	/* pad to a multiple of ALIGNSZ */
};

/*
 *	template for a small block
 */

struct lblk  {
	union {
		/*
		 * the next free little block in this holding block.
		 * This field is used when the block is free
		 */
		struct lblk *nextfree;
		/*
		 * the holding block containing this little block.
		 * This field is used when the block is allocated
		 */
		struct holdblk *holder;
		/*
		 * Insure over head is multiple of ALIGNSZ
		 * assumes  ALIGNSZ >= sizeof pointer
		 */
		char __Overhead[ALIGNSZ];
	}  header;
	/* There is no telling how big this field really is.  */
	/* This must be on a ALIGNSZ  boundary */
	char byte;
};

/*
 *	template for holding block
 */
struct holdblk {
	struct holdblk *nexthblk;   /* next holding block */
	struct holdblk *prevhblk;   /* previous holding block */
	struct lblk *lfreeq;	/* head of free queue within block */
	struct lblk *unused;	/* pointer to 1st little block never used */
	long blksz;		/* size of little blocks contained */
	struct lblk *__Pad;	/* pad to a multiple of ALIGNSZ */
	char space[1];		/* start of space to allocate. */
				/* This must be on a ALIGNSZ boundary */
};

/*
 *	 The following manipulate the free queue
 *
 *		DELFREEQ will remove x from the free queue
 *		ADDFREEQ will add an element to the head
 *			 of the free queue.
 *		MOVEHEAD will move the free pointers so that
 *			 x is at the front of the queue
 */
#define	ADDFREEQ(x)	(x)->prevfree = &(freeptr[0]);\
				(x)->nextfree = freeptr[0].nextfree;\
				freeptr[0].nextfree->prevfree = (x);\
				freeptr[0].nextfree = (x);\
				assert((x)->nextfree != (x));\
				assert((x)->prevfree != (x));
#define	DELFREEQ(x)	(x)->prevfree->nextfree = (x)->nextfree;\
				(x)->nextfree->prevfree = (x)->prevfree;\
				assert((x)->nextfree != (x));\
				assert((x)->prevfree != (x));
#define	MOVEHEAD(x)	freeptr[1].prevfree->nextfree = freeptr[0].nextfree;\
				freeptr[0].nextfree->prevfree = \
				    freeptr[1].prevfree;\
				(x)->prevfree->nextfree = &(freeptr[1]);\
				freeptr[1].prevfree = (x)->prevfree;\
				(x)->prevfree = &(freeptr[0]);\
				freeptr[0].nextfree = (x);\
				assert((x)->nextfree != (x));\
				assert((x)->prevfree != (x));
/*
 *	The following manipulate the busy flag
 */
#define	BUSY	1L
#define	SETBUSY(x)	((struct header *)((long)(x) | BUSY))
#define	CLRBUSY(x)	((struct header *)((long)(x) & ~BUSY))
#define	TESTBUSY(x)	((long)(x) & BUSY)
/*
 *	The following manipulate the small block flag
 */
#define	SMAL	2L
#define	SETSMAL(x)	((struct lblk *)((long)(x) | SMAL))
#define	CLRSMAL(x)	((struct lblk *)((long)(x) & ~SMAL))
#define	TESTSMAL(x)	((long)(x) & SMAL)
/*
 *	The following manipulate both flags.  They must be
 *	type coerced
 */
#define	SETALL(x)	((long)(x) | (SMAL | BUSY))
#define	CLRALL(x)	((long)(x) & ~(SMAL | BUSY))
/*
 *	Other useful constants
 */
#define	TRUE	1
#define	FALSE	0
#define	HEADSZ	sizeof (struct header)	/* size of unallocated block header */

/* MINHEAD is the minimum size of an allocated block header */
#define	MINHEAD	ALIGNSZ

/* min. block size must as big as HEADSZ */
#define	MINBLKSZ	HEADSZ

/* memory is gotten from sbrk in multiples of BLOCKSZ */
#define	BLOCKSZ		2048	/* ??? Too Small, ?? pagesize? */

#define	GROUND	(struct header *)0
#define	LGROUND	(struct lblk *)0
#define	HGROUND	(struct holdblk *)0	/* ground for the holding block queue */
#ifndef	NULL
#define	NULL	(char *)0
#endif
/*
 *	Structures and constants describing the holding blocks
 */
/* default number of small blocks per holding block */
#define	NUMLBLKS	100

/* size of a holding block with small blocks of size blksz */
#define	HOLDSZ(blksz)	\
	    (sizeof (struct holdblk) - sizeof (struct lblk *) + blksz*numlblks)
#define	FASTCT	6	/* number of blocks that can be allocated quickly */

/* default maximum size block for fast allocation */
/* assumes initial value of grain == ALIGNSZ */
#define	MAXFAST	ALIGNSZ*FASTCT

#ifdef	debug
#define	CHECKQ	checkq();
#else
#define	CHECKQ
#endif

#ifdef	__cplusplus
}
#endif

#endif	/* _MALLINT_H */