xref: /onnv-gate/usr/src/lib/libc/port/gen/memalign.c (revision 6812:febeba71273d)
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
52186Sraf  * Common Development and Distribution License (the "License").
62186Sraf  * 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  */
212186Sraf 
220Sstevel@tonic-gate /*
236515Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * 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 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*6812Sraf 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "mallint.h"
340Sstevel@tonic-gate #include "mtlib.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	_misaligned(p)		((unsigned)(p) & 3)
370Sstevel@tonic-gate 		/* 4-byte "word" alignment is considered ok in LP64 */
380Sstevel@tonic-gate #define	_nextblk(p, size)	((TREE *)((uintptr_t)(p) + (size)))
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * memalign(align, nbytes)
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * Description:
440Sstevel@tonic-gate  *	Returns a block of specified size on a specified alignment boundary.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Algorithm:
470Sstevel@tonic-gate  *	Malloc enough to ensure that a block can be aligned correctly.
480Sstevel@tonic-gate  *	Find the alignment point and return the fragments
490Sstevel@tonic-gate  *	before and after the block.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * Errors:
520Sstevel@tonic-gate  *	Returns NULL and sets errno as follows:
530Sstevel@tonic-gate  *	[EINVAL]
540Sstevel@tonic-gate  *		if nbytes = 0,
550Sstevel@tonic-gate  *		or if alignment is misaligned,
560Sstevel@tonic-gate  *		or if the heap has been detectably corrupted.
570Sstevel@tonic-gate  *	[ENOMEM]
580Sstevel@tonic-gate  *		if the requested memory could not be allocated.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate void *
memalign(size_t align,size_t nbytes)620Sstevel@tonic-gate memalign(size_t align, size_t nbytes)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	size_t	 reqsize;	/* Num of bytes to get from malloc() */
650Sstevel@tonic-gate 	TREE	*p;		/* Ptr returned from malloc() */
660Sstevel@tonic-gate 	TREE	*blk;		/* For addressing fragment blocks */
670Sstevel@tonic-gate 	size_t	blksize;	/* Current (shrinking) block size */
680Sstevel@tonic-gate 	TREE	*alignedp;	/* Ptr to properly aligned boundary */
690Sstevel@tonic-gate 	TREE	*aligned_blk;	/* The block to be returned */
700Sstevel@tonic-gate 	size_t	frag_size;	/* size of fragments fore and aft */
710Sstevel@tonic-gate 	size_t	 x;
720Sstevel@tonic-gate 
736515Sraf 	if (!primary_link_map) {
746515Sraf 		errno = ENOTSUP;
756515Sraf 		return (NULL);
766515Sraf 	}
776515Sraf 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * check for valid size and alignment parameters
800Sstevel@tonic-gate 	 * MAX_ALIGN check prevents overflow in later calculation.
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	if (nbytes == 0 || _misaligned(align) || align == 0 ||
830Sstevel@tonic-gate 	    align > MAX_ALIGN) {
840Sstevel@tonic-gate 		errno = EINVAL;
850Sstevel@tonic-gate 		return (NULL);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 * Malloc enough memory to guarantee that the result can be
900Sstevel@tonic-gate 	 * aligned correctly. The worst case is when malloc returns
910Sstevel@tonic-gate 	 * a block so close to the next alignment boundary that a
920Sstevel@tonic-gate 	 * fragment of minimum size cannot be created.  In order to
930Sstevel@tonic-gate 	 * make sure we can handle this, we need to force the
940Sstevel@tonic-gate 	 * alignment to be at least as large as the minimum frag size
950Sstevel@tonic-gate 	 * (MINSIZE + WORDSIZE).
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* check for size that could overflow calculations */
990Sstevel@tonic-gate 	if (nbytes > MAX_MALLOC) {
1000Sstevel@tonic-gate 		errno = ENOMEM;
1010Sstevel@tonic-gate 		return (NULL);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	ROUND(nbytes);
1040Sstevel@tonic-gate 	if (nbytes < MINSIZE)
1050Sstevel@tonic-gate 		nbytes = MINSIZE;
1060Sstevel@tonic-gate 	ROUND(align);
1070Sstevel@tonic-gate 	while (align < MINSIZE + WORDSIZE)
1080Sstevel@tonic-gate 		align <<= 1;
1090Sstevel@tonic-gate 	reqsize = nbytes + align + (MINSIZE + WORDSIZE);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/* check for overflow */
1120Sstevel@tonic-gate 	if (reqsize < nbytes) {
1130Sstevel@tonic-gate 		errno = ENOMEM;
1140Sstevel@tonic-gate 		return (NULL);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	p = (TREE *)malloc(reqsize);
1180Sstevel@tonic-gate 	if (p == (TREE *)NULL) {
1190Sstevel@tonic-gate 		/* malloc sets errno */
1200Sstevel@tonic-gate 		return (NULL);
1210Sstevel@tonic-gate 	}
1226515Sraf 	(void) mutex_lock(&libc_malloc_lock);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * get size of the entire block (overhead and all)
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	blk = BLOCK(p);			/* back up to get length word */
1280Sstevel@tonic-gate 	blksize = SIZE(blk);
1290Sstevel@tonic-gate 	CLRBITS01(blksize);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * locate the proper alignment boundary within the block.
1330Sstevel@tonic-gate 	 */
1340Sstevel@tonic-gate 	x = (size_t)p;
1350Sstevel@tonic-gate 	if (x % align != 0)
1360Sstevel@tonic-gate 		x += align - (x % align);
1370Sstevel@tonic-gate 	alignedp = (TREE *)x;
1380Sstevel@tonic-gate 	aligned_blk = BLOCK(alignedp);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Check out the space to the left of the alignment
1420Sstevel@tonic-gate 	 * boundary, and split off a fragment if necessary.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	frag_size = (size_t)aligned_blk - (size_t)blk;
1450Sstevel@tonic-gate 	if (frag_size != 0) {
1460Sstevel@tonic-gate 		/*
1470Sstevel@tonic-gate 		 * Create a fragment to the left of the aligned block.
1480Sstevel@tonic-gate 		 */
1490Sstevel@tonic-gate 		if (frag_size < MINSIZE + WORDSIZE) {
1500Sstevel@tonic-gate 			/*
1510Sstevel@tonic-gate 			 * Not enough space. So make the split
1520Sstevel@tonic-gate 			 * at the other end of the alignment unit.
1530Sstevel@tonic-gate 			 * We know this yields enough space, because
1540Sstevel@tonic-gate 			 * we forced align >= MINSIZE + WORDSIZE above.
1550Sstevel@tonic-gate 			 */
1560Sstevel@tonic-gate 			frag_size += align;
1570Sstevel@tonic-gate 			aligned_blk = _nextblk(aligned_blk, align);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		blksize -= frag_size;
1600Sstevel@tonic-gate 		SIZE(aligned_blk) = blksize | BIT0;
1610Sstevel@tonic-gate 		frag_size -= WORDSIZE;
1620Sstevel@tonic-gate 		SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk));
1630Sstevel@tonic-gate 		_free_unlocked(DATA(blk));
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 * Is there a (sufficiently large) fragment to the
1680Sstevel@tonic-gate 	 * right of the aligned block?
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	frag_size = blksize - nbytes;
1710Sstevel@tonic-gate 	if (frag_size >= MINSIZE + WORDSIZE) {
1720Sstevel@tonic-gate 		/*
1730Sstevel@tonic-gate 		 * split and free a fragment on the right
1740Sstevel@tonic-gate 		 */
1750Sstevel@tonic-gate 		blksize = SIZE(aligned_blk);
1760Sstevel@tonic-gate 		SIZE(aligned_blk) = nbytes;
1770Sstevel@tonic-gate 		blk = NEXT(aligned_blk);
1780Sstevel@tonic-gate 		SETOLD01(SIZE(aligned_blk), blksize);
1790Sstevel@tonic-gate 		frag_size -= WORDSIZE;
1800Sstevel@tonic-gate 		SIZE(blk) = frag_size | BIT0;
1810Sstevel@tonic-gate 		_free_unlocked(DATA(blk));
1820Sstevel@tonic-gate 	}
1836515Sraf 	(void) mutex_unlock(&libc_malloc_lock);
1840Sstevel@tonic-gate 	return (DATA(aligned_blk));
1850Sstevel@tonic-gate }
186