xref: /titanic_50/usr/src/lib/libbc/libc/gen/common/mallint.h (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1986 by Sun Microsystems, Inc.
26  */
27 
28 /*
29  * file: mallint.h
30  * description:
31  *
32  * Definitions for malloc.c and friends (realloc.c, memalign.c)
33  *
34  * The node header structure.  Header info never overlaps with user
35  * data space, in order to accommodate the following atrocity:
36  *		free(p);
37  *		realloc(p, newsize);
38  * ... which was historically used to obtain storage compaction as
39  * a side effect of the realloc() call, when the block referenced
40  * by p was coalesced with another free block by the call to free().
41  *
42  * To reduce storage consumption, a header block is associated with
43  * free blocks only, not allocated blocks.
44  * When a free block is allocated, its header block is put on
45  * a free header block list.
46  *
47  * This creates a header space and a free block space.
48  * The left pointer of a header blocks is used to chain free header
49  * blocks together.  New header blocks are allocated in chunks of
50  * NFREE_HDRS.
51  */
52 #include <malloc.h>
53 
54 typedef enum {false,true} bool;
55 typedef struct	freehdr	*Freehdr;
56 typedef struct	dblk	*Dblk;
57 typedef unsigned int	uint;
58 
59 /*
60  * Description of a header for a free block
61  * Only free blocks have such headers.
62  */
63 struct 	freehdr	{
64 	Freehdr	left;			/* Left tree pointer */
65 	Freehdr	right;			/* Right tree pointer */
66 	Dblk	block;			/* Ptr to the data block */
67 	uint	size;
68 };
69 
70 #define NIL		((Freehdr) 0)
71 #define	NFREE_HDRS	512		/* Get this many headers at a time */
72 #define	SMALLEST_BLK	sizeof(struct dblk) 	/* Size of smallest block */
73 #define NULL		0
74 
75 /*
76  * Description of a data block.
77  * A data block consists of a length word, possibly followed by
78  * a filler word for alignment, followed by the user's data.
79  * To back up from the user's data to the length word, use
80  * (address of data) - ALIGNSIZ;
81  */
82 
83 #ifdef sparc
84 #define ALIGNSIZ	sizeof(double)
85 struct	dblk	{
86 	uint	size;			/* Size of the block */
87 	uint	filler;			/* filler, for double alignment */
88 	char	data[ALIGNSIZ];		/* Addr returned to the caller */
89 };
90 #endif
91 
92 #ifdef mc68000
93 #define ALIGNSIZ	sizeof(uint)
94 struct	dblk	{
95 	uint	size;			/* Size of the block */
96 	char	data[ALIGNSIZ];		/* Addr returned to the caller */
97 };
98 #endif
99 
100 
101 /*
102  * weight(x) is the size of a block, in bytes; or 0 if and only if x
103  *	is a null pointer.  Note that malloc() and free() should be
104  *	prepared to deal with things like zero-length blocks, which
105  *	can be introduced by errant programs.
106  */
107 
108 #define	weight(x)	((x) == NIL? 0: (x->size))
109 #define	roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
110 #define	nextblk(p, size) ((Dblk) ((char *) (p) + (size)))
111 #define	max(a, b)	((a) < (b)? (b): (a))
112 #define	min(a, b)	((a) < (b)? (a): (b))
113 #define heapsize()	(_ubound - _lbound)
114 #define misaligned(p)	((unsigned)(p)&3)
115 
116 extern	Freehdr	_root;
117 extern	char	*_lbound, *_ubound;
118 extern	int	malloc_debug();
119 
120 extern	struct mallinfo __mallinfo;
121