xref: /titanic_41/usr/src/lib/watchmalloc/common/mallint.h (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1996-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
32 
33 #ifndef	_REENTRANT
34 #define	_REENTRANT
35 #endif
36 
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <memory.h>
43 #include <thread.h>
44 #include <synch.h>
45 #include <malloc.h>
46 #include <procfs.h>
47 #include <limits.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 /* debugging macros */
52 #ifdef	DEBUG
53 #define	ASSERT(p)	((void) ((p) || (abort(), 0)))
54 #define	COUNT(n)	((void) n++)
55 static int		nmalloc, nrealloc, nfree;
56 #else
57 #define	ASSERT(p)	((void)0)
58 #define	COUNT(n)	((void)0)
59 #endif /* DEBUG */
60 
61 /* for conveniences */
62 #ifndef NULL
63 #define	NULL		(0)
64 #endif
65 
66 #define	WORDSIZE	(sizeof (WORD))
67 #define	MINSIZE		(sizeof (TREE) - sizeof (WORD))
68 #define	ROUND(s)	if ((s)%WORDSIZE) (s) += (WORDSIZE - ((s)%WORDSIZE))
69 
70 /*
71  * All of our allocations will be aligned on the least multiple of 4,
72  * at least, so the two low order bits are guaranteed to be available.
73  */
74 #ifdef _LP64
75 #define	ALIGN		16
76 #else
77 #define	ALIGN		8
78 #endif
79 
80 /* the proto-word; size must be ALIGN bytes */
81 typedef union _w_ {
82 	size_t		w_i;		/* an unsigned int */
83 	struct _t_	*w_p[2];	/* two pointers */
84 } WORD;
85 
86 /* structure of a node in the free tree */
87 typedef struct _t_ {
88 	WORD	t_s;	/* size of this element */
89 	WORD	t_p;	/* parent node */
90 	WORD	t_l;	/* left child */
91 	WORD	t_r;	/* right child */
92 	WORD	t_n;	/* next in link list */
93 	WORD	t_d;	/* dummy to reserve space for self-pointer */
94 } TREE;
95 
96 /* usable # of bytes in the block */
97 #define	SIZE(b)		(((b)->t_s).w_i)
98 #define	RSIZE(b)	(((b)->t_s).w_i & ~BITS01)
99 
100 /* free tree pointers */
101 #define	PARENT(b)	(((b)->t_p).w_p[0])
102 #define	LEFT(b)		(((b)->t_l).w_p[0])
103 #define	RIGHT(b)	(((b)->t_r).w_p[0])
104 
105 /* forward link in lists of small blocks */
106 #define	AFTER(b)	(((b)->t_p).w_p[0])
107 
108 /* forward and backward links for lists in the tree */
109 #define	LINKFOR(b)	(((b)->t_n).w_p[0])
110 #define	LINKBAK(b)	(((b)->t_p).w_p[0])
111 
112 /* set/test indicator if a block is in the tree or in a list */
113 #define	SETNOTREE(b)	(LEFT(b) = (TREE *)(-1))
114 #define	ISNOTREE(b)	(LEFT(b) == (TREE *)(-1))
115 
116 /* functions to get information on a block */
117 #define	DATA(b)		(((char *)(b)) + WORDSIZE)
118 #define	BLOCK(d)	((TREE *)(((char *)(d)) - WORDSIZE))
119 #define	SELFP(b)	(&(NEXT(b)->t_s.w_p[1]))
120 #define	LAST(b)		((b)->t_s.w_p[1])
121 #define	NEXT(b)		((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE))
122 #define	BOTTOM(b)	((DATA(b) + RSIZE(b) + WORDSIZE) == Baddr)
123 
124 /* functions to set and test the lowest two bits of a word */
125 #define	BIT0		(01)		/* ...001 */
126 #define	BIT1		(02)		/* ...010 */
127 #define	BITS01		(03)		/* ...011 */
128 #define	ISBIT0(w)	((w) & BIT0)	/* Is busy? */
129 #define	ISBIT1(w)	((w) & BIT1)	/* Is the preceding free? */
130 #define	SETBIT0(w)	((w) |= BIT0)	/* Block is busy */
131 #define	SETBIT1(w)	((w) |= BIT1)	/* The preceding is free */
132 #define	CLRBIT0(w)	((w) &= ~BIT0)	/* Clean bit0 */
133 #define	CLRBIT1(w)	((w) &= ~BIT1)	/* Clean bit1 */
134 #define	SETBITS01(w)	((w) |= BITS01)	/* Set bits 0 & 1 */
135 #define	CLRBITS01(w)	((w) &= ~BITS01) /* Clean bits 0 & 1 */
136 #define	SETOLD01(n, o)	((n) |= (BITS01 & (o)))
137 
138 /* system call to get more memory */
139 #define	GETCORE		sbrk
140 #define	ERRCORE		((char *)(-1))
141 #define	CORESIZE	(1024*ALIGN)
142 #define	MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */
143 #define	MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */
144 #define	MAX_ALIGN	(1 + (size_t)SSIZE_MAX)
145 
146 /* where are these *really* declared? */
147 extern	int	_mutex_lock(mutex_t *mp);
148 extern	int	_mutex_unlock(mutex_t *mp);
149