17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
51d53067raf * Common Development and Distribution License (the "License").
61d53067raf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate
227c478bdstevel@tonic-gate/*
231d53067raf * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241d53067raf * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
271d53067raf/*	Copyright (c) 1988 AT&T	*/
281d53067raf/*	  All Rights Reserved  	*/
291d53067raf
307c478bdstevel@tonic-gate#ifndef _MALLINT_H
317c478bdstevel@tonic-gate#define	_MALLINT_H
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
347c478bdstevel@tonic-gate
357c478bdstevel@tonic-gate/*	From:	SVr4.0	libmalloc:mallint.h	1.3		*/
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate/*
387c478bdstevel@tonic-gate * number of bytes to align to  (must be at least 4, because lower 2 bits
397c478bdstevel@tonic-gate * are used for flags
407c478bdstevel@tonic-gate *
417c478bdstevel@tonic-gate * header and code assume ALIGNSZ is exact multiple of sizeof (struct header *)
427c478bdstevel@tonic-gate * several places assume sizeof (long) == sizeof (struct holdblk *)
437c478bdstevel@tonic-gate */
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#include <sys/types.h>
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate#ifdef	__cplusplus
487c478bdstevel@tonic-gateextern "C" {
497c478bdstevel@tonic-gate#endif
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate#ifdef _LP64
527c478bdstevel@tonic-gate#define	ALIGNSZ	16
537c478bdstevel@tonic-gate#else
547c478bdstevel@tonic-gate#define	ALIGNSZ	8
557c478bdstevel@tonic-gate#endif
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate/*
587c478bdstevel@tonic-gate *	template for the header
597c478bdstevel@tonic-gate */
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gatestruct header {
627c478bdstevel@tonic-gate	struct header *nextblk;
637c478bdstevel@tonic-gate	struct header *nextfree;
647c478bdstevel@tonic-gate	struct header *prevfree;
657c478bdstevel@tonic-gate	struct header *__Pad;	/* pad to a multiple of ALIGNSZ */
667c478bdstevel@tonic-gate};
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate/*
697c478bdstevel@tonic-gate *	template for a small block
707c478bdstevel@tonic-gate */
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gatestruct lblk  {
737c478bdstevel@tonic-gate	union {
747c478bdstevel@tonic-gate		/*
757c478bdstevel@tonic-gate		 * the next free little block in this holding block.
767c478bdstevel@tonic-gate		 * This field is used when the block is free
777c478bdstevel@tonic-gate		 */
787c478bdstevel@tonic-gate		struct lblk *nextfree;
797c478bdstevel@tonic-gate		/*
807c478bdstevel@tonic-gate		 * the holding block containing this little block.
817c478bdstevel@tonic-gate		 * This field is used when the block is allocated
827c478bdstevel@tonic-gate		 */
837c478bdstevel@tonic-gate		struct holdblk *holder;
847c478bdstevel@tonic-gate		/*
857c478bdstevel@tonic-gate		 * Insure over head is multiple of ALIGNSZ
867c478bdstevel@tonic-gate		 * assumes  ALIGNSZ >= sizeof pointer
877c478bdstevel@tonic-gate		 */
887c478bdstevel@tonic-gate		char __Overhead[ALIGNSZ];
897c478bdstevel@tonic-gate	}  header;
907c478bdstevel@tonic-gate	/* There is no telling how big this field really is.  */
917c478bdstevel@tonic-gate	/* This must be on a ALIGNSZ  boundary */
927c478bdstevel@tonic-gate	char byte;
937c478bdstevel@tonic-gate};
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate/*
967c478bdstevel@tonic-gate *	template for holding block
977c478bdstevel@tonic-gate */
987c478bdstevel@tonic-gatestruct holdblk {
997c478bdstevel@tonic-gate	struct holdblk *nexthblk;   /* next holding block */
1007c478bdstevel@tonic-gate	struct holdblk *prevhblk;   /* previous holding block */
1017c478bdstevel@tonic-gate	struct lblk *lfreeq;	/* head of free queue within block */
1027c478bdstevel@tonic-gate	struct lblk *unused;	/* pointer to 1st little block never used */
1037c478bdstevel@tonic-gate	long blksz;		/* size of little blocks contained */
1047c478bdstevel@tonic-gate	struct lblk *__Pad;	/* pad to a multiple of ALIGNSZ */
1057c478bdstevel@tonic-gate	char space[1];		/* start of space to allocate. */
1067c478bdstevel@tonic-gate				/* This must be on a ALIGNSZ boundary */
1077c478bdstevel@tonic-gate};
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate/*
1107c478bdstevel@tonic-gate *	 The following manipulate the free queue
1117c478bdstevel@tonic-gate *
1127c478bdstevel@tonic-gate *		DELFREEQ will remove x from the free queue
1137c478bdstevel@tonic-gate *		ADDFREEQ will add an element to the head
1147c478bdstevel@tonic-gate *			 of the free queue.
1157c478bdstevel@tonic-gate *		MOVEHEAD will move the free pointers so that
1167c478bdstevel@tonic-gate *			 x is at the front of the queue
1177c478bdstevel@tonic-gate */
1187c478bdstevel@tonic-gate#define	ADDFREEQ(x)	(x)->prevfree = &(freeptr[0]);\
1197c478bdstevel@tonic-gate				(x)->nextfree = freeptr[0].nextfree;\
1207c478bdstevel@tonic-gate				freeptr[0].nextfree->prevfree = (x);\
1217c478bdstevel@tonic-gate				freeptr[0].nextfree = (x);\
1227c478bdstevel@tonic-gate				assert((x)->nextfree != (x));\
1237c478bdstevel@tonic-gate				assert((x)->prevfree != (x));
1247c478bdstevel@tonic-gate#define	DELFREEQ(x)	(x)->prevfree->nextfree = (x)->nextfree;\
1257c478bdstevel@tonic-gate				(x)->nextfree->prevfree = (x)->prevfree;\
1267c478bdstevel@tonic-gate				assert((x)->nextfree != (x));\
1277c478bdstevel@tonic-gate				assert((x)->prevfree != (x));
1287c478bdstevel@tonic-gate#define	MOVEHEAD(x)	freeptr[1].prevfree->nextfree = freeptr[0].nextfree;\
1297c478bdstevel@tonic-gate				freeptr[0].nextfree->prevfree = \
1307c478bdstevel@tonic-gate				    freeptr[1].prevfree;\
1317c478bdstevel@tonic-gate				(x)->prevfree->nextfree = &(freeptr[1]);\
1327c478bdstevel@tonic-gate				freeptr[1].prevfree = (x)->prevfree;\
1337c478bdstevel@tonic-gate				(x)->prevfree = &(freeptr[0]);\
1347c478bdstevel@tonic-gate				freeptr[0].nextfree = (x);\
1357c478bdstevel@tonic-gate				assert((x)->nextfree != (x));\
1367c478bdstevel@tonic-gate				assert((x)->prevfree != (x));
1377c478bdstevel@tonic-gate/*
1387c478bdstevel@tonic-gate *	The following manipulate the busy flag
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gate#define	BUSY	1L
1417c478bdstevel@tonic-gate#define	SETBUSY(x)	((struct header *)((long)(x) | BUSY))
1427c478bdstevel@tonic-gate#define	CLRBUSY(x)	((struct header *)((long)(x) & ~BUSY))
1437c478bdstevel@tonic-gate#define	TESTBUSY(x)	((long)(x) & BUSY)
1447c478bdstevel@tonic-gate/*
1457c478bdstevel@tonic-gate *	The following manipulate the small block flag
1467c478bdstevel@tonic-gate */
1477c478bdstevel@tonic-gate#define	SMAL	2L
1487c478bdstevel@tonic-gate#define	SETSMAL(x)	((struct lblk *)((long)(x) | SMAL))
1497c478bdstevel@tonic-gate#define	CLRSMAL(x)	((struct lblk *)((long)(x) & ~SMAL))
1507c478bdstevel@tonic-gate#define	TESTSMAL(x)	((long)(x) & SMAL)
1517c478bdstevel@tonic-gate/*
1527c478bdstevel@tonic-gate *	The following manipulate both flags.  They must be
1537c478bdstevel@tonic-gate *	type coerced
1547c478bdstevel@tonic-gate */
1557c478bdstevel@tonic-gate#define	SETALL(x)	((long)(x) | (SMAL | BUSY))
1567c478bdstevel@tonic-gate#define	CLRALL(x)	((long)(x) & ~(SMAL | BUSY))
1577c478bdstevel@tonic-gate/*
1587c478bdstevel@tonic-gate *	Other useful constants
1597c478bdstevel@tonic-gate */
1607c478bdstevel@tonic-gate#define	TRUE	1
1617c478bdstevel@tonic-gate#define	FALSE	0
1627c478bdstevel@tonic-gate#define	HEADSZ	sizeof (struct header)	/* size of unallocated block header */
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate/* MINHEAD is the minimum size of an allocated block header */
1657c478bdstevel@tonic-gate#define	MINHEAD	ALIGNSZ
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate/* min. block size must as big as HEADSZ */
1687c478bdstevel@tonic-gate#define	MINBLKSZ	HEADSZ
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate/* memory is gotten from sbrk in multiples of BLOCKSZ */
1717c478bdstevel@tonic-gate#define	BLOCKSZ		2048	/* ??? Too Small, ?? pagesize? */
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate#define	GROUND	(struct header *)0
1747c478bdstevel@tonic-gate#define	LGROUND	(struct lblk *)0
1757c478bdstevel@tonic-gate#define	HGROUND	(struct holdblk *)0	/* ground for the holding block queue */
1767c478bdstevel@tonic-gate#ifndef	NULL
1777c478bdstevel@tonic-gate#define	NULL	(char *)0
1787c478bdstevel@tonic-gate#endif
1797c478bdstevel@tonic-gate/*
1807c478bdstevel@tonic-gate *	Structures and constants describing the holding blocks
1817c478bdstevel@tonic-gate */
1827c478bdstevel@tonic-gate/* default number of small blocks per holding block */
1837c478bdstevel@tonic-gate#define	NUMLBLKS	100
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate/* size of a holding block with small blocks of size blksz */
1867c478bdstevel@tonic-gate#define	HOLDSZ(blksz)	\
1877c478bdstevel@tonic-gate	    (sizeof (struct holdblk) - sizeof (struct lblk *) + blksz*numlblks)
1887c478bdstevel@tonic-gate#define	FASTCT	6	/* number of blocks that can be allocated quickly */
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate/* default maximum size block for fast allocation */
1917c478bdstevel@tonic-gate/* assumes initial value of grain == ALIGNSZ */
1927c478bdstevel@tonic-gate#define	MAXFAST	ALIGNSZ*FASTCT
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate#ifdef	debug
1957c478bdstevel@tonic-gate#define	CHECKQ	checkq();
1967c478bdstevel@tonic-gate#else
1977c478bdstevel@tonic-gate#define	CHECKQ
1987c478bdstevel@tonic-gate#endif
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gate#ifdef	__cplusplus
2017c478bdstevel@tonic-gate}
2027c478bdstevel@tonic-gate#endif
2037c478bdstevel@tonic-gate
2047c478bdstevel@tonic-gate#endif	/* _MALLINT_H */
205