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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
41 #include <sys/types.h>
44 /* debugging macros */
46 #define ASSERT(p) ((void) ((p) || (abort(), 0)))
47 #define COUNT(n) ((void) n++)
48 static int nmalloc
, nrealloc
, nfree
;
50 #define ASSERT(p) ((void)0)
51 #define COUNT(n) ((void)0)
54 #define WORDSIZE (sizeof (WORD))
55 #define MINSIZE (sizeof (TREE) - sizeof (WORD))
56 #define ROUND(s) if ((s)%WORDSIZE) (s) += (WORDSIZE - ((s)%WORDSIZE))
59 * All of our allocations will be aligned on the least multiple of 4,
60 * at least, so the two low order bits are guaranteed to be available.
68 /* the proto-word; size must be ALIGN bytes */
70 size_t w_i
; /* an unsigned int */
71 struct _t_
*w_p
[2]; /* two pointers */
74 /* structure of a node in the free tree */
76 WORD t_s
; /* size of this element */
77 WORD t_p
; /* parent node */
78 WORD t_l
; /* left child */
79 WORD t_r
; /* right child */
80 WORD t_n
; /* next in link list */
81 WORD t_d
; /* dummy to reserve space for self-pointer */
84 /* usable # of bytes in the block */
85 #define SIZE(b) (((b)->t_s).w_i)
86 #define RSIZE(b) (((b)->t_s).w_i & ~BITS01)
88 /* free tree pointers */
89 #define PARENT(b) (((b)->t_p).w_p[0])
90 #define LEFT(b) (((b)->t_l).w_p[0])
91 #define RIGHT(b) (((b)->t_r).w_p[0])
93 /* forward link in lists of small blocks */
94 #define AFTER(b) (((b)->t_p).w_p[0])
96 /* forward and backward links for lists in the tree */
97 #define LINKFOR(b) (((b)->t_n).w_p[0])
98 #define LINKBAK(b) (((b)->t_p).w_p[0])
100 /* set/test indicator if a block is in the tree or in a list */
101 #define SETNOTREE(b) (LEFT(b) = (TREE *)(-1))
102 #define ISNOTREE(b) (LEFT(b) == (TREE *)(-1))
104 /* functions to get information on a block */
105 #define DATA(b) (((char *)(b)) + WORDSIZE)
106 #define BLOCK(d) ((TREE *)(((char *)(d)) - WORDSIZE))
107 #define SELFP(b) (&(NEXT(b)->t_s.w_p[1]))
108 #define LAST(b) ((b)->t_s.w_p[1])
109 #define NEXT(b) ((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE))
110 #define BOTTOM(b) ((DATA(b) + RSIZE(b) + WORDSIZE) == Baddr)
112 /* functions to set and test the lowest two bits of a word */
113 #define BIT0 (01) /* ...001 */
114 #define BIT1 (02) /* ...010 */
115 #define BITS01 (03) /* ...011 */
116 #define ISBIT0(w) ((w) & BIT0) /* Is busy? */
117 #define ISBIT1(w) ((w) & BIT1) /* Is the preceding free? */
118 #define SETBIT0(w) ((w) |= BIT0) /* Block is busy */
119 #define SETBIT1(w) ((w) |= BIT1) /* The preceding is free */
120 #define CLRBIT0(w) ((w) &= ~BIT0) /* Clean bit0 */
121 #define CLRBIT1(w) ((w) &= ~BIT1) /* Clean bit1 */
122 #define SETBITS01(w) ((w) |= BITS01) /* Set bits 0 & 1 */
123 #define CLRBITS01(w) ((w) &= ~BITS01) /* Clean bits 0 & 1 */
124 #define SETOLD01(n, o) ((n) |= (BITS01 & (o)))
126 /* system call to get more memory */
128 #define ERRCORE ((char *)(-1))
129 #define CORESIZE (1024*ALIGN)
130 #define MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */
131 #define MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */
132 #define MAX_ALIGN (1 + (size_t)SSIZE_MAX)