xref: /onnv-gate/usr/src/lib/watchmalloc/common/mallint.h (revision 3866:a61ffaa37ee1)
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
52658Sraf  * Common Development and Distribution License (the "License").
62658Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
212658Sraf 
220Sstevel@tonic-gate /*
23*3866Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
242658Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <memory.h>
380Sstevel@tonic-gate #include <thread.h>
39*3866Sraf #include <pthread.h>
400Sstevel@tonic-gate #include <synch.h>
410Sstevel@tonic-gate #include <procfs.h>
420Sstevel@tonic-gate #include <limits.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /* debugging macros */
470Sstevel@tonic-gate #ifdef	DEBUG
480Sstevel@tonic-gate #define	ASSERT(p)	((void) ((p) || (abort(), 0)))
490Sstevel@tonic-gate #define	COUNT(n)	((void) n++)
500Sstevel@tonic-gate static int		nmalloc, nrealloc, nfree;
510Sstevel@tonic-gate #else
520Sstevel@tonic-gate #define	ASSERT(p)	((void)0)
530Sstevel@tonic-gate #define	COUNT(n)	((void)0)
540Sstevel@tonic-gate #endif /* DEBUG */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* for conveniences */
570Sstevel@tonic-gate #ifndef NULL
580Sstevel@tonic-gate #define	NULL		(0)
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	WORDSIZE	(sizeof (WORD))
620Sstevel@tonic-gate #define	MINSIZE		(sizeof (TREE) - sizeof (WORD))
630Sstevel@tonic-gate #define	ROUND(s)	if ((s)%WORDSIZE) (s) += (WORDSIZE - ((s)%WORDSIZE))
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * All of our allocations will be aligned on the least multiple of 4,
670Sstevel@tonic-gate  * at least, so the two low order bits are guaranteed to be available.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate #ifdef _LP64
700Sstevel@tonic-gate #define	ALIGN		16
710Sstevel@tonic-gate #else
720Sstevel@tonic-gate #define	ALIGN		8
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /* the proto-word; size must be ALIGN bytes */
760Sstevel@tonic-gate typedef union _w_ {
770Sstevel@tonic-gate 	size_t		w_i;		/* an unsigned int */
780Sstevel@tonic-gate 	struct _t_	*w_p[2];	/* two pointers */
790Sstevel@tonic-gate } WORD;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* structure of a node in the free tree */
820Sstevel@tonic-gate typedef struct _t_ {
830Sstevel@tonic-gate 	WORD	t_s;	/* size of this element */
840Sstevel@tonic-gate 	WORD	t_p;	/* parent node */
850Sstevel@tonic-gate 	WORD	t_l;	/* left child */
860Sstevel@tonic-gate 	WORD	t_r;	/* right child */
870Sstevel@tonic-gate 	WORD	t_n;	/* next in link list */
880Sstevel@tonic-gate 	WORD	t_d;	/* dummy to reserve space for self-pointer */
890Sstevel@tonic-gate } TREE;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /* usable # of bytes in the block */
920Sstevel@tonic-gate #define	SIZE(b)		(((b)->t_s).w_i)
930Sstevel@tonic-gate #define	RSIZE(b)	(((b)->t_s).w_i & ~BITS01)
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /* free tree pointers */
960Sstevel@tonic-gate #define	PARENT(b)	(((b)->t_p).w_p[0])
970Sstevel@tonic-gate #define	LEFT(b)		(((b)->t_l).w_p[0])
980Sstevel@tonic-gate #define	RIGHT(b)	(((b)->t_r).w_p[0])
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /* forward link in lists of small blocks */
1010Sstevel@tonic-gate #define	AFTER(b)	(((b)->t_p).w_p[0])
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* forward and backward links for lists in the tree */
1040Sstevel@tonic-gate #define	LINKFOR(b)	(((b)->t_n).w_p[0])
1050Sstevel@tonic-gate #define	LINKBAK(b)	(((b)->t_p).w_p[0])
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /* set/test indicator if a block is in the tree or in a list */
1080Sstevel@tonic-gate #define	SETNOTREE(b)	(LEFT(b) = (TREE *)(-1))
1090Sstevel@tonic-gate #define	ISNOTREE(b)	(LEFT(b) == (TREE *)(-1))
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* functions to get information on a block */
1120Sstevel@tonic-gate #define	DATA(b)		(((char *)(b)) + WORDSIZE)
1130Sstevel@tonic-gate #define	BLOCK(d)	((TREE *)(((char *)(d)) - WORDSIZE))
1140Sstevel@tonic-gate #define	SELFP(b)	(&(NEXT(b)->t_s.w_p[1]))
1150Sstevel@tonic-gate #define	LAST(b)		((b)->t_s.w_p[1])
1160Sstevel@tonic-gate #define	NEXT(b)		((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE))
1170Sstevel@tonic-gate #define	BOTTOM(b)	((DATA(b) + RSIZE(b) + WORDSIZE) == Baddr)
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /* functions to set and test the lowest two bits of a word */
1200Sstevel@tonic-gate #define	BIT0		(01)		/* ...001 */
1210Sstevel@tonic-gate #define	BIT1		(02)		/* ...010 */
1220Sstevel@tonic-gate #define	BITS01		(03)		/* ...011 */
1230Sstevel@tonic-gate #define	ISBIT0(w)	((w) & BIT0)	/* Is busy? */
1240Sstevel@tonic-gate #define	ISBIT1(w)	((w) & BIT1)	/* Is the preceding free? */
1250Sstevel@tonic-gate #define	SETBIT0(w)	((w) |= BIT0)	/* Block is busy */
1260Sstevel@tonic-gate #define	SETBIT1(w)	((w) |= BIT1)	/* The preceding is free */
1270Sstevel@tonic-gate #define	CLRBIT0(w)	((w) &= ~BIT0)	/* Clean bit0 */
1280Sstevel@tonic-gate #define	CLRBIT1(w)	((w) &= ~BIT1)	/* Clean bit1 */
1290Sstevel@tonic-gate #define	SETBITS01(w)	((w) |= BITS01)	/* Set bits 0 & 1 */
1300Sstevel@tonic-gate #define	CLRBITS01(w)	((w) &= ~BITS01) /* Clean bits 0 & 1 */
1310Sstevel@tonic-gate #define	SETOLD01(n, o)	((n) |= (BITS01 & (o)))
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* system call to get more memory */
1340Sstevel@tonic-gate #define	GETCORE		sbrk
1350Sstevel@tonic-gate #define	ERRCORE		((char *)(-1))
1360Sstevel@tonic-gate #define	CORESIZE	(1024*ALIGN)
1370Sstevel@tonic-gate #define	MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */
1380Sstevel@tonic-gate #define	MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */
1390Sstevel@tonic-gate #define	MAX_ALIGN	(1 + (size_t)SSIZE_MAX)
140