xref: /titanic_50/usr/src/lib/watchmalloc/common/mallint.h (revision 080575042aba2197b425ebfd52061dea061a9aa1)
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 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
31 
32 #ifndef	_REENTRANT
33 #define	_REENTRANT
34 #endif
35 
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <memory.h>
42 #include <thread.h>
43 #include <synch.h>
44 #include <procfs.h>
45 #include <limits.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 
49 /* debugging macros */
50 #ifdef	DEBUG
51 #define	ASSERT(p)	((void) ((p) || (abort(), 0)))
52 #define	COUNT(n)	((void) n++)
53 static int		nmalloc, nrealloc, nfree;
54 #else
55 #define	ASSERT(p)	((void)0)
56 #define	COUNT(n)	((void)0)
57 #endif /* DEBUG */
58 
59 /* for conveniences */
60 #ifndef NULL
61 #define	NULL		(0)
62 #endif
63 
64 #define	WORDSIZE	(sizeof (WORD))
65 #define	MINSIZE		(sizeof (TREE) - sizeof (WORD))
66 #define	ROUND(s)	if ((s)%WORDSIZE) (s) += (WORDSIZE - ((s)%WORDSIZE))
67 
68 /*
69  * All of our allocations will be aligned on the least multiple of 4,
70  * at least, so the two low order bits are guaranteed to be available.
71  */
72 #ifdef _LP64
73 #define	ALIGN		16
74 #else
75 #define	ALIGN		8
76 #endif
77 
78 /* the proto-word; size must be ALIGN bytes */
79 typedef union _w_ {
80 	size_t		w_i;		/* an unsigned int */
81 	struct _t_	*w_p[2];	/* two pointers */
82 } WORD;
83 
84 /* structure of a node in the free tree */
85 typedef struct _t_ {
86 	WORD	t_s;	/* size of this element */
87 	WORD	t_p;	/* parent node */
88 	WORD	t_l;	/* left child */
89 	WORD	t_r;	/* right child */
90 	WORD	t_n;	/* next in link list */
91 	WORD	t_d;	/* dummy to reserve space for self-pointer */
92 } TREE;
93 
94 /* usable # of bytes in the block */
95 #define	SIZE(b)		(((b)->t_s).w_i)
96 #define	RSIZE(b)	(((b)->t_s).w_i & ~BITS01)
97 
98 /* free tree pointers */
99 #define	PARENT(b)	(((b)->t_p).w_p[0])
100 #define	LEFT(b)		(((b)->t_l).w_p[0])
101 #define	RIGHT(b)	(((b)->t_r).w_p[0])
102 
103 /* forward link in lists of small blocks */
104 #define	AFTER(b)	(((b)->t_p).w_p[0])
105 
106 /* forward and backward links for lists in the tree */
107 #define	LINKFOR(b)	(((b)->t_n).w_p[0])
108 #define	LINKBAK(b)	(((b)->t_p).w_p[0])
109 
110 /* set/test indicator if a block is in the tree or in a list */
111 #define	SETNOTREE(b)	(LEFT(b) = (TREE *)(-1))
112 #define	ISNOTREE(b)	(LEFT(b) == (TREE *)(-1))
113 
114 /* functions to get information on a block */
115 #define	DATA(b)		(((char *)(b)) + WORDSIZE)
116 #define	BLOCK(d)	((TREE *)(((char *)(d)) - WORDSIZE))
117 #define	SELFP(b)	(&(NEXT(b)->t_s.w_p[1]))
118 #define	LAST(b)		((b)->t_s.w_p[1])
119 #define	NEXT(b)		((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE))
120 #define	BOTTOM(b)	((DATA(b) + RSIZE(b) + WORDSIZE) == Baddr)
121 
122 /* functions to set and test the lowest two bits of a word */
123 #define	BIT0		(01)		/* ...001 */
124 #define	BIT1		(02)		/* ...010 */
125 #define	BITS01		(03)		/* ...011 */
126 #define	ISBIT0(w)	((w) & BIT0)	/* Is busy? */
127 #define	ISBIT1(w)	((w) & BIT1)	/* Is the preceding free? */
128 #define	SETBIT0(w)	((w) |= BIT0)	/* Block is busy */
129 #define	SETBIT1(w)	((w) |= BIT1)	/* The preceding is free */
130 #define	CLRBIT0(w)	((w) &= ~BIT0)	/* Clean bit0 */
131 #define	CLRBIT1(w)	((w) &= ~BIT1)	/* Clean bit1 */
132 #define	SETBITS01(w)	((w) |= BITS01)	/* Set bits 0 & 1 */
133 #define	CLRBITS01(w)	((w) &= ~BITS01) /* Clean bits 0 & 1 */
134 #define	SETOLD01(n, o)	((n) |= (BITS01 & (o)))
135 
136 /* system call to get more memory */
137 #define	GETCORE		sbrk
138 #define	ERRCORE		((char *)(-1))
139 #define	CORESIZE	(1024*ALIGN)
140 #define	MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */
141 #define	MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */
142 #define	MAX_ALIGN	(1 + (size_t)SSIZE_MAX)
143 
144 /* where are these *really* declared? */
145 extern	int	_mutex_lock(mutex_t *mp);
146 extern	int	_mutex_unlock(mutex_t *mp);
147