xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/malloc.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*722Smuffin /*
23*722Smuffin  * Copyright 1986 Sun Microsystems, Inc.  All rights reserved.
24*722Smuffin  * Use is subject to license terms.
25*722Smuffin  */
260Sstevel@tonic-gate 
27*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * file: malloc.c
310Sstevel@tonic-gate  * description:
320Sstevel@tonic-gate  *	Yet another memory allocator, this one based on a method
330Sstevel@tonic-gate  *	described in C.J. Stephenson, "Fast Fits"
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *	The basic data structure is a "Cartesian" binary tree, in which
360Sstevel@tonic-gate  *	nodes are ordered by ascending addresses (thus minimizing free
370Sstevel@tonic-gate  *	list insertion time) and block sizes decrease with depth in the
380Sstevel@tonic-gate  *	tree (thus minimizing search time for a block of a given size).
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *	In other words: for any node s, let D(s) denote the set of
410Sstevel@tonic-gate  *	descendents of s; for all x in D(left(s)) and all y in
420Sstevel@tonic-gate  *	D(right(s)), we have:
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *	a. addr(x) <  addr(s) <  addr(y)
450Sstevel@tonic-gate  *	b. len(x)  <= len(s)  >= len(y)
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "mallint.h"
490Sstevel@tonic-gate #include <errno.h>
50*722Smuffin #include <stdlib.h>
51*722Smuffin #include <stdarg.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /* system interface */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate extern	char	*sbrk();
560Sstevel@tonic-gate extern	int	getpagesize();
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static	int	nbpg = 0;	/* set by calling getpagesize() */
59*722Smuffin static	bool	morecore(uint);	/* get more memory into free space */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifdef	S5EMUL
620Sstevel@tonic-gate #define	ptr_t		void *	/* ANSI C says these are voids */
630Sstevel@tonic-gate #define	free_t		void	/* ANSI says void free(ptr_t ptr) */
640Sstevel@tonic-gate #define	free_return(x)	return
650Sstevel@tonic-gate #else
660Sstevel@tonic-gate #define	ptr_t		char *	/* BSD still (4.3) wants char*'s */
670Sstevel@tonic-gate #define	free_t		int	/* BSD says int free(ptr_t ptr) */
680Sstevel@tonic-gate #define	free_return(x)	return(x)
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /* SystemV-compatible information structure */
720Sstevel@tonic-gate #define INIT_MXFAST 0
730Sstevel@tonic-gate #define INIT_NLBLKS 100
740Sstevel@tonic-gate #define INIT_GRAIN ALIGNSIZ
750Sstevel@tonic-gate 
760Sstevel@tonic-gate struct	mallinfo __mallinfo = {
770Sstevel@tonic-gate 	0,0,0,0,0,0,0,0,0,0,			/* basic info */
780Sstevel@tonic-gate 	INIT_MXFAST, INIT_NLBLKS, INIT_GRAIN,	/* mallopt options */
790Sstevel@tonic-gate 	0,0,0
800Sstevel@tonic-gate };
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /* heap data structures */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate Freehdr	_root	= NIL;			/* root of free space list */
850Sstevel@tonic-gate char	*_lbound = NULL;		/* lower bound of heap */
860Sstevel@tonic-gate char	*_ubound = NULL;		/* upper bound of heap */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /* free header list management */
890Sstevel@tonic-gate 
90*722Smuffin static	Freehdr	getfreehdr(void);
91*722Smuffin static	void	putfreehdr(Freehdr);
920Sstevel@tonic-gate static	Freehdr	freehdrptr = NIL;	/* ptr to block of available headers */
930Sstevel@tonic-gate static	int	nfreehdrs = 0;		/* # of headers in current block */
940Sstevel@tonic-gate static	Freehdr	freehdrlist = NIL;	/* List of available headers */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /* error checking */
97*722Smuffin static void	error(char *, ...);
98*722Smuffin /* sets errno; prints msg and aborts if DEBUG is on */
99*722Smuffin 
100*722Smuffin static int	reclaim(Dblk, uint, int);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #ifdef	DEBUG	/*	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
1030Sstevel@tonic-gate 
104*722Smuffin int	malloc_debug(int);
105*722Smuffin int	malloc_verify(void);
1060Sstevel@tonic-gate static	int debug_level = 1;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * A block with a negative size, a size that is not a multiple
1100Sstevel@tonic-gate  * of ALIGNSIZ, a size greater than the current extent of the
1110Sstevel@tonic-gate  * heap, or a size which extends beyond the end of the heap is
1120Sstevel@tonic-gate  * considered bad.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #define badblksize(p,size)\
1160Sstevel@tonic-gate ( (size) < SMALLEST_BLK \
1170Sstevel@tonic-gate 	|| (size) & (ALIGNSIZ-1) \
1180Sstevel@tonic-gate 	|| (size) > heapsize() \
1190Sstevel@tonic-gate 	|| ((char*)(p))+(size) > _ubound )
1200Sstevel@tonic-gate 
121*722Smuffin #else	/* !DEBUG	================================================= */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #define malloc_debug(level) 0
1240Sstevel@tonic-gate #define malloc_verify() 1
1250Sstevel@tonic-gate #define debug_level 0
1260Sstevel@tonic-gate #define badblksize(p,size) 0
1270Sstevel@tonic-gate 
128*722Smuffin #endif	/* !DEBUG	<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1290Sstevel@tonic-gate 
130*722Smuffin 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * insert (newblk, len)
1330Sstevel@tonic-gate  *	Inserts a new node in the free space tree, placing it
1340Sstevel@tonic-gate  *	in the correct position with respect to the existing nodes.
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * algorithm:
1370Sstevel@tonic-gate  *	Starting from the root, a binary search is made for the new
1380Sstevel@tonic-gate  *	node. If this search were allowed to continue, it would
1390Sstevel@tonic-gate  *	eventually fail (since there cannot already be a node at the
1400Sstevel@tonic-gate  *	given address); but in fact it stops when it reaches a node in
1410Sstevel@tonic-gate  *	the tree which has a length less than that of the new node (or
1420Sstevel@tonic-gate  *	when it reaches a null tree pointer).
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  *	The new node is then inserted at the root of the subtree for
1450Sstevel@tonic-gate  *	which the shorter node forms the old root (or in place of the
1460Sstevel@tonic-gate  *	null pointer).
147*722Smuffin  *
148*722Smuffin  * Arguments
149*722Smuffin  *	newblk:	Ptr to the block to insert
150*722Smuffin  *	len:	Length of new node
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate 
153*722Smuffin static void
insert(Dblk newblk,uint len)154*722Smuffin insert(Dblk newblk, uint len)
1550Sstevel@tonic-gate {
156*722Smuffin 	Freehdr *fpp;		/* Address of ptr to subtree */
157*722Smuffin 	Freehdr x;
158*722Smuffin 	Freehdr *left_hook;	/* Temp for insertion */
159*722Smuffin 	Freehdr *right_hook;	/* Temp for insertion */
160*722Smuffin 	Freehdr newhdr;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * check for bad block size.
1640Sstevel@tonic-gate 	 */
1650Sstevel@tonic-gate 	if ( badblksize(newblk,len) ) {
1660Sstevel@tonic-gate 		error("insert: bad block size (%d) at %#x\n", len, newblk);
1670Sstevel@tonic-gate 		return;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Search for the first node which has a weight less
1720Sstevel@tonic-gate 	 *	than that of the new node; this will be the
1730Sstevel@tonic-gate 	 *	point at which we insert the new node.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	fpp = &_root;
1760Sstevel@tonic-gate 	x = *fpp;
1770Sstevel@tonic-gate 	while (weight(x) >= len) {
1780Sstevel@tonic-gate 		if (newblk < x->block)
1790Sstevel@tonic-gate 			fpp = &x->left;
1800Sstevel@tonic-gate 		else
1810Sstevel@tonic-gate 			fpp = &x->right;
1820Sstevel@tonic-gate 		x = *fpp;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Perform root insertion. The variable x traces a path through
1870Sstevel@tonic-gate 	 *	the fpp, and with the help of left_hook and right_hook,
1880Sstevel@tonic-gate 	 *	rewrites all links that cross the territory occupied
1890Sstevel@tonic-gate 	 *	by newblk.
1900Sstevel@tonic-gate 	 */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if ((newhdr = getfreehdr()) == NIL) {
1930Sstevel@tonic-gate 		/* Error message returned by getfreehdr() */
1940Sstevel@tonic-gate 		return;
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	*fpp = newhdr;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	newhdr->left = NIL;
1990Sstevel@tonic-gate 	newhdr->right = NIL;
2000Sstevel@tonic-gate 	newhdr->block = newblk;
2010Sstevel@tonic-gate 	newhdr->size = len;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/*
2040Sstevel@tonic-gate 	 * set length word in the block for consistency with the header.
2050Sstevel@tonic-gate 	 */
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	newblk->size = len;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	left_hook = &newhdr->left;
2100Sstevel@tonic-gate 	right_hook = &newhdr->right;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	while (x != NIL) {
2130Sstevel@tonic-gate 		/*
2140Sstevel@tonic-gate 		 * Remark:
2150Sstevel@tonic-gate 		 *	The name 'left_hook' is somewhat confusing, since
2160Sstevel@tonic-gate 		 *	it is always set to the address of a .right link
2170Sstevel@tonic-gate 		 *	field.  However, its value is always an address
2180Sstevel@tonic-gate 		 *	below (i.e., to the left of) newblk. Similarly
2190Sstevel@tonic-gate 		 *	for right_hook. The values of left_hook and
2200Sstevel@tonic-gate 		 *	right_hook converge toward the value of newblk,
2210Sstevel@tonic-gate 		 *	as in a classical binary search.
2220Sstevel@tonic-gate 		 */
2230Sstevel@tonic-gate 		if (x->block < newblk) {
2240Sstevel@tonic-gate 			/*
2250Sstevel@tonic-gate 			 * rewrite link crossing from the left
2260Sstevel@tonic-gate 			 */
2270Sstevel@tonic-gate 			*left_hook = x;
2280Sstevel@tonic-gate 			left_hook = &x->right;
2290Sstevel@tonic-gate 			x = x->right;
2300Sstevel@tonic-gate 		} else {
2310Sstevel@tonic-gate 			/*
2320Sstevel@tonic-gate 			 * rewrite link crossing from the right
2330Sstevel@tonic-gate 			 */
2340Sstevel@tonic-gate 			*right_hook = x;
2350Sstevel@tonic-gate 			right_hook = &x->left;
2360Sstevel@tonic-gate 			x = x->left;
2370Sstevel@tonic-gate 		} /*else*/
2380Sstevel@tonic-gate 	} /*while*/
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	*left_hook = *right_hook = NIL;		/* clear remaining hooks */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate } /*insert*/
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * delete(p)
2460Sstevel@tonic-gate  *	deletes a node from a cartesian tree. p is the address of
2470Sstevel@tonic-gate  *	a pointer to the node which is to be deleted.
2480Sstevel@tonic-gate  *
2490Sstevel@tonic-gate  * algorithm:
2500Sstevel@tonic-gate  *	The left and right branches of the node to be deleted define two
2510Sstevel@tonic-gate  *	subtrees which are to be merged and attached in place of the
2520Sstevel@tonic-gate  *	deleted node.  Each node on the inside edges of these two
2530Sstevel@tonic-gate  *	subtrees is examined and longer nodes are placed above the
2540Sstevel@tonic-gate  *	shorter ones.
2550Sstevel@tonic-gate  *
2560Sstevel@tonic-gate  * On entry:
2570Sstevel@tonic-gate  *	*p is assumed to be non-null.
2580Sstevel@tonic-gate  */
259*722Smuffin static void
delete(Freehdr * p)260*722Smuffin delete(Freehdr *p)
2610Sstevel@tonic-gate {
262*722Smuffin 	Freehdr x;
263*722Smuffin 	Freehdr left_branch;	/* left subtree of deleted node */
264*722Smuffin 	Freehdr right_branch;	/* right subtree of deleted node */
265*722Smuffin 	uint left_weight;
266*722Smuffin 	uint right_weight;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	x = *p;
2690Sstevel@tonic-gate 	left_branch = x->left;
2700Sstevel@tonic-gate 	left_weight = weight(left_branch);
2710Sstevel@tonic-gate 	right_branch = x->right;
2720Sstevel@tonic-gate 	right_weight = weight(right_branch);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	while (left_branch != right_branch) {
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * iterate until left branch and right branch are
2770Sstevel@tonic-gate 		 * both NIL.
2780Sstevel@tonic-gate 		 */
2790Sstevel@tonic-gate 		if ( left_weight >= right_weight ) {
2800Sstevel@tonic-gate 			/*
2810Sstevel@tonic-gate 			 * promote the left branch
2820Sstevel@tonic-gate 			 */
2830Sstevel@tonic-gate 			if (left_branch != NIL) {
2840Sstevel@tonic-gate 				if (left_weight == 0) {
2850Sstevel@tonic-gate 					/* zero-length block */
2860Sstevel@tonic-gate 					error("blocksize=0 at %#x\n",
2870Sstevel@tonic-gate 						(int)left_branch->block->data);
2880Sstevel@tonic-gate 					break;
2890Sstevel@tonic-gate 				}
2900Sstevel@tonic-gate 				*p = left_branch;
2910Sstevel@tonic-gate 				p = &left_branch->right;
2920Sstevel@tonic-gate 				left_branch = *p;
2930Sstevel@tonic-gate 				left_weight = weight(left_branch);
2940Sstevel@tonic-gate 			}
2950Sstevel@tonic-gate 		} else {
2960Sstevel@tonic-gate 			/*
2970Sstevel@tonic-gate 			 * promote the right branch
2980Sstevel@tonic-gate 			 */
2990Sstevel@tonic-gate 			if (right_branch != NIL) {
3000Sstevel@tonic-gate 				if (right_weight == 0) {
3010Sstevel@tonic-gate 					/* zero-length block */
3020Sstevel@tonic-gate 					error("blocksize=0 at %#x\n",
3030Sstevel@tonic-gate 						(int)right_branch->block->data);
3040Sstevel@tonic-gate 					break;
3050Sstevel@tonic-gate 				}
3060Sstevel@tonic-gate 				*p = right_branch;
3070Sstevel@tonic-gate 				p = &right_branch->left;
3080Sstevel@tonic-gate 				right_branch = *p;
3090Sstevel@tonic-gate 				right_weight = weight(right_branch);
3100Sstevel@tonic-gate 			}
3110Sstevel@tonic-gate 		}/*else*/
3120Sstevel@tonic-gate 	}/*while*/
3130Sstevel@tonic-gate 	*p = NIL;
3140Sstevel@tonic-gate 	putfreehdr(x);
3150Sstevel@tonic-gate } /*delete*/
3160Sstevel@tonic-gate 
317*722Smuffin 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * demote(p)
3200Sstevel@tonic-gate  *	Demotes a node in a cartesian tree, if necessary, to establish
3210Sstevel@tonic-gate  *	the required vertical ordering.
3220Sstevel@tonic-gate  *
3230Sstevel@tonic-gate  * algorithm:
3240Sstevel@tonic-gate  *	The left and right subtrees of the node to be demoted are to
3250Sstevel@tonic-gate  *	be partially merged and attached in place of the demoted node.
3260Sstevel@tonic-gate  *	The nodes on the inside edges of these two subtrees are
3270Sstevel@tonic-gate  *	examined and the longer nodes are placed above the shorter
3280Sstevel@tonic-gate  *	ones, until a node is reached which has a length no greater
3290Sstevel@tonic-gate  *	than that of the node being demoted (or until a null pointer
3300Sstevel@tonic-gate  *	is reached).  The node is then attached at this point, and
3310Sstevel@tonic-gate  *	the remaining subtrees (if any) become its descendants.
3320Sstevel@tonic-gate  *
3330Sstevel@tonic-gate  * on entry:
3340Sstevel@tonic-gate  *   a. All the nodes in the tree, including the one to be demoted,
3350Sstevel@tonic-gate  *	must be correctly ordered horizontally;
3360Sstevel@tonic-gate  *   b. All the nodes except the one to be demoted must also be
3370Sstevel@tonic-gate  *	correctly positioned vertically.  The node to be demoted
3380Sstevel@tonic-gate  *	may be already correctly positioned vertically, or it may
3390Sstevel@tonic-gate  *	have a length which is less than that of one or both of
3400Sstevel@tonic-gate  *	its progeny.
3410Sstevel@tonic-gate  *   c. *p is non-null
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate 
344*722Smuffin static void
demote(Freehdr * p)345*722Smuffin demote(Freehdr *p)
3460Sstevel@tonic-gate {
347*722Smuffin 	Freehdr x;		/* addr of node to be demoted */
348*722Smuffin 	Freehdr left_branch;
349*722Smuffin 	Freehdr right_branch;
350*722Smuffin 	uint	left_weight;
351*722Smuffin 	uint	right_weight;
352*722Smuffin 	uint	x_weight;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	x = *p;
3550Sstevel@tonic-gate 	x_weight = weight(x);
3560Sstevel@tonic-gate 	left_branch = x->left;
3570Sstevel@tonic-gate 	right_branch = x->right;
3580Sstevel@tonic-gate 	left_weight = weight(left_branch);
3590Sstevel@tonic-gate 	right_weight = weight(right_branch);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	while (left_weight > x_weight || right_weight > x_weight) {
3620Sstevel@tonic-gate 		/*
3630Sstevel@tonic-gate 		 * select a descendant branch for promotion
3640Sstevel@tonic-gate 		 */
3650Sstevel@tonic-gate 		if (left_weight >= right_weight) {
3660Sstevel@tonic-gate 			/*
3670Sstevel@tonic-gate 			 * promote the left branch
3680Sstevel@tonic-gate 			 */
3690Sstevel@tonic-gate 			*p = left_branch;
3700Sstevel@tonic-gate 			p = &left_branch->right;
3710Sstevel@tonic-gate 			left_branch = *p;
3720Sstevel@tonic-gate 			left_weight = weight(left_branch);
3730Sstevel@tonic-gate 		} else {
3740Sstevel@tonic-gate 			/*
3750Sstevel@tonic-gate 			 * promote the right branch
3760Sstevel@tonic-gate 			 */
3770Sstevel@tonic-gate 			*p = right_branch;
3780Sstevel@tonic-gate 			p = &right_branch->left;
3790Sstevel@tonic-gate 			right_branch = *p;
3800Sstevel@tonic-gate 			right_weight = weight(right_branch);
3810Sstevel@tonic-gate 		} /*else*/
3820Sstevel@tonic-gate 	} /*while*/
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	*p = x;				/* attach demoted node here */
3850Sstevel@tonic-gate 	x->left = left_branch;
3860Sstevel@tonic-gate 	x->right = right_branch;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate } /*demote*/
3890Sstevel@tonic-gate 
390*722Smuffin 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * char*
3930Sstevel@tonic-gate  * malloc(nbytes)
3940Sstevel@tonic-gate  *	Allocates a block of length specified in bytes.  If nbytes is
3950Sstevel@tonic-gate  *	zero, a valid pointer (that should not be dereferenced) is returned.
3960Sstevel@tonic-gate  *
3970Sstevel@tonic-gate  * algorithm:
3980Sstevel@tonic-gate  *	The freelist is searched by descending the tree from the root
3990Sstevel@tonic-gate  *	so that at each decision point the "better fitting" branch node
4000Sstevel@tonic-gate  *	is chosen (i.e., the shorter one, if it is long enough, or
4010Sstevel@tonic-gate  *	the longer one, otherwise).  The descent stops when both
4020Sstevel@tonic-gate  *	branch nodes are too short.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * function result:
4050Sstevel@tonic-gate  *	Malloc returns a pointer to the allocated block. A null
4060Sstevel@tonic-gate  *	pointer indicates an error.
4070Sstevel@tonic-gate  *
4080Sstevel@tonic-gate  * diagnostics:
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  *	ENOMEM: storage could not be allocated.
4110Sstevel@tonic-gate  *
4120Sstevel@tonic-gate  *	EINVAL: either the argument was invalid, or the heap was found
4130Sstevel@tonic-gate  *	to be in an inconsistent state.  More detailed information may
4140Sstevel@tonic-gate  *	be obtained by enabling range checks (cf., malloc_debug()).
4150Sstevel@tonic-gate  *
4160Sstevel@tonic-gate  * Note: In this implementation, each allocated block includes a
4170Sstevel@tonic-gate  *	length word, which occurs before the address seen by the caller.
4180Sstevel@tonic-gate  *	Allocation requests are rounded up to a multiple of wordsize.
4190Sstevel@tonic-gate  */
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate ptr_t
malloc(uint nbytes)422*722Smuffin malloc(uint nbytes)
4230Sstevel@tonic-gate {
424*722Smuffin 	Freehdr allocp;	/* ptr to node to be allocated */
425*722Smuffin 	Freehdr *fpp;		/* for tree modifications */
426*722Smuffin 	Freehdr left_branch;
427*722Smuffin 	Freehdr right_branch;
428*722Smuffin 	uint	 left_weight;
429*722Smuffin 	uint	 right_weight;
4300Sstevel@tonic-gate 	Dblk	 retblk;		/* block returned to the user */
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * if rigorous checking was requested, do it.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (debug_level >= 2) {
4360Sstevel@tonic-gate 		malloc_verify();
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * add the size of a length word to the request, and
4410Sstevel@tonic-gate 	 * guarantee at least one word of usable data.
4420Sstevel@tonic-gate 	 */
4430Sstevel@tonic-gate 	nbytes += ALIGNSIZ;
4440Sstevel@tonic-gate 	if (nbytes < SMALLEST_BLK) {
4450Sstevel@tonic-gate 		nbytes = SMALLEST_BLK;
4460Sstevel@tonic-gate 	} else {
4470Sstevel@tonic-gate 		nbytes = roundup(nbytes, ALIGNSIZ);
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/*
4510Sstevel@tonic-gate 	 * ensure that at least one block is big enough to satisfy
4520Sstevel@tonic-gate 	 *	the request.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (weight(_root) < nbytes) {
4560Sstevel@tonic-gate 		/*
4570Sstevel@tonic-gate 		 * the largest block is not enough.
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		if(!morecore(nbytes))
4600Sstevel@tonic-gate 			return 0;
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/*
4640Sstevel@tonic-gate 	 * search down through the tree until a suitable block is
4650Sstevel@tonic-gate 	 *	found.  At each decision point, select the better
4660Sstevel@tonic-gate 	 *	fitting node.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	fpp = &_root;
4700Sstevel@tonic-gate 	allocp = *fpp;
4710Sstevel@tonic-gate 	left_branch = allocp->left;
4720Sstevel@tonic-gate 	right_branch = allocp->right;
4730Sstevel@tonic-gate 	left_weight = weight(left_branch);
4740Sstevel@tonic-gate 	right_weight = weight(right_branch);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	while (left_weight >= nbytes || right_weight >= nbytes) {
4770Sstevel@tonic-gate 		if (left_weight <= right_weight) {
4780Sstevel@tonic-gate 			if (left_weight >= nbytes) {
4790Sstevel@tonic-gate 				fpp = &allocp->left;
4800Sstevel@tonic-gate 				allocp = left_branch;
4810Sstevel@tonic-gate 			} else {
4820Sstevel@tonic-gate 				fpp = &allocp->right;
4830Sstevel@tonic-gate 				allocp = right_branch;
4840Sstevel@tonic-gate 			}
4850Sstevel@tonic-gate 		} else {
4860Sstevel@tonic-gate 			if (right_weight >= nbytes) {
4870Sstevel@tonic-gate 				fpp = &allocp->right;
4880Sstevel@tonic-gate 				allocp = right_branch;
4890Sstevel@tonic-gate 			} else {
4900Sstevel@tonic-gate 				fpp = &allocp->left;
4910Sstevel@tonic-gate 				allocp = left_branch;
4920Sstevel@tonic-gate 			}
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 		left_branch = allocp->left;
4950Sstevel@tonic-gate 		right_branch = allocp->right;
4960Sstevel@tonic-gate 		left_weight = weight(left_branch);
4970Sstevel@tonic-gate 		right_weight = weight(right_branch);
4980Sstevel@tonic-gate 	} /*while*/
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * allocate storage from the selected node.
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if (allocp->size - nbytes <= SMALLEST_BLK) {
5050Sstevel@tonic-gate 		/*
5060Sstevel@tonic-gate 		 * not big enough to split; must leave at least
5070Sstevel@tonic-gate 		 * a dblk's worth of space.
5080Sstevel@tonic-gate 		 */
5090Sstevel@tonic-gate 		retblk = allocp->block;
5100Sstevel@tonic-gate 		delete(fpp);
5110Sstevel@tonic-gate 	} else {
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		/*
5140Sstevel@tonic-gate 		 * Split the selected block n bytes from the top. The
5150Sstevel@tonic-gate 		 * n bytes at the top are returned to the caller; the
5160Sstevel@tonic-gate 		 * remainder of the block goes back to free space.
5170Sstevel@tonic-gate 		 */
518*722Smuffin 		Dblk nblk;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		retblk = allocp->block;
5210Sstevel@tonic-gate 		nblk = nextblk(retblk, nbytes);		/* ^next block */
5220Sstevel@tonic-gate 		nblk->size =  allocp->size = retblk->size - nbytes;
5230Sstevel@tonic-gate 		__mallinfo.ordblks++;			/* count fragments */
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * Change the selected node to point at the newly split
5270Sstevel@tonic-gate 		 * block, and move the node to its proper place in
5280Sstevel@tonic-gate 		 * the free space list.
5290Sstevel@tonic-gate 		 */
5300Sstevel@tonic-gate 		allocp->block = nblk;
5310Sstevel@tonic-gate 		demote(fpp);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		/*
5340Sstevel@tonic-gate 		 * set the length field of the allocated block; we need
5350Sstevel@tonic-gate 		 * this because free() does not specify a length.
5360Sstevel@tonic-gate 		 */
5370Sstevel@tonic-gate 		retblk->size = nbytes;
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	/* maintain statistics */
5400Sstevel@tonic-gate 	__mallinfo.uordbytes += retblk->size;		/* bytes allocated */
5410Sstevel@tonic-gate 	__mallinfo.allocated++;				/* frags allocated */
5420Sstevel@tonic-gate 	if (nbytes < __mallinfo.mxfast)
5430Sstevel@tonic-gate 		__mallinfo.smblks++;	/* kludge to pass the SVVS */
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return((ptr_t)retblk->data);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate } /*malloc*/
548*722Smuffin 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * free(p)
5510Sstevel@tonic-gate  *	return a block to the free space tree.
5520Sstevel@tonic-gate  *
5530Sstevel@tonic-gate  * algorithm:
5540Sstevel@tonic-gate  *	Starting at the root, search for and coalesce free blocks
5550Sstevel@tonic-gate  *	adjacent to one given.  When the appropriate place in the
5560Sstevel@tonic-gate  *	tree is found, insert the given block.
5570Sstevel@tonic-gate  *
5580Sstevel@tonic-gate  * Some sanity checks to avoid total confusion in the tree.
5590Sstevel@tonic-gate  *	If the block has already been freed, return.
5600Sstevel@tonic-gate  *	If the ptr is not from the sbrk'ed space, return.
5610Sstevel@tonic-gate  *	If the block size is invalid, return.
5620Sstevel@tonic-gate  */
5630Sstevel@tonic-gate free_t
free(ptr_t ptr)564*722Smuffin free(ptr_t ptr)
5650Sstevel@tonic-gate {
566*722Smuffin 	uint 	 nbytes;	/* Size of node to be released */
567*722Smuffin 	Freehdr *fpp;		/* For deletion from free list */
568*722Smuffin 	Freehdr neighbor;	/* Node to be coalesced */
569*722Smuffin 	Dblk	 neighbor_blk;	/* Ptr to potential neighbor */
570*722Smuffin 	uint	 neighbor_size;	/* Size of potential neighbor */
571*722Smuffin 	Dblk	 oldblk;	/* Ptr to block to be freed */
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * if rigorous checking was requested, do it.
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	if (debug_level >= 2) {
5770Sstevel@tonic-gate 		malloc_verify();
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/*
5810Sstevel@tonic-gate 	 * Check the address of the old block.
5820Sstevel@tonic-gate 	 */
5830Sstevel@tonic-gate 	if ( misaligned(ptr) ) {
5840Sstevel@tonic-gate 		error("free: illegal address (%#x)\n", ptr);
5850Sstevel@tonic-gate 		free_return(0);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * Freeing something that wasn't allocated isn't
5900Sstevel@tonic-gate 	 * exactly kosher, but fclose() does it routinely.
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	if( ptr < (ptr_t)_lbound || ptr > (ptr_t)_ubound ) {
5930Sstevel@tonic-gate 		errno = EINVAL;
5940Sstevel@tonic-gate 		free_return(0);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * Get node length by backing up by the size of a header.
5990Sstevel@tonic-gate 	 * Check for a valid length.  It must be a positive
6000Sstevel@tonic-gate 	 * multiple of ALIGNSIZ, at least as large as SMALLEST_BLK,
6010Sstevel@tonic-gate 	 * no larger than the extent of the heap, and must not
6020Sstevel@tonic-gate 	 * extend beyond the end of the heap.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	oldblk = (Dblk)((char*)ptr - ALIGNSIZ);
6050Sstevel@tonic-gate 	nbytes = oldblk->size;
6060Sstevel@tonic-gate 	if (badblksize(oldblk,nbytes)) {
6070Sstevel@tonic-gate 		error("free: bad block size (%d) at %#x\n",
6080Sstevel@tonic-gate 			(int)nbytes, (int)oldblk );
6090Sstevel@tonic-gate 		free_return(0);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/* maintain statistics */
6130Sstevel@tonic-gate 	__mallinfo.uordbytes -= nbytes;		/* bytes allocated */
6140Sstevel@tonic-gate 	__mallinfo.allocated--;			/* frags allocated */
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * Search the tree for the correct insertion point for this
6180Sstevel@tonic-gate 	 *	node, coalescing adjacent free blocks along the way.
6190Sstevel@tonic-gate 	 */
6200Sstevel@tonic-gate 	fpp = &_root;
6210Sstevel@tonic-gate 	neighbor = *fpp;
6220Sstevel@tonic-gate 	while (neighbor != NIL) {
6230Sstevel@tonic-gate 		neighbor_blk = neighbor->block;
6240Sstevel@tonic-gate 		neighbor_size = neighbor->size;
6250Sstevel@tonic-gate 		if (oldblk < neighbor_blk) {
6260Sstevel@tonic-gate 			Dblk nblk = nextblk(oldblk,nbytes);
6270Sstevel@tonic-gate 			if (nblk == neighbor_blk) {
6280Sstevel@tonic-gate 				/*
6290Sstevel@tonic-gate 				 * Absorb and delete right neighbor
6300Sstevel@tonic-gate 				 */
6310Sstevel@tonic-gate 				nbytes += neighbor_size;
6320Sstevel@tonic-gate 				__mallinfo.ordblks--;
6330Sstevel@tonic-gate 				delete(fpp);
6340Sstevel@tonic-gate 			} else if (nblk > neighbor_blk) {
6350Sstevel@tonic-gate 				/*
6360Sstevel@tonic-gate 				 * The block being freed overlaps
6370Sstevel@tonic-gate 				 * another block in the tree.  This
6380Sstevel@tonic-gate 				 * is bad news.  Return to avoid
6390Sstevel@tonic-gate 				 * further fouling up the the tree.
6400Sstevel@tonic-gate 				 */
6410Sstevel@tonic-gate 				 error("free: blocks %#x, %#x overlap\n",
6420Sstevel@tonic-gate 						(int)oldblk, (int)neighbor_blk);
6430Sstevel@tonic-gate 				 free_return(0);
6440Sstevel@tonic-gate 			} else {
6450Sstevel@tonic-gate 				/*
6460Sstevel@tonic-gate 				 * Search to the left
6470Sstevel@tonic-gate 			 	 */
6480Sstevel@tonic-gate 				fpp = &neighbor->left;
6490Sstevel@tonic-gate 			}
6500Sstevel@tonic-gate 		} else if (oldblk > neighbor_blk) {
6510Sstevel@tonic-gate 			Dblk nblk = nextblk(neighbor_blk, neighbor_size);
6520Sstevel@tonic-gate 			if (nblk == oldblk) {
6530Sstevel@tonic-gate 				/*
6540Sstevel@tonic-gate 				 * Absorb and delete left neighbor
6550Sstevel@tonic-gate 				 */
6560Sstevel@tonic-gate 				oldblk = neighbor_blk;
6570Sstevel@tonic-gate 				nbytes += neighbor_size;
6580Sstevel@tonic-gate 				__mallinfo.ordblks--;
6590Sstevel@tonic-gate 				delete(fpp);
6600Sstevel@tonic-gate 			} else if (nblk > oldblk) {
6610Sstevel@tonic-gate 				/*
6620Sstevel@tonic-gate 				 * This block has already been freed
6630Sstevel@tonic-gate 				 */
6640Sstevel@tonic-gate 				error("free: block %#x was already free\n",
6650Sstevel@tonic-gate 					(int)ptr);
6660Sstevel@tonic-gate 				free_return(0);
6670Sstevel@tonic-gate 			} else {
6680Sstevel@tonic-gate 				/*
6690Sstevel@tonic-gate 				 * search to the right
6700Sstevel@tonic-gate 				 */
6710Sstevel@tonic-gate 				fpp = &neighbor->right;
6720Sstevel@tonic-gate 			}
6730Sstevel@tonic-gate 		} else {
6740Sstevel@tonic-gate 			/*
6750Sstevel@tonic-gate 			 * This block has already been freed
6760Sstevel@tonic-gate 			 * as "oldblk == neighbor_blk"
6770Sstevel@tonic-gate 			 */
6780Sstevel@tonic-gate 			error("free: block %#x was already free\n", (int)ptr);
6790Sstevel@tonic-gate 			free_return(0);
6800Sstevel@tonic-gate 		} /*else*/
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * Note that this depends on a side effect of
6840Sstevel@tonic-gate 		 * delete(fpp) in order to terminate the loop!
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate 		neighbor = *fpp;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	} /*while*/
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * Insert the new node into the free space tree
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	insert( oldblk, nbytes );
6940Sstevel@tonic-gate 	free_return(1);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate } /*free*/
6970Sstevel@tonic-gate 
698*722Smuffin 
6990Sstevel@tonic-gate /*
7000Sstevel@tonic-gate  * char*
7010Sstevel@tonic-gate  * shrink(oldblk, oldsize, newsize)
7020Sstevel@tonic-gate  *	Decreases the size of an old block to a new size.
7030Sstevel@tonic-gate  *	Returns the remainder to free space.  Returns the
7040Sstevel@tonic-gate  *	truncated block to the caller.
7050Sstevel@tonic-gate  */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate static char *
shrink(Dblk oldblk,uint oldsize,uint newsize)708*722Smuffin shrink(Dblk oldblk, uint oldsize, uint newsize)
7090Sstevel@tonic-gate {
710*722Smuffin 	Dblk remainder;
7110Sstevel@tonic-gate 	if (oldsize - newsize >= SMALLEST_BLK) {
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * Block is to be contracted. Split the old block
7140Sstevel@tonic-gate 		 * and return the remainder to free space.
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 		remainder = nextblk(oldblk, newsize);
7170Sstevel@tonic-gate 		remainder->size = oldsize - newsize;
7180Sstevel@tonic-gate 		oldblk->size = newsize;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		/* maintain statistics */
7210Sstevel@tonic-gate 		__mallinfo.ordblks++;		/* count fragments */
7220Sstevel@tonic-gate 		__mallinfo.allocated++;		/* negate effect of free() */
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		free(remainder->data);
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 	return(oldblk->data);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * char*
7310Sstevel@tonic-gate  * realloc(ptr, nbytes)
7320Sstevel@tonic-gate  *
7330Sstevel@tonic-gate  * Reallocate an old block with a new size, returning the old block
7340Sstevel@tonic-gate  * if possible. The block returned is guaranteed to preserve the
7350Sstevel@tonic-gate  * contents of the old block up to min(size(old block), newsize).
7360Sstevel@tonic-gate  *
7370Sstevel@tonic-gate  * For backwards compatibility, ptr is allowed to reference
7380Sstevel@tonic-gate  * a block freed since the LAST call of malloc().  Thus the old
7390Sstevel@tonic-gate  * block may be busy, free, or may even be nested within a free
7400Sstevel@tonic-gate  * block.
7410Sstevel@tonic-gate  *
7420Sstevel@tonic-gate  * Some old programs have been known to do things like the following,
7430Sstevel@tonic-gate  * which is guaranteed not to work:
7440Sstevel@tonic-gate  *
7450Sstevel@tonic-gate  *	free(ptr);
7460Sstevel@tonic-gate  *	free(dummy);
7470Sstevel@tonic-gate  *	dummy = malloc(1);
7480Sstevel@tonic-gate  *	ptr = realloc(ptr,nbytes);
7490Sstevel@tonic-gate  *
7500Sstevel@tonic-gate  * This atrocity was found in the source for diff(1).
7510Sstevel@tonic-gate  */
7520Sstevel@tonic-gate ptr_t
realloc(ptr_t ptr,uint nbytes)753*722Smuffin realloc(ptr_t ptr, uint nbytes)
7540Sstevel@tonic-gate {
755*722Smuffin 	Freehdr *fpp;
756*722Smuffin 	Freehdr fp;
757*722Smuffin 	Dblk	oldblk;
758*722Smuffin 	Dblk	freeblk;
759*722Smuffin 	Dblk	oldneighbor;
760*722Smuffin 	uint	oldsize;
761*722Smuffin 	uint	newsize;
762*722Smuffin 	uint	oldneighborsize;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * Add SVR4 semantics for OS 5.x so /usr/lib librarys
7660Sstevel@tonic-gate 	 * work correctly when running in BCP mode
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	if (ptr == NULL) {
7690Sstevel@tonic-gate 		return (malloc(nbytes));
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/*
7730Sstevel@tonic-gate 	 * if rigorous checking was requested, do it.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	if (debug_level >= 2) {
7760Sstevel@tonic-gate 		malloc_verify();
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/*
7800Sstevel@tonic-gate 	 * Check the address of the old block.
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	if ( misaligned(ptr) ||
7830Sstevel@tonic-gate 	    ptr < (ptr_t)_lbound ||
7840Sstevel@tonic-gate 	    ptr > (ptr_t)_ubound ) {
7850Sstevel@tonic-gate 		error("realloc: illegal address (%#x)\n", ptr);
7860Sstevel@tonic-gate 		return(NULL);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * check location and size of the old block and its
7910Sstevel@tonic-gate 	 * neighboring block to the right.  If the old block is
7920Sstevel@tonic-gate 	 * at end of memory, the neighboring block is undefined.
7930Sstevel@tonic-gate 	 */
7940Sstevel@tonic-gate 	oldblk = (Dblk)((char*)ptr - ALIGNSIZ);
7950Sstevel@tonic-gate 	oldsize = oldblk->size;
7960Sstevel@tonic-gate 	if (badblksize(oldblk,oldsize)) {
7970Sstevel@tonic-gate 		error("realloc: bad block size (%d) at %#x\n",
7980Sstevel@tonic-gate 			oldsize, oldblk);
7990Sstevel@tonic-gate 		return(NULL);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 	oldneighbor = nextblk(oldblk,oldsize);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	/* *** tree search code pulled into separate subroutine *** */
8040Sstevel@tonic-gate 	if (reclaim(oldblk, oldsize, 1) == -1) {
8050Sstevel@tonic-gate 		return(NULL);		/* internal error */
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	/*
8090Sstevel@tonic-gate 	 * At this point, we can guarantee that oldblk is out of free
8100Sstevel@tonic-gate 	 * space. What we do next depends on a comparison of the size
8110Sstevel@tonic-gate 	 * of the old block and the requested new block size.  To do
8120Sstevel@tonic-gate 	 * this, first round up the new size request.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	newsize = nbytes + ALIGNSIZ;		/* add size of a length word */
8150Sstevel@tonic-gate 	if (newsize < SMALLEST_BLK) {
8160Sstevel@tonic-gate 		newsize = SMALLEST_BLK;
8170Sstevel@tonic-gate 	} else {
8180Sstevel@tonic-gate 		newsize = roundup(newsize, ALIGNSIZ);
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	/*
8220Sstevel@tonic-gate 	 * Next, examine the size of the old block, and compare it
8230Sstevel@tonic-gate 	 * with the requested new size.
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (oldsize >= newsize) {
8270Sstevel@tonic-gate 		/*
8280Sstevel@tonic-gate 		 * Block is to be made smaller.
8290Sstevel@tonic-gate 		 */
8300Sstevel@tonic-gate 		return(shrink(oldblk, oldsize, newsize));
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/*
8340Sstevel@tonic-gate 	 * Block is to be expanded.  Look for adjacent free memory.
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	if ( oldneighbor < (Dblk)_ubound ) {
8370Sstevel@tonic-gate 		/*
8380Sstevel@tonic-gate 		 * Search for the adjacent block in the free
8390Sstevel@tonic-gate 		 * space tree.  Note that the tree may have been
8400Sstevel@tonic-gate 		 * modified in the earlier loop.
8410Sstevel@tonic-gate 		 */
8420Sstevel@tonic-gate 		fpp = &_root;
8430Sstevel@tonic-gate 		fp = *fpp;
8440Sstevel@tonic-gate 		oldneighborsize = oldneighbor->size;
8450Sstevel@tonic-gate 		if ( badblksize(oldneighbor, oldneighborsize) ) {
8460Sstevel@tonic-gate 			error("realloc: bad blocksize(%d) at %#x\n",
8470Sstevel@tonic-gate 				oldneighborsize, oldneighbor);
8480Sstevel@tonic-gate 			return(NULL);
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 		while ( weight(fp) >= oldneighborsize ) {
8510Sstevel@tonic-gate 			freeblk = fp->block;
8520Sstevel@tonic-gate 			if (oldneighbor < freeblk) {
8530Sstevel@tonic-gate 				/*
8540Sstevel@tonic-gate 				 * search to the left
8550Sstevel@tonic-gate 				 */
8560Sstevel@tonic-gate 				fpp = &(fp->left);
8570Sstevel@tonic-gate 				fp = *fpp;
8580Sstevel@tonic-gate 			}
8590Sstevel@tonic-gate 			else if (oldneighbor > freeblk) {
8600Sstevel@tonic-gate 				/*
8610Sstevel@tonic-gate 				 * search to the right
8620Sstevel@tonic-gate 				 */
8630Sstevel@tonic-gate 				fpp = &(fp->right);
8640Sstevel@tonic-gate 				fp = *fpp;
8650Sstevel@tonic-gate 			}
8660Sstevel@tonic-gate 			else {		/* oldneighbor == freeblk */
8670Sstevel@tonic-gate 				/*
8680Sstevel@tonic-gate 				 * neighboring block is free; is it big enough?
8690Sstevel@tonic-gate 				 */
8700Sstevel@tonic-gate 				if (oldsize + oldneighborsize >= newsize) {
8710Sstevel@tonic-gate 					/*
8720Sstevel@tonic-gate 					 * Big enough. Delete freeblk, join
8730Sstevel@tonic-gate 					 * oldblk to neighbor, return newsize
8740Sstevel@tonic-gate 					 * bytes to the caller, and return the
8750Sstevel@tonic-gate 					 * remainder to free storage.
8760Sstevel@tonic-gate 					 */
8770Sstevel@tonic-gate 					delete(fpp);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 					/* maintain statistics */
8800Sstevel@tonic-gate 					__mallinfo.ordblks--;
8810Sstevel@tonic-gate 					__mallinfo.uordbytes += oldneighborsize;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 					oldsize += oldneighborsize;
8840Sstevel@tonic-gate 					oldblk->size = oldsize;
8850Sstevel@tonic-gate 					return(shrink(oldblk, oldsize, newsize));
8860Sstevel@tonic-gate 				} else {
8870Sstevel@tonic-gate 					/*
8880Sstevel@tonic-gate 					 * Not big enough. Stop looking for a
8890Sstevel@tonic-gate 					 * free lunch.
8900Sstevel@tonic-gate 					 */
8910Sstevel@tonic-gate 					break;
8920Sstevel@tonic-gate 				} /*else*/
8930Sstevel@tonic-gate 			} /*else*/
8940Sstevel@tonic-gate 		}/*while*/
8950Sstevel@tonic-gate 	} /*if*/
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*
8980Sstevel@tonic-gate 	 * At this point, we know there is no free space in which to
8990Sstevel@tonic-gate 	 * expand. Malloc a new block, copy the old block to the new,
9000Sstevel@tonic-gate 	 * and free the old block, IN THAT ORDER.
9010Sstevel@tonic-gate 	 */
9020Sstevel@tonic-gate 	ptr = malloc(nbytes);
9030Sstevel@tonic-gate 	if (ptr != NULL) {
9040Sstevel@tonic-gate 		bcopy(oldblk->data, ptr, oldsize-ALIGNSIZ);
9050Sstevel@tonic-gate 		free(oldblk->data);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 	return(ptr);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate } /* realloc */
9100Sstevel@tonic-gate 
911*722Smuffin 
9120Sstevel@tonic-gate /*
9130Sstevel@tonic-gate  * *** The following code was pulled out of realloc() ***
9140Sstevel@tonic-gate  *
9150Sstevel@tonic-gate  * int
9160Sstevel@tonic-gate  * reclaim(oldblk, oldsize, flag)
9170Sstevel@tonic-gate  *	If a block containing 'oldsize' bytes from 'oldblk'
9180Sstevel@tonic-gate  *	is in the free list, remove it from the free list.
9190Sstevel@tonic-gate  *	'oldblk' and 'oldsize' are assumed to include the free block header.
9200Sstevel@tonic-gate  *
9210Sstevel@tonic-gate  *	Returns 1 if block was successfully removed.
9220Sstevel@tonic-gate  *	Returns 0 if block was not in free list.
9230Sstevel@tonic-gate  *	Returns -1 if block spans a free/allocated boundary (error() called
9240Sstevel@tonic-gate  *						if 'flag' == 1).
9250Sstevel@tonic-gate  */
9260Sstevel@tonic-gate static int
reclaim(Dblk oldblk,uint oldsize,int flag)927*722Smuffin reclaim(Dblk oldblk, uint oldsize, int flag)
9280Sstevel@tonic-gate {
929*722Smuffin 	Dblk oldneighbor;
930*722Smuffin 	Freehdr	*fpp;
931*722Smuffin 	Freehdr	fp;
932*722Smuffin 	Dblk		freeblk;
933*722Smuffin 	uint		size;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/*
9360Sstevel@tonic-gate 	 * Search the free space list for a node describing oldblk,
9370Sstevel@tonic-gate 	 * or a node describing a block containing oldblk.  Assuming
9380Sstevel@tonic-gate 	 * the size of blocks decreases monotonically with depth in
9390Sstevel@tonic-gate 	 * the tree, the loop may terminate as soon as a block smaller
9400Sstevel@tonic-gate 	 * than oldblk is encountered.
9410Sstevel@tonic-gate 	 */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	oldneighbor = nextblk(oldblk, oldsize);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	fpp = &_root;
9460Sstevel@tonic-gate 	fp = *fpp;
9470Sstevel@tonic-gate 	while ( (size = weight(fp)) >= oldsize ) {
9480Sstevel@tonic-gate 		freeblk = fp->block;
9490Sstevel@tonic-gate 		if (badblksize(freeblk,size)) {
9500Sstevel@tonic-gate 			error("realloc: bad block size (%d) at %#x\n",
9510Sstevel@tonic-gate 				size, freeblk);
9520Sstevel@tonic-gate 			return(-1);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		if ( oldblk == freeblk ) {
9550Sstevel@tonic-gate 			/*
9560Sstevel@tonic-gate 			 * |<-- freeblk ...
9570Sstevel@tonic-gate 			 * _________________________________
9580Sstevel@tonic-gate 			 * |<-- oldblk ...
9590Sstevel@tonic-gate 			 * ---------------------------------
9600Sstevel@tonic-gate 			 * Found oldblk in the free space tree; delete it.
9610Sstevel@tonic-gate 			 */
9620Sstevel@tonic-gate 			delete(fpp);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 			/* maintain statistics */
9650Sstevel@tonic-gate 			__mallinfo.uordbytes += oldsize;
9660Sstevel@tonic-gate 			__mallinfo.allocated++;
9670Sstevel@tonic-gate 			return(1);
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 		else if (oldblk < freeblk) {
9700Sstevel@tonic-gate 			/*
9710Sstevel@tonic-gate 			 * 		|<-- freeblk ...
9720Sstevel@tonic-gate 			 * _________________________________
9730Sstevel@tonic-gate 			 * |<--oldblk ...
9740Sstevel@tonic-gate 			 * ---------------------------------
9750Sstevel@tonic-gate 			 * Search to the left for oldblk
9760Sstevel@tonic-gate 			 */
9770Sstevel@tonic-gate 			fpp = &fp->left;
9780Sstevel@tonic-gate 			fp = *fpp;
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 		else {
9810Sstevel@tonic-gate 			/*
9820Sstevel@tonic-gate 			 * |<-- freeblk ...
9830Sstevel@tonic-gate 			 * _________________________________
9840Sstevel@tonic-gate 			 * |     		|<--oldblk--->|<--oldneighbor
9850Sstevel@tonic-gate 			 * ---------------------------------
9860Sstevel@tonic-gate 			 * oldblk is somewhere to the right of freeblk.
9870Sstevel@tonic-gate 			 * Check to see if it lies within freeblk.
9880Sstevel@tonic-gate 			 */
989*722Smuffin 			Dblk freeneighbor;
9900Sstevel@tonic-gate 			freeneighbor =  nextblk(freeblk, freeblk->size);
9910Sstevel@tonic-gate 			if (oldblk >= freeneighbor) {
9920Sstevel@tonic-gate 				/*
9930Sstevel@tonic-gate 				 * |<-- freeblk--->|<--- freeneighbor ...
9940Sstevel@tonic-gate 				 * _________________________________
9950Sstevel@tonic-gate 				 * |  		      |<--oldblk--->|
9960Sstevel@tonic-gate 				 * ---------------------------------
9970Sstevel@tonic-gate 				 * no such luck; search to the right.
9980Sstevel@tonic-gate 				 */
9990Sstevel@tonic-gate 				fpp =  &fp->right;
10000Sstevel@tonic-gate 				fp = *fpp;
10010Sstevel@tonic-gate 			}
10020Sstevel@tonic-gate 			else {
10030Sstevel@tonic-gate 				/*
10040Sstevel@tonic-gate 				 * freeblk < oldblk < freeneighbor;
10050Sstevel@tonic-gate 				 * i.e., oldblk begins within freeblk.
10060Sstevel@tonic-gate 				 */
10070Sstevel@tonic-gate 				if (oldneighbor > freeneighbor) {
10080Sstevel@tonic-gate 					/*
10090Sstevel@tonic-gate 					 * |<-- freeblk--->|<--- freeneighbor
10100Sstevel@tonic-gate 					 * _________________________________
10110Sstevel@tonic-gate 					 * |     |<--oldblk--->|<--oldneighbor
10120Sstevel@tonic-gate 					 * ---------------------------------
10130Sstevel@tonic-gate 					 * oldblk straddles a block boundary!
10140Sstevel@tonic-gate 					 */
10150Sstevel@tonic-gate 					if (flag) {
10160Sstevel@tonic-gate 	    error("realloc: block %#x straddles free block boundary\n", oldblk);
10170Sstevel@tonic-gate 					}
10180Sstevel@tonic-gate 					return(-1);
10190Sstevel@tonic-gate 				}
10200Sstevel@tonic-gate 				else if (  oldneighbor == freeneighbor ) {
10210Sstevel@tonic-gate 					/*
10220Sstevel@tonic-gate 					 * |<-------- freeblk------------->|
10230Sstevel@tonic-gate 					 * _________________________________
10240Sstevel@tonic-gate 					 * |                 |<--oldblk--->|
10250Sstevel@tonic-gate 					 * ---------------------------------
10260Sstevel@tonic-gate 					 * Oldblk is on the right end of
10270Sstevel@tonic-gate 					 * freeblk. Delete freeblk, split
10280Sstevel@tonic-gate 					 * into two fragments, and return
10290Sstevel@tonic-gate 					 * the one on the left to free space.
10300Sstevel@tonic-gate 					 */
10310Sstevel@tonic-gate 					delete(fpp);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 					/* maintain statistics */
10340Sstevel@tonic-gate 					__mallinfo.ordblks++;
10350Sstevel@tonic-gate 					__mallinfo.uordbytes += oldsize;
10360Sstevel@tonic-gate 					__mallinfo.allocated += 2;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 					freeblk->size -= oldsize;
10390Sstevel@tonic-gate 					free(freeblk->data);
10400Sstevel@tonic-gate 					return(1);
10410Sstevel@tonic-gate 				}
10420Sstevel@tonic-gate 				else {
10430Sstevel@tonic-gate 					/*
10440Sstevel@tonic-gate 					 * |<-------- freeblk------------->|
10450Sstevel@tonic-gate 					 * _________________________________
10460Sstevel@tonic-gate 					 * |        |oldblk  | oldneighbor |
10470Sstevel@tonic-gate 					 * ---------------------------------
10480Sstevel@tonic-gate 					 * Oldblk is in the middle of freeblk.
10490Sstevel@tonic-gate 					 * Delete freeblk, split into three
10500Sstevel@tonic-gate 					 * fragments, and return the ones on
10510Sstevel@tonic-gate 					 * the ends to free space.
10520Sstevel@tonic-gate 					 */
10530Sstevel@tonic-gate 					delete(fpp);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 					/* maintain statistics */
10560Sstevel@tonic-gate 					__mallinfo.ordblks += 2;
10570Sstevel@tonic-gate 					__mallinfo.uordbytes += freeblk->size;
10580Sstevel@tonic-gate 					__mallinfo.allocated += 3;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 					/*
10610Sstevel@tonic-gate 					 * split the left fragment by
10620Sstevel@tonic-gate 					 * subtracting the size of oldblk
10630Sstevel@tonic-gate 					 * and oldblk's neighbor
10640Sstevel@tonic-gate 					 */
10650Sstevel@tonic-gate 					freeblk->size -=
10660Sstevel@tonic-gate 						( (char*)freeneighbor
10670Sstevel@tonic-gate 							- (char*)oldblk );
10680Sstevel@tonic-gate 					/*
10690Sstevel@tonic-gate 					 * split the right fragment by
10700Sstevel@tonic-gate 					 * setting oldblk's neighbor's size
10710Sstevel@tonic-gate 					 */
10720Sstevel@tonic-gate 					oldneighbor->size =
10730Sstevel@tonic-gate 						(char*)freeneighbor
10740Sstevel@tonic-gate 							- (char*)oldneighbor;
10750Sstevel@tonic-gate 					/*
10760Sstevel@tonic-gate 					 * return the fragments to free space
10770Sstevel@tonic-gate 					 */
10780Sstevel@tonic-gate 					free(freeblk->data);
10790Sstevel@tonic-gate 					free(oldneighbor->data);
10800Sstevel@tonic-gate 					return(1);
10810Sstevel@tonic-gate 				} /*else*/
10820Sstevel@tonic-gate 			} /*else*/
10830Sstevel@tonic-gate 		} /* else */
10840Sstevel@tonic-gate 	} /*while*/
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	return(0);		/* free block not found */
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate  * bool
10910Sstevel@tonic-gate  * morecore(nbytes)
10920Sstevel@tonic-gate  *	Add a block of at least nbytes from end-of-memory to the
10930Sstevel@tonic-gate  *	free space tree.
10940Sstevel@tonic-gate  *
10950Sstevel@tonic-gate  * return value:
10960Sstevel@tonic-gate  *	true	if at least n bytes can be allocated
10970Sstevel@tonic-gate  *	false	otherwise
10980Sstevel@tonic-gate  *
10990Sstevel@tonic-gate  * remarks:
11000Sstevel@tonic-gate  *
11010Sstevel@tonic-gate  *   -- free space (delimited by the extern variable _ubound) is
11020Sstevel@tonic-gate  *	extended by an amount determined by rounding nbytes up to
11030Sstevel@tonic-gate  *	a multiple of the system page size.
11040Sstevel@tonic-gate  *
11050Sstevel@tonic-gate  *   -- The lower bound of the heap is determined the first time
11060Sstevel@tonic-gate  *	this routine is entered. It does NOT necessarily begin at
11070Sstevel@tonic-gate  *	the end of static data space, since startup code (e.g., for
11080Sstevel@tonic-gate  *	profiling) may have invoked sbrk() before we got here.
11090Sstevel@tonic-gate  */
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate static bool
morecore(uint nbytes)1112*722Smuffin morecore(uint nbytes)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate 	Dblk p;
11150Sstevel@tonic-gate 	Freehdr newhdr;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	if (nbpg == 0) {
11180Sstevel@tonic-gate 		nbpg = getpagesize();
11190Sstevel@tonic-gate 		/* hack to avoid fragmenting the heap with the first
11200Sstevel@tonic-gate 		   freehdr page */
11210Sstevel@tonic-gate 		if ((newhdr = getfreehdr()) == NIL) {
11220Sstevel@tonic-gate 			/* Error message returned by getfreehdr() */
11230Sstevel@tonic-gate 			return(false);
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 		(void)putfreehdr(newhdr);
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 	nbytes = roundup(nbytes, nbpg);
11280Sstevel@tonic-gate 	p = (Dblk) sbrk((int)nbytes);
11290Sstevel@tonic-gate 	if (p == (Dblk) -1) {
11300Sstevel@tonic-gate 		if (errno == EAGAIN) errno = ENOMEM;
11310Sstevel@tonic-gate 		return(false);	/* errno = ENOMEM */
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	if (_lbound == NULL)	/* set _lbound the first time through */
11340Sstevel@tonic-gate 		_lbound = (char*) p;
11350Sstevel@tonic-gate 	_ubound = (char *) p + nbytes;
11360Sstevel@tonic-gate 	p->size = nbytes;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/* maintain statistics */
11390Sstevel@tonic-gate 	__mallinfo.arena = _ubound - _lbound;
11400Sstevel@tonic-gate 	__mallinfo.uordbytes += nbytes;
11410Sstevel@tonic-gate 	__mallinfo.ordblks++;
11420Sstevel@tonic-gate 	__mallinfo.allocated++;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	free(p->data);
11450Sstevel@tonic-gate 	return(true);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate } /*morecore*/
11480Sstevel@tonic-gate 
1149*722Smuffin 
11500Sstevel@tonic-gate /*
11510Sstevel@tonic-gate  * Get a free block header from the free header list.
11520Sstevel@tonic-gate  * When the list is empty, allocate an array of headers.
11530Sstevel@tonic-gate  * When the array is empty, allocate another one.
11540Sstevel@tonic-gate  * When we can't allocate another array, we're in deep weeds.
11550Sstevel@tonic-gate  */
11560Sstevel@tonic-gate static	Freehdr
getfreehdr(void)1157*722Smuffin getfreehdr(void)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	Freehdr	r;
1160*722Smuffin 	Dblk	blk;
1161*722Smuffin 	uint	size;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (freehdrlist != NIL) {
11640Sstevel@tonic-gate 		r = freehdrlist;
11650Sstevel@tonic-gate 		freehdrlist = freehdrlist->left;
11660Sstevel@tonic-gate 		return(r);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 	if (nfreehdrs <= 0) {
11690Sstevel@tonic-gate 		size = NFREE_HDRS*sizeof(struct freehdr) + ALIGNSIZ;
11700Sstevel@tonic-gate 		blk = (Dblk) sbrk(size);
11710Sstevel@tonic-gate 		if ((int)blk == -1) {
11720Sstevel@tonic-gate 			malloc_debug(1);
11730Sstevel@tonic-gate 			error("getfreehdr: out of memory");
11740Sstevel@tonic-gate 			if (errno == EAGAIN) errno = ENOMEM;
11750Sstevel@tonic-gate 			return(NIL);
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 		if (_lbound == NULL)	/* set _lbound on first allocation */
11780Sstevel@tonic-gate 			_lbound = (char*)blk;
11790Sstevel@tonic-gate 		blk->size = size;
11800Sstevel@tonic-gate 		freehdrptr = (Freehdr)blk->data;
11810Sstevel@tonic-gate 		nfreehdrs = NFREE_HDRS;
11820Sstevel@tonic-gate 		_ubound = (char*) nextblk(blk,size);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		/* maintain statistics */
11850Sstevel@tonic-gate 		__mallinfo.arena = _ubound - _lbound;
11860Sstevel@tonic-gate 		__mallinfo.treeoverhead += size;
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 	nfreehdrs--;
11890Sstevel@tonic-gate 	return(freehdrptr++);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * Free a free block header
11940Sstevel@tonic-gate  * Add it to the list of available headers.
11950Sstevel@tonic-gate  */
1196*722Smuffin static void
putfreehdr(Freehdr p)1197*722Smuffin putfreehdr(Freehdr p)
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	p->left = freehdrlist;
12000Sstevel@tonic-gate 	freehdrlist = p;
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate #ifndef DEBUG	/*	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate /*
12060Sstevel@tonic-gate  * stubs for error handling and diagnosis routines. These are what
12070Sstevel@tonic-gate  * you get in the standard C library; for non-placebo diagnostics
12080Sstevel@tonic-gate  * load /usr/lib/malloc.debug.o with your program.
12090Sstevel@tonic-gate  */
12100Sstevel@tonic-gate /*ARGSUSED*/
1211*722Smuffin static void
error(char * fmt,...)1212*722Smuffin error(char *fmt, ...)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	errno = EINVAL;
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate 
1217*722Smuffin #endif	/* !DEBUG	<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate #ifdef	DEBUG	/*	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate /*
12230Sstevel@tonic-gate  * malloc_debug(level)
12240Sstevel@tonic-gate  *
12250Sstevel@tonic-gate  * description:
12260Sstevel@tonic-gate  *
12270Sstevel@tonic-gate  *	Controls the level of error diagnosis and consistency checking
12280Sstevel@tonic-gate  *	done by malloc() and free(). level is interpreted as follows:
12290Sstevel@tonic-gate  *
12300Sstevel@tonic-gate  *	0:  malloc() and free() return 0 if error detected in arguments
12310Sstevel@tonic-gate  *	    (errno is set to EINVAL)
12320Sstevel@tonic-gate  *	1:  malloc() and free() abort if errors detected in arguments
12330Sstevel@tonic-gate  *	2:  same as 1, but scan entire heap for errors on every call
12340Sstevel@tonic-gate  *	    to malloc() or free()
12350Sstevel@tonic-gate  *
12360Sstevel@tonic-gate  * function result:
12370Sstevel@tonic-gate  *	returns the previous level of error reporting.
12380Sstevel@tonic-gate  */
12390Sstevel@tonic-gate int
malloc_debug(int level)1240*722Smuffin malloc_debug(int level)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	int old_level;
12430Sstevel@tonic-gate 	old_level = debug_level;
12440Sstevel@tonic-gate 	debug_level = level;
1245*722Smuffin 	return (old_level);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate  * check a free space tree pointer. Should be in
12500Sstevel@tonic-gate  * the static free pool or somewhere in the heap.
12510Sstevel@tonic-gate  */
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate #define chkblk(p)\
12540Sstevel@tonic-gate 	if ( misaligned(p)\
12550Sstevel@tonic-gate 		|| ((Dblk)(p) < (Dblk)_lbound || (Dblk)(p) > (Dblk)_ubound)){\
12560Sstevel@tonic-gate 		blkerror(p);\
12570Sstevel@tonic-gate 		return 0;\
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate #define chkhdr(p) chkblk(p)
12610Sstevel@tonic-gate 
1262*722Smuffin static
blkerror(Freehdr p)1263*722Smuffin blkerror(Freehdr p)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	error("Illegal block address (%#x)\n", (p));
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate /*
12690Sstevel@tonic-gate  * cartesian(p)
12700Sstevel@tonic-gate  *	returns 1 if free space tree p satisfies internal consistency
12710Sstevel@tonic-gate  *	checks.
12720Sstevel@tonic-gate  */
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate static int
cartesian(Freehdr p)1275*722Smuffin cartesian(Freehdr p)
12760Sstevel@tonic-gate {
1277*722Smuffin 	Freehdr probe;
1278*722Smuffin 	Dblk db,pdb;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	if (p == NIL)				/* no tree to test */
12810Sstevel@tonic-gate 		return 1;
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * check that root has a data block
12840Sstevel@tonic-gate 	 */
12850Sstevel@tonic-gate 	chkhdr(p);
12860Sstevel@tonic-gate 	pdb = p->block;
12870Sstevel@tonic-gate 	chkblk(pdb);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	/*
12900Sstevel@tonic-gate 	 * check that the child blocks are no larger than the parent block.
12910Sstevel@tonic-gate 	 */
12920Sstevel@tonic-gate 	probe = p->left;
12930Sstevel@tonic-gate 	if (probe != NIL) {
12940Sstevel@tonic-gate 		chkhdr(probe);
12950Sstevel@tonic-gate 		db = probe->block;
12960Sstevel@tonic-gate 		chkblk(db);
12970Sstevel@tonic-gate 		if (probe->size > p->size)	/* child larger than parent */
12980Sstevel@tonic-gate 			return 0;
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 	probe = p->right;
13010Sstevel@tonic-gate 	if (probe != NIL) {
13020Sstevel@tonic-gate 		chkhdr(probe);
13030Sstevel@tonic-gate 		db = probe->block;
13040Sstevel@tonic-gate 		chkblk(db);
13050Sstevel@tonic-gate 		if (probe->size > p->size)	/* child larger than parent */
13060Sstevel@tonic-gate 			return 0;
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * test data addresses in the left subtree,
13100Sstevel@tonic-gate 	 * starting at the left subroot and probing to
13110Sstevel@tonic-gate 	 * the right.  All data addresses must be < p->block.
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	probe = p->left;
13140Sstevel@tonic-gate 	while (probe != NIL) {
13150Sstevel@tonic-gate 		chkhdr(probe);
13160Sstevel@tonic-gate 		db = probe->block;
13170Sstevel@tonic-gate 		chkblk(db);
13180Sstevel@tonic-gate 		if ( nextblk(db, probe->size) >= pdb )	/* overlap */
13190Sstevel@tonic-gate 			return 0;
13200Sstevel@tonic-gate 		probe = probe->right;
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * test data addresses in the right subtree,
13240Sstevel@tonic-gate 	 * starting at the right subroot and probing to
13250Sstevel@tonic-gate 	 * the left.  All addresses must be > nextblk(p->block).
13260Sstevel@tonic-gate 	 */
13270Sstevel@tonic-gate 	pdb = nextblk(pdb, p->size);
13280Sstevel@tonic-gate 	probe = p->right;
13290Sstevel@tonic-gate 	while (probe != NIL) {
13300Sstevel@tonic-gate 		chkhdr(probe);
13310Sstevel@tonic-gate 		db = probe->block;
13320Sstevel@tonic-gate 		chkblk(db);
13330Sstevel@tonic-gate 		if (db == NULL || db <= pdb)		/* overlap */
13340Sstevel@tonic-gate 			return 0;
13350Sstevel@tonic-gate 		probe = probe->left;
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 	return (cartesian(p->left) && cartesian(p->right));
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate  * malloc_verify()
13420Sstevel@tonic-gate  *
13430Sstevel@tonic-gate  * This is a verification routine.  It walks through all blocks
13440Sstevel@tonic-gate  * in the heap (both free and busy) and checks for bad blocks.
13450Sstevel@tonic-gate  * malloc_verify returns 1 if the heap contains no detectably bad
13460Sstevel@tonic-gate  * blocks; otherwise it returns 0.
13470Sstevel@tonic-gate  */
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate int
malloc_verify(void)1350*722Smuffin malloc_verify(void)
13510Sstevel@tonic-gate {
1352*722Smuffin 	int	maxsize;
1353*722Smuffin 	int	hdrsize;
1354*722Smuffin 	int	size;
1355*722Smuffin 	Dblk	p;
13560Sstevel@tonic-gate 	uint	lb,ub;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	extern  char	end[];
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	if (_lbound == NULL)	/* no allocation yet */
13610Sstevel@tonic-gate 		return 1;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	/*
13640Sstevel@tonic-gate 	 * first check heap bounds pointers
13650Sstevel@tonic-gate 	 */
13660Sstevel@tonic-gate 	lb = (uint)end;
13670Sstevel@tonic-gate 	ub = (uint)sbrk(0);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	if ((uint)_lbound < lb || (uint)_lbound > ub) {
13700Sstevel@tonic-gate 		error("malloc_verify: illegal heap lower bound (%#x)\n",
13710Sstevel@tonic-gate 			_lbound);
13720Sstevel@tonic-gate 		return 0;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	if ((uint)_ubound < lb || (uint)_ubound > ub) {
13750Sstevel@tonic-gate 		error("malloc_verify: illegal heap upper bound (%#x)\n",
13760Sstevel@tonic-gate 			_ubound);
13770Sstevel@tonic-gate 		return 0;
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 	maxsize = heapsize();
13800Sstevel@tonic-gate 	p = (Dblk)_lbound;
13810Sstevel@tonic-gate 	while (p < (Dblk) _ubound) {
13820Sstevel@tonic-gate 		size = p->size;
13830Sstevel@tonic-gate 		if ( (size) < SMALLEST_BLK
13840Sstevel@tonic-gate 			|| (size) & (ALIGNSIZ-1)
13850Sstevel@tonic-gate 			|| (size) > heapsize()
13860Sstevel@tonic-gate 			|| ((char*)(p))+(size) > _ubound ) {
13870Sstevel@tonic-gate 			error("malloc_verify: bad block size (%d) at %#x\n",
13880Sstevel@tonic-gate 				size, p);
13890Sstevel@tonic-gate 			return(0);		/* Badness */
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 		p = nextblk(p, size);
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 	if (p > (Dblk) _ubound) {
13940Sstevel@tonic-gate 		error("malloc_verify: heap corrupted\n");
13950Sstevel@tonic-gate 		return(0);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 	if (!cartesian(_root)){
13980Sstevel@tonic-gate 		error("malloc_verify: free space tree corrupted\n");
13990Sstevel@tonic-gate 		return(0);
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate 	return(1);
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate /*
14050Sstevel@tonic-gate  * The following is a kludge to avoid dependency on stdio, which
14060Sstevel@tonic-gate  * uses malloc() and free(), one of which probably got us here in
14070Sstevel@tonic-gate  * the first place.
14080Sstevel@tonic-gate  */
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate #define putchar(c) (*buf++ = (c))
14110Sstevel@tonic-gate extern	int	fileno();	/*bletch*/
14120Sstevel@tonic-gate #define stderr 2		/*bletch*/
14130Sstevel@tonic-gate #define	LBUFSIZ	256
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate static	char	stderrbuf[LBUFSIZ];
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate /*
14180Sstevel@tonic-gate  * Error routine.
14190Sstevel@tonic-gate  * If debug_level == 0, does nothing except set errno = EINVAL.
14200Sstevel@tonic-gate  * Otherwise, prints an error message to stderr and generates a
14210Sstevel@tonic-gate  * core image.
14220Sstevel@tonic-gate  */
1423*722Smuffin static void
error(char * fmt,...)1424*722Smuffin error(char *fmt, ...)
14250Sstevel@tonic-gate {
1426*722Smuffin 	static int n = 0; /* prevents infinite recursion when using stdio */
1427*722Smuffin 	int nbytes;
1428*722Smuffin 	va_list ap;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	errno = EINVAL;
14310Sstevel@tonic-gate 	if (debug_level == 0)
14320Sstevel@tonic-gate 		return;
14330Sstevel@tonic-gate 	if (!n++) {
1434*722Smuffin 		va_start(ap, fmt);
1435*722Smuffin 		nbytes = vsprintf(stderrbuf, fmt, ap);
1436*722Smuffin 		va_end(ap);
14370Sstevel@tonic-gate 		stderrbuf[nbytes++] = '\n';
14380Sstevel@tonic-gate 		stderrbuf[nbytes] = '\0';
14390Sstevel@tonic-gate 		write(fileno(stderr), stderrbuf, nbytes);
14400Sstevel@tonic-gate 	}
14410Sstevel@tonic-gate 	abort();
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
1444*722Smuffin #endif	/* DEBUG	<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
1445