summaryrefslogtreecommitdiff
path: root/informationals/teso-i0037/mallint.h
blob: 2e501f07ad13cd0c5cfce71dc4718ae1ada44b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

/*
 * Copyright (c) 1996-1997 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma	ident	"@(#)mallint.h	1.11	97/12/02 SMI"	/* SVr4.0 1.2	*/

#include <sys/isa_defs.h>
#include <stdlib.h>
#include <memory.h>
#include <thread.h>
#include <synch.h>
#if 0
#include <mtlib.h>
#endif

/* debugging macros */
#ifdef	DEBUG
#define	ASSERT(p)	((void) ((p) || (abort(), 0)))
#define	COUNT(n)	((void) n++)
static int		nmalloc, nrealloc, nfree;
#else
#define	ASSERT(p)	((void)0)
#define	COUNT(n)	((void)0)
#endif /* DEBUG */

/* function to copy data from one area to another */
#define	MEMCOPY(to, fr, n)	((void) memcpy(to, fr, n))

/* for conveniences */
#ifndef NULL
#define	NULL		(0)
#endif

#define	reg		register
#define	WORDSIZE	(sizeof (WORD))
#define	MINSIZE		(sizeof (TREE) - sizeof (WORD))
#define	ROUND(s)	if (s % WORDSIZE) s += (WORDSIZE - (s % WORDSIZE))

#ifdef	DEBUG32
/*
 * The following definitions ease debugging
 * on a machine in which sizeof(pointer) == sizeof(int) == 4.
 * These definitions are not portable.
 *
 * Alignment (ALIGN) changed to 8 for SPARC ldd/std.
*/
#define	ALIGN	8
typedef int	WORD;
typedef struct _t_ {
	size_t		t_s;
	struct _t_	*t_p;
	struct _t_	*t_l;
	struct _t_	*t_r;
	struct _t_	*t_n;
	struct _t_	*t_d;
} TREE;
#define	SIZE(b)		((b)->t_s)
#define	AFTER(b)	((b)->t_p)
#define	PARENT(b)	((b)->t_p)
#define	LEFT(b)		((b)->t_l)
#define	RIGHT(b)	((b)->t_r)
#define	LINKFOR(b)	((b)->t_n)
#define	LINKBAK(b)	((b)->t_p)

#else	/* !DEBUG32 */
/*
 * All of our allocations will be aligned on the least multiple of 4,
 * at least, so the two low order bits are guaranteed to be available.
 */
#ifdef _LP64
#define	ALIGN		16
#else
#define	ALIGN		8
#endif

/* the proto-word; size must be ALIGN bytes */
typedef union _w_ {
	size_t		w_i;		/* an unsigned int */
	struct _t_	*w_p;		/* a pointer */
	char		w_a[ALIGN];	/* to force size */
} WORD;

/* structure of a node in the free tree */
typedef struct _t_ {
	WORD	t_s;	/* size of this element */
	WORD	t_p;	/* parent node */
	WORD	t_l;	/* left child */
	WORD	t_r;	/* right child */
	WORD	t_n;	/* next in link list */
	WORD	t_d;	/* dummy to reserve space for self-pointer */
} TREE;

/* usable # of bytes in the block */
#define	SIZE(b)		(((b)->t_s).w_i)

/* free tree pointers */
#define	PARENT(b)	(((b)->t_p).w_p)
#define	LEFT(b)		(((b)->t_l).w_p)
#define	RIGHT(b)	(((b)->t_r).w_p)

/* forward link in lists of small blocks */
#define	AFTER(b)	(((b)->t_p).w_p)

/* forward and backward links for lists in the tree */
#define	LINKFOR(b)	(((b)->t_n).w_p)
#define	LINKBAK(b)	(((b)->t_p).w_p)

#endif	/* DEBUG32 */

/* set/test indicator if a block is in the tree or in a list */
#define	SETNOTREE(b)	(LEFT(b) = (TREE *)(-1))
#define	ISNOTREE(b)	(LEFT(b) == (TREE *)(-1))

/* functions to get information on a block */
#define	DATA(b)		(((char *)(b)) + WORDSIZE)
#define	BLOCK(d)	((TREE *)(((char *)(d)) - WORDSIZE))
#define	SELFP(b)	((TREE **)(((char *)(b)) + SIZE(b)))
#define	LAST(b)		(*((TREE **)(((char *)(b)) - WORDSIZE)))
#define	NEXT(b)		((TREE *)(((char *)(b)) + SIZE(b) + WORDSIZE))
#define	BOTTOM(b)	((DATA(b) + SIZE(b) + WORDSIZE) == Baddr)

/* functions to set and test the lowest two bits of a word */
#define	BIT0		(01)		/* ...001 */
#define	BIT1		(02)		/* ...010 */
#define	BITS01		(03)		/* ...011 */
#define	ISBIT0(w)	((w) & BIT0)	/* Is busy? */
#define	ISBIT1(w)	((w) & BIT1)	/* Is the preceding free? */
#define	SETBIT0(w)	((w) |= BIT0)	/* Block is busy */
#define	SETBIT1(w)	((w) |= BIT1)	/* The preceding is free */
#define	CLRBIT0(w)	((w) &= ~BIT0)	/* Clean bit0 */
#define	CLRBIT1(w)	((w) &= ~BIT1)	/* Clean bit1 */
#define	SETBITS01(w)	((w) |= BITS01)	/* Set bits 0 & 1 */
#define	CLRBITS01(w)	((w) &= ~BITS01) /* Clean bits 0 & 1 */
#define	SETOLD01(n, o)	((n) |= (BITS01 & (o)))

/* system call to get more core */
#define	GETCORE		sbrk
#define	ERRCORE		((void *)(-1))
#define	CORESIZE	(1024*ALIGN)

extern void	*GETCORE(size_t);
extern void	_free_unlocked(void *);

#ifdef _REENTRANT
extern mutex_t __malloc_lock;
#endif /* _REENTRANT */