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