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