xref: /titanic_50/usr/src/lib/libc/port/gen/memalign.c (revision dcdfe824b3dff2df12578b936adf1daf000aa129)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52333b8a2Sraf  * Common Development and Distribution License (the "License").
62333b8a2Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212333b8a2Sraf 
227c478bd9Sstevel@tonic-gate /*
238cd45542Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*dcdfe824SRobert Mustacchi  * Copyright 2016 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317257d1b4Sraf #include "lint.h"
327c478bd9Sstevel@tonic-gate #include "mallint.h"
337c478bd9Sstevel@tonic-gate #include "mtlib.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define	_misaligned(p)		((unsigned)(p) & 3)
367c478bd9Sstevel@tonic-gate 		/* 4-byte "word" alignment is considered ok in LP64 */
377c478bd9Sstevel@tonic-gate #define	_nextblk(p, size)	((TREE *)((uintptr_t)(p) + (size)))
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * memalign(align, nbytes)
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * Description:
437c478bd9Sstevel@tonic-gate  *	Returns a block of specified size on a specified alignment boundary.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * Algorithm:
467c478bd9Sstevel@tonic-gate  *	Malloc enough to ensure that a block can be aligned correctly.
477c478bd9Sstevel@tonic-gate  *	Find the alignment point and return the fragments
487c478bd9Sstevel@tonic-gate  *	before and after the block.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Errors:
517c478bd9Sstevel@tonic-gate  *	Returns NULL and sets errno as follows:
527c478bd9Sstevel@tonic-gate  *	[EINVAL]
537c478bd9Sstevel@tonic-gate  *		if nbytes = 0,
547c478bd9Sstevel@tonic-gate  *		or if alignment is misaligned,
557c478bd9Sstevel@tonic-gate  *		or if the heap has been detectably corrupted.
567c478bd9Sstevel@tonic-gate  *	[ENOMEM]
577c478bd9Sstevel@tonic-gate  *		if the requested memory could not be allocated.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void *
memalign(size_t align,size_t nbytes)617c478bd9Sstevel@tonic-gate memalign(size_t align, size_t nbytes)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	size_t	 reqsize;	/* Num of bytes to get from malloc() */
647c478bd9Sstevel@tonic-gate 	TREE	*p;		/* Ptr returned from malloc() */
657c478bd9Sstevel@tonic-gate 	TREE	*blk;		/* For addressing fragment blocks */
667c478bd9Sstevel@tonic-gate 	size_t	blksize;	/* Current (shrinking) block size */
677c478bd9Sstevel@tonic-gate 	TREE	*alignedp;	/* Ptr to properly aligned boundary */
687c478bd9Sstevel@tonic-gate 	TREE	*aligned_blk;	/* The block to be returned */
697c478bd9Sstevel@tonic-gate 	size_t	frag_size;	/* size of fragments fore and aft */
707c478bd9Sstevel@tonic-gate 	size_t	 x;
717c478bd9Sstevel@tonic-gate 
728cd45542Sraf 	if (!primary_link_map) {
738cd45542Sraf 		errno = ENOTSUP;
748cd45542Sraf 		return (NULL);
758cd45542Sraf 	}
768cd45542Sraf 
777c478bd9Sstevel@tonic-gate 	/*
787c478bd9Sstevel@tonic-gate 	 * check for valid size and alignment parameters
797c478bd9Sstevel@tonic-gate 	 * MAX_ALIGN check prevents overflow in later calculation.
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	if (nbytes == 0 || _misaligned(align) || align == 0 ||
827c478bd9Sstevel@tonic-gate 	    align > MAX_ALIGN) {
837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
847c478bd9Sstevel@tonic-gate 		return (NULL);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * Malloc enough memory to guarantee that the result can be
897c478bd9Sstevel@tonic-gate 	 * aligned correctly. The worst case is when malloc returns
907c478bd9Sstevel@tonic-gate 	 * a block so close to the next alignment boundary that a
917c478bd9Sstevel@tonic-gate 	 * fragment of minimum size cannot be created.  In order to
927c478bd9Sstevel@tonic-gate 	 * make sure we can handle this, we need to force the
937c478bd9Sstevel@tonic-gate 	 * alignment to be at least as large as the minimum frag size
947c478bd9Sstevel@tonic-gate 	 * (MINSIZE + WORDSIZE).
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* check for size that could overflow calculations */
987c478bd9Sstevel@tonic-gate 	if (nbytes > MAX_MALLOC) {
997c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1007c478bd9Sstevel@tonic-gate 		return (NULL);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	ROUND(nbytes);
1037c478bd9Sstevel@tonic-gate 	if (nbytes < MINSIZE)
1047c478bd9Sstevel@tonic-gate 		nbytes = MINSIZE;
1057c478bd9Sstevel@tonic-gate 	ROUND(align);
1067c478bd9Sstevel@tonic-gate 	while (align < MINSIZE + WORDSIZE)
1077c478bd9Sstevel@tonic-gate 		align <<= 1;
1087c478bd9Sstevel@tonic-gate 	reqsize = nbytes + align + (MINSIZE + WORDSIZE);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* check for overflow */
1117c478bd9Sstevel@tonic-gate 	if (reqsize < nbytes) {
1127c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1137c478bd9Sstevel@tonic-gate 		return (NULL);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	p = (TREE *)malloc(reqsize);
1177c478bd9Sstevel@tonic-gate 	if (p == (TREE *)NULL) {
1187c478bd9Sstevel@tonic-gate 		/* malloc sets errno */
1197c478bd9Sstevel@tonic-gate 		return (NULL);
1207c478bd9Sstevel@tonic-gate 	}
1218cd45542Sraf 	(void) mutex_lock(&libc_malloc_lock);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * get size of the entire block (overhead and all)
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	blk = BLOCK(p);			/* back up to get length word */
1277c478bd9Sstevel@tonic-gate 	blksize = SIZE(blk);
1287c478bd9Sstevel@tonic-gate 	CLRBITS01(blksize);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * locate the proper alignment boundary within the block.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	x = (size_t)p;
1347c478bd9Sstevel@tonic-gate 	if (x % align != 0)
1357c478bd9Sstevel@tonic-gate 		x += align - (x % align);
1367c478bd9Sstevel@tonic-gate 	alignedp = (TREE *)x;
1377c478bd9Sstevel@tonic-gate 	aligned_blk = BLOCK(alignedp);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * Check out the space to the left of the alignment
1417c478bd9Sstevel@tonic-gate 	 * boundary, and split off a fragment if necessary.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	frag_size = (size_t)aligned_blk - (size_t)blk;
1447c478bd9Sstevel@tonic-gate 	if (frag_size != 0) {
1457c478bd9Sstevel@tonic-gate 		/*
1467c478bd9Sstevel@tonic-gate 		 * Create a fragment to the left of the aligned block.
1477c478bd9Sstevel@tonic-gate 		 */
1487c478bd9Sstevel@tonic-gate 		if (frag_size < MINSIZE + WORDSIZE) {
1497c478bd9Sstevel@tonic-gate 			/*
1507c478bd9Sstevel@tonic-gate 			 * Not enough space. So make the split
1517c478bd9Sstevel@tonic-gate 			 * at the other end of the alignment unit.
1527c478bd9Sstevel@tonic-gate 			 * We know this yields enough space, because
1537c478bd9Sstevel@tonic-gate 			 * we forced align >= MINSIZE + WORDSIZE above.
1547c478bd9Sstevel@tonic-gate 			 */
1557c478bd9Sstevel@tonic-gate 			frag_size += align;
1567c478bd9Sstevel@tonic-gate 			aligned_blk = _nextblk(aligned_blk, align);
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 		blksize -= frag_size;
1597c478bd9Sstevel@tonic-gate 		SIZE(aligned_blk) = blksize | BIT0;
1607c478bd9Sstevel@tonic-gate 		frag_size -= WORDSIZE;
1617c478bd9Sstevel@tonic-gate 		SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk));
1627c478bd9Sstevel@tonic-gate 		_free_unlocked(DATA(blk));
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * Is there a (sufficiently large) fragment to the
1677c478bd9Sstevel@tonic-gate 	 * right of the aligned block?
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	frag_size = blksize - nbytes;
1707c478bd9Sstevel@tonic-gate 	if (frag_size >= MINSIZE + WORDSIZE) {
1717c478bd9Sstevel@tonic-gate 		/*
1727c478bd9Sstevel@tonic-gate 		 * split and free a fragment on the right
1737c478bd9Sstevel@tonic-gate 		 */
1747c478bd9Sstevel@tonic-gate 		blksize = SIZE(aligned_blk);
1757c478bd9Sstevel@tonic-gate 		SIZE(aligned_blk) = nbytes;
1767c478bd9Sstevel@tonic-gate 		blk = NEXT(aligned_blk);
1777c478bd9Sstevel@tonic-gate 		SETOLD01(SIZE(aligned_blk), blksize);
1787c478bd9Sstevel@tonic-gate 		frag_size -= WORDSIZE;
1797c478bd9Sstevel@tonic-gate 		SIZE(blk) = frag_size | BIT0;
1807c478bd9Sstevel@tonic-gate 		_free_unlocked(DATA(blk));
1817c478bd9Sstevel@tonic-gate 	}
1828cd45542Sraf 	(void) mutex_unlock(&libc_malloc_lock);
1837c478bd9Sstevel@tonic-gate 	return (DATA(aligned_blk));
1847c478bd9Sstevel@tonic-gate }
185*dcdfe824SRobert Mustacchi 
186*dcdfe824SRobert Mustacchi /*
187*dcdfe824SRobert Mustacchi  * This is the ISO/IEC C11 version of memalign. We have kept it as a separate
188*dcdfe824SRobert Mustacchi  * function, but it is basically the same thing. Note that this is implemented
189*dcdfe824SRobert Mustacchi  * this way to make life easier to libraries which already interpose on
190*dcdfe824SRobert Mustacchi  * memalign.
191*dcdfe824SRobert Mustacchi  */
192*dcdfe824SRobert Mustacchi void *
aligned_alloc(size_t align,size_t size)193*dcdfe824SRobert Mustacchi aligned_alloc(size_t align, size_t size)
194*dcdfe824SRobert Mustacchi {
195*dcdfe824SRobert Mustacchi 	return (memalign(align, size));
196*dcdfe824SRobert Mustacchi }
197