xref: /titanic_44/usr/src/lib/libc/port/gen/memalign.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 #include "synonyms.h"
33 #include "mallint.h"
34 #include "mtlib.h"
35 
36 #define	_misaligned(p)		((unsigned)(p) & 3)
37 		/* 4-byte "word" alignment is considered ok in LP64 */
38 #define	_nextblk(p, size)	((TREE *)((uintptr_t)(p) + (size)))
39 
40 /*
41  * memalign(align, nbytes)
42  *
43  * Description:
44  *	Returns a block of specified size on a specified alignment boundary.
45  *
46  * Algorithm:
47  *	Malloc enough to ensure that a block can be aligned correctly.
48  *	Find the alignment point and return the fragments
49  *	before and after the block.
50  *
51  * Errors:
52  *	Returns NULL and sets errno as follows:
53  *	[EINVAL]
54  *		if nbytes = 0,
55  *		or if alignment is misaligned,
56  *		or if the heap has been detectably corrupted.
57  *	[ENOMEM]
58  *		if the requested memory could not be allocated.
59  */
60 
61 void *
62 memalign(size_t align, size_t nbytes)
63 {
64 	size_t	 reqsize;	/* Num of bytes to get from malloc() */
65 	TREE	*p;		/* Ptr returned from malloc() */
66 	TREE	*blk;		/* For addressing fragment blocks */
67 	size_t	blksize;	/* Current (shrinking) block size */
68 	TREE	*alignedp;	/* Ptr to properly aligned boundary */
69 	TREE	*aligned_blk;	/* The block to be returned */
70 	size_t	frag_size;	/* size of fragments fore and aft */
71 	size_t	 x;
72 
73 	/*
74 	 * check for valid size and alignment parameters
75 	 * MAX_ALIGN check prevents overflow in later calculation.
76 	 */
77 	if (nbytes == 0 || _misaligned(align) || align == 0 ||
78 	    align > MAX_ALIGN) {
79 		errno = EINVAL;
80 		return (NULL);
81 	}
82 
83 	/*
84 	 * Malloc enough memory to guarantee that the result can be
85 	 * aligned correctly. The worst case is when malloc returns
86 	 * a block so close to the next alignment boundary that a
87 	 * fragment of minimum size cannot be created.  In order to
88 	 * make sure we can handle this, we need to force the
89 	 * alignment to be at least as large as the minimum frag size
90 	 * (MINSIZE + WORDSIZE).
91 	 */
92 
93 	/* check for size that could overflow calculations */
94 	if (nbytes > MAX_MALLOC) {
95 		errno = ENOMEM;
96 		return (NULL);
97 	}
98 	ROUND(nbytes);
99 	if (nbytes < MINSIZE)
100 		nbytes = MINSIZE;
101 	ROUND(align);
102 	while (align < MINSIZE + WORDSIZE)
103 		align <<= 1;
104 	reqsize = nbytes + align + (MINSIZE + WORDSIZE);
105 
106 	/* check for overflow */
107 	if (reqsize < nbytes) {
108 		errno = ENOMEM;
109 		return (NULL);
110 	}
111 
112 	p = (TREE *)malloc(reqsize);
113 	if (p == (TREE *)NULL) {
114 		/* malloc sets errno */
115 		return (NULL);
116 	}
117 	(void) _private_mutex_lock(&libc_malloc_lock);
118 
119 	/*
120 	 * get size of the entire block (overhead and all)
121 	 */
122 	blk = BLOCK(p);			/* back up to get length word */
123 	blksize = SIZE(blk);
124 	CLRBITS01(blksize);
125 
126 	/*
127 	 * locate the proper alignment boundary within the block.
128 	 */
129 	x = (size_t)p;
130 	if (x % align != 0)
131 		x += align - (x % align);
132 	alignedp = (TREE *)x;
133 	aligned_blk = BLOCK(alignedp);
134 
135 	/*
136 	 * Check out the space to the left of the alignment
137 	 * boundary, and split off a fragment if necessary.
138 	 */
139 	frag_size = (size_t)aligned_blk - (size_t)blk;
140 	if (frag_size != 0) {
141 		/*
142 		 * Create a fragment to the left of the aligned block.
143 		 */
144 		if (frag_size < MINSIZE + WORDSIZE) {
145 			/*
146 			 * Not enough space. So make the split
147 			 * at the other end of the alignment unit.
148 			 * We know this yields enough space, because
149 			 * we forced align >= MINSIZE + WORDSIZE above.
150 			 */
151 			frag_size += align;
152 			aligned_blk = _nextblk(aligned_blk, align);
153 		}
154 		blksize -= frag_size;
155 		SIZE(aligned_blk) = blksize | BIT0;
156 		frag_size -= WORDSIZE;
157 		SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk));
158 		_free_unlocked(DATA(blk));
159 	}
160 
161 	/*
162 	 * Is there a (sufficiently large) fragment to the
163 	 * right of the aligned block?
164 	 */
165 	frag_size = blksize - nbytes;
166 	if (frag_size >= MINSIZE + WORDSIZE) {
167 		/*
168 		 * split and free a fragment on the right
169 		 */
170 		blksize = SIZE(aligned_blk);
171 		SIZE(aligned_blk) = nbytes;
172 		blk = NEXT(aligned_blk);
173 		SETOLD01(SIZE(aligned_blk), blksize);
174 		frag_size -= WORDSIZE;
175 		SIZE(blk) = frag_size | BIT0;
176 		_free_unlocked(DATA(blk));
177 	}
178 	(void) _private_mutex_unlock(&libc_malloc_lock);
179 	return (DATA(aligned_blk));
180 }
181