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