import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / mallint.h
blobef5e5b277ad8c871c07abb67b2bbaa33be7e31af
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
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>
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 */
52 /* function to copy data from one area to another */
53 #define MEMCOPY(to, fr, n) ((void) memcpy(to, fr, n))
55 #define WORDSIZE (sizeof (WORD))
56 #define MINSIZE (sizeof (TREE) - sizeof (WORD))
57 #define ROUND(s) if (s % WORDSIZE) s += (WORDSIZE - (s % WORDSIZE))
59 #ifdef DEBUG32
61 * The following definitions ease debugging
62 * on a machine in which sizeof(pointer) == sizeof(int) == 4.
63 * These definitions are not portable.
65 * Alignment (ALIGN) changed to 8 for SPARC ldd/std.
67 #define ALIGN 8
68 typedef int WORD;
69 typedef struct _t_ {
70 size_t t_s;
71 struct _t_ *t_p;
72 struct _t_ *t_l;
73 struct _t_ *t_r;
74 struct _t_ *t_n;
75 struct _t_ *t_d;
76 } TREE;
77 #define SIZE(b) ((b)->t_s)
78 #define AFTER(b) ((b)->t_p)
79 #define PARENT(b) ((b)->t_p)
80 #define LEFT(b) ((b)->t_l)
81 #define RIGHT(b) ((b)->t_r)
82 #define LINKFOR(b) ((b)->t_n)
83 #define LINKBAK(b) ((b)->t_p)
85 #else /* !DEBUG32 */
87 * All of our allocations will be aligned on the least multiple of 4,
88 * at least, so the two low order bits are guaranteed to be available.
90 #ifdef _LP64
91 #define ALIGN 16
92 #else
93 #define ALIGN 8
94 #endif
96 /* the proto-word; size must be ALIGN bytes */
97 typedef union _w_ {
98 size_t w_i; /* an unsigned int */
99 struct _t_ *w_p; /* a pointer */
100 char w_a[ALIGN]; /* to force size */
101 } WORD;
103 /* structure of a node in the free tree */
104 typedef struct _t_ {
105 WORD t_s; /* size of this element */
106 WORD t_p; /* parent node */
107 WORD t_l; /* left child */
108 WORD t_r; /* right child */
109 WORD t_n; /* next in link list */
110 WORD t_d; /* dummy to reserve space for self-pointer */
111 } TREE;
113 /* usable # of bytes in the block */
114 #define SIZE(b) (((b)->t_s).w_i)
116 /* free tree pointers */
117 #define PARENT(b) (((b)->t_p).w_p)
118 #define LEFT(b) (((b)->t_l).w_p)
119 #define RIGHT(b) (((b)->t_r).w_p)
121 /* forward link in lists of small blocks */
122 #define AFTER(b) (((b)->t_p).w_p)
124 /* forward and backward links for lists in the tree */
125 #define LINKFOR(b) (((b)->t_n).w_p)
126 #define LINKBAK(b) (((b)->t_p).w_p)
128 #endif /* DEBUG32 */
130 /* set/test indicator if a block is in the tree or in a list */
131 #define SETNOTREE(b) (LEFT(b) = (TREE *)(-1))
132 #define ISNOTREE(b) (LEFT(b) == (TREE *)(-1))
134 /* functions to get information on a block */
135 #define DATA(b) ((char *)(((uintptr_t)(b)) + WORDSIZE))
136 #define BLOCK(d) ((TREE *)(((uintptr_t)(d)) - WORDSIZE))
137 #define SELFP(b) ((TREE **)(((uintptr_t)(b)) + SIZE(b)))
138 #define LAST(b) (*((TREE **)(((uintptr_t)(b)) - WORDSIZE)))
139 #define NEXT(b) ((TREE *)(((uintptr_t)(b)) + SIZE(b) + WORDSIZE))
140 #define BOTTOM(b) ((DATA(b) + SIZE(b) + WORDSIZE) == Baddr)
142 /* functions to set and test the lowest two bits of a word */
143 #define BIT0 (01) /* ...001 */
144 #define BIT1 (02) /* ...010 */
145 #define BITS01 (03) /* ...011 */
146 #define ISBIT0(w) ((w) & BIT0) /* Is busy? */
147 #define ISBIT1(w) ((w) & BIT1) /* Is the preceding free? */
148 #define SETBIT0(w) ((w) |= BIT0) /* Block is busy */
149 #define SETBIT1(w) ((w) |= BIT1) /* The preceding is free */
150 #define CLRBIT0(w) ((w) &= ~BIT0) /* Clean bit0 */
151 #define CLRBIT1(w) ((w) &= ~BIT1) /* Clean bit1 */
152 #define SETBITS01(w) ((w) |= BITS01) /* Set bits 0 & 1 */
153 #define CLRBITS01(w) ((w) &= ~BITS01) /* Clean bits 0 & 1 */
154 #define SETOLD01(n, o) ((n) |= (BITS01 & (o)))
156 /* system call to get more core */
157 #define GETCORE sbrk
158 #define ERRCORE ((void *)(-1))
159 #define CORESIZE (1024*ALIGN)
160 #define MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */
161 #define MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */
162 #define MAX_ALIGN (1 + (size_t)SSIZE_MAX)
164 extern void *GETCORE(ssize_t);
165 extern void _free_unlocked(void *);
167 extern mutex_t libc_malloc_lock;