xref: /titanic_51/usr/src/lib/libc/port/gen/mallint.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #include <sys/isa_defs.h>
34 #include <stdlib.h>
35 #include <memory.h>
36 #include <thread.h>
37 #include <synch.h>
38 #include <mtlib.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <limits.h>
43 
44 /* debugging macros */
45 #ifdef	DEBUG
46 #define	ASSERT(p)	((void) ((p) || (abort(), 0)))
47 #define	COUNT(n)	((void) n++)
48 static int		nmalloc, nrealloc, nfree;
49 #else
50 #define	ASSERT(p)	((void)0)
51 #define	COUNT(n)	((void)0)
52 #endif /* DEBUG */
53 
54 /* function to copy data from one area to another */
55 #define	MEMCOPY(to, fr, n)	((void) memcpy(to, fr, n))
56 
57 /* for conveniences */
58 #ifndef NULL
59 #define	NULL		(0)
60 #endif
61 
62 #define	WORDSIZE	(sizeof (WORD))
63 #define	MINSIZE		(sizeof (TREE) - sizeof (WORD))
64 #define	ROUND(s)	if (s % WORDSIZE) s += (WORDSIZE - (s % WORDSIZE))
65 
66 #ifdef	DEBUG32
67 /*
68  * The following definitions ease debugging
69  * on a machine in which sizeof(pointer) == sizeof(int) == 4.
70  * These definitions are not portable.
71  *
72  * Alignment (ALIGN) changed to 8 for SPARC ldd/std.
73  */
74 #define	ALIGN	8
75 typedef int	WORD;
76 typedef struct _t_ {
77 	size_t		t_s;
78 	struct _t_	*t_p;
79 	struct _t_	*t_l;
80 	struct _t_	*t_r;
81 	struct _t_	*t_n;
82 	struct _t_	*t_d;
83 } TREE;
84 #define	SIZE(b)		((b)->t_s)
85 #define	AFTER(b)	((b)->t_p)
86 #define	PARENT(b)	((b)->t_p)
87 #define	LEFT(b)		((b)->t_l)
88 #define	RIGHT(b)	((b)->t_r)
89 #define	LINKFOR(b)	((b)->t_n)
90 #define	LINKBAK(b)	((b)->t_p)
91 
92 #else	/* !DEBUG32 */
93 /*
94  * All of our allocations will be aligned on the least multiple of 4,
95  * at least, so the two low order bits are guaranteed to be available.
96  */
97 #ifdef _LP64
98 #define	ALIGN		16
99 #else
100 #define	ALIGN		8
101 #endif
102 
103 /* the proto-word; size must be ALIGN bytes */
104 typedef union _w_ {
105 	size_t		w_i;		/* an unsigned int */
106 	struct _t_	*w_p;		/* a pointer */
107 	char		w_a[ALIGN];	/* to force size */
108 } WORD;
109 
110 /* structure of a node in the free tree */
111 typedef struct _t_ {
112 	WORD	t_s;	/* size of this element */
113 	WORD	t_p;	/* parent node */
114 	WORD	t_l;	/* left child */
115 	WORD	t_r;	/* right child */
116 	WORD	t_n;	/* next in link list */
117 	WORD	t_d;	/* dummy to reserve space for self-pointer */
118 } TREE;
119 
120 /* usable # of bytes in the block */
121 #define	SIZE(b)		(((b)->t_s).w_i)
122 
123 /* free tree pointers */
124 #define	PARENT(b)	(((b)->t_p).w_p)
125 #define	LEFT(b)		(((b)->t_l).w_p)
126 #define	RIGHT(b)	(((b)->t_r).w_p)
127 
128 /* forward link in lists of small blocks */
129 #define	AFTER(b)	(((b)->t_p).w_p)
130 
131 /* forward and backward links for lists in the tree */
132 #define	LINKFOR(b)	(((b)->t_n).w_p)
133 #define	LINKBAK(b)	(((b)->t_p).w_p)
134 
135 #endif	/* DEBUG32 */
136 
137 /* set/test indicator if a block is in the tree or in a list */
138 #define	SETNOTREE(b)	(LEFT(b) = (TREE *)(-1))
139 #define	ISNOTREE(b)	(LEFT(b) == (TREE *)(-1))
140 
141 /* functions to get information on a block */
142 #define	DATA(b)		((char *)(((uintptr_t)(b)) + WORDSIZE))
143 #define	BLOCK(d)	((TREE *)(((uintptr_t)(d)) - WORDSIZE))
144 #define	SELFP(b)	((TREE **)(((uintptr_t)(b)) + SIZE(b)))
145 #define	LAST(b)		(*((TREE **)(((uintptr_t)(b)) - WORDSIZE)))
146 #define	NEXT(b)		((TREE *)(((uintptr_t)(b)) + SIZE(b) + WORDSIZE))
147 #define	BOTTOM(b)	((DATA(b) + SIZE(b) + WORDSIZE) == Baddr)
148 
149 /* functions to set and test the lowest two bits of a word */
150 #define	BIT0		(01)		/* ...001 */
151 #define	BIT1		(02)		/* ...010 */
152 #define	BITS01		(03)		/* ...011 */
153 #define	ISBIT0(w)	((w) & BIT0)	/* Is busy? */
154 #define	ISBIT1(w)	((w) & BIT1)	/* Is the preceding free? */
155 #define	SETBIT0(w)	((w) |= BIT0)	/* Block is busy */
156 #define	SETBIT1(w)	((w) |= BIT1)	/* The preceding is free */
157 #define	CLRBIT0(w)	((w) &= ~BIT0)	/* Clean bit0 */
158 #define	CLRBIT1(w)	((w) &= ~BIT1)	/* Clean bit1 */
159 #define	SETBITS01(w)	((w) |= BITS01)	/* Set bits 0 & 1 */
160 #define	CLRBITS01(w)	((w) &= ~BITS01) /* Clean bits 0 & 1 */
161 #define	SETOLD01(n, o)	((n) |= (BITS01 & (o)))
162 
163 /* system call to get more core */
164 #define	GETCORE		sbrk
165 #define	ERRCORE		((void *)(-1))
166 #define	CORESIZE	(1024*ALIGN)
167 #define	MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1))	/* round down ALIGN */
168 #define	MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */
169 #define	MAX_ALIGN	(1 + (size_t)SSIZE_MAX)
170 
171 extern void	*GETCORE(ssize_t);
172 extern void	_free_unlocked(void *);
173 
174 extern mutex_t libc_malloc_lock;
175