xref: /titanic_41/usr/src/lib/libc/port/gen/memalign.c (revision 4bac220845f606f60663ed6f3a2b88caa00ae87e)
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 #pragma weak memalign = _memalign
33 
34 #include "synonyms.h"
35 #include "mallint.h"
36 #include "mtlib.h"
37 
38 #define	_misaligned(p)		((unsigned)(p) & 3)
39 		/* 4-byte "word" alignment is considered ok in LP64 */
40 #define	_nextblk(p, size)	((TREE *)((uintptr_t)(p) + (size)))
41 
42 /*
43  * memalign(align, nbytes)
44  *
45  * Description:
46  *	Returns a block of specified size on a specified alignment boundary.
47  *
48  * Algorithm:
49  *	Malloc enough to ensure that a block can be aligned correctly.
50  *	Find the alignment point and return the fragments
51  *	before and after the block.
52  *
53  * Errors:
54  *	Returns NULL and sets errno as follows:
55  *	[EINVAL]
56  *		if nbytes = 0,
57  *		or if alignment is misaligned,
58  *		or if the heap has been detectably corrupted.
59  *	[ENOMEM]
60  *		if the requested memory could not be allocated.
61  */
62 
63 void *
64 memalign(size_t align, size_t nbytes)
65 {
66 	size_t	 reqsize;	/* Num of bytes to get from malloc() */
67 	TREE	*p;		/* Ptr returned from malloc() */
68 	TREE	*blk;		/* For addressing fragment blocks */
69 	size_t	blksize;	/* Current (shrinking) block size */
70 	TREE	*alignedp;	/* Ptr to properly aligned boundary */
71 	TREE	*aligned_blk;	/* The block to be returned */
72 	size_t	frag_size;	/* size of fragments fore and aft */
73 	size_t	 x;
74 
75 	/*
76 	 * check for valid size and alignment parameters
77 	 * MAX_ALIGN check prevents overflow in later calculation.
78 	 */
79 	if (nbytes == 0 || _misaligned(align) || align == 0 ||
80 	    align > MAX_ALIGN) {
81 		errno = EINVAL;
82 		return (NULL);
83 	}
84 
85 	/*
86 	 * Malloc enough memory to guarantee that the result can be
87 	 * aligned correctly. The worst case is when malloc returns
88 	 * a block so close to the next alignment boundary that a
89 	 * fragment of minimum size cannot be created.  In order to
90 	 * make sure we can handle this, we need to force the
91 	 * alignment to be at least as large as the minimum frag size
92 	 * (MINSIZE + WORDSIZE).
93 	 */
94 
95 	/* check for size that could overflow calculations */
96 	if (nbytes > MAX_MALLOC) {
97 		errno = ENOMEM;
98 		return (NULL);
99 	}
100 	ROUND(nbytes);
101 	if (nbytes < MINSIZE)
102 		nbytes = MINSIZE;
103 	ROUND(align);
104 	while (align < MINSIZE + WORDSIZE)
105 		align <<= 1;
106 	reqsize = nbytes + align + (MINSIZE + WORDSIZE);
107 
108 	/* check for overflow */
109 	if (reqsize < nbytes) {
110 		errno = ENOMEM;
111 		return (NULL);
112 	}
113 
114 	p = (TREE *)malloc(reqsize);
115 	if (p == (TREE *)NULL) {
116 		/* malloc sets errno */
117 		return (NULL);
118 	}
119 	(void) _private_mutex_lock(&libc_malloc_lock);
120 
121 	/*
122 	 * get size of the entire block (overhead and all)
123 	 */
124 	blk = BLOCK(p);			/* back up to get length word */
125 	blksize = SIZE(blk);
126 	CLRBITS01(blksize);
127 
128 	/*
129 	 * locate the proper alignment boundary within the block.
130 	 */
131 	x = (size_t)p;
132 	if (x % align != 0)
133 		x += align - (x % align);
134 	alignedp = (TREE *)x;
135 	aligned_blk = BLOCK(alignedp);
136 
137 	/*
138 	 * Check out the space to the left of the alignment
139 	 * boundary, and split off a fragment if necessary.
140 	 */
141 	frag_size = (size_t)aligned_blk - (size_t)blk;
142 	if (frag_size != 0) {
143 		/*
144 		 * Create a fragment to the left of the aligned block.
145 		 */
146 		if (frag_size < MINSIZE + WORDSIZE) {
147 			/*
148 			 * Not enough space. So make the split
149 			 * at the other end of the alignment unit.
150 			 * We know this yields enough space, because
151 			 * we forced align >= MINSIZE + WORDSIZE above.
152 			 */
153 			frag_size += align;
154 			aligned_blk = _nextblk(aligned_blk, align);
155 		}
156 		blksize -= frag_size;
157 		SIZE(aligned_blk) = blksize | BIT0;
158 		frag_size -= WORDSIZE;
159 		SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk));
160 		_free_unlocked(DATA(blk));
161 	}
162 
163 	/*
164 	 * Is there a (sufficiently large) fragment to the
165 	 * right of the aligned block?
166 	 */
167 	frag_size = blksize - nbytes;
168 	if (frag_size >= MINSIZE + WORDSIZE) {
169 		/*
170 		 * split and free a fragment on the right
171 		 */
172 		blksize = SIZE(aligned_blk);
173 		SIZE(aligned_blk) = nbytes;
174 		blk = NEXT(aligned_blk);
175 		SETOLD01(SIZE(aligned_blk), blksize);
176 		frag_size -= WORDSIZE;
177 		SIZE(blk) = frag_size | BIT0;
178 		_free_unlocked(DATA(blk));
179 	}
180 	(void) _private_mutex_unlock(&libc_malloc_lock);
181 	return (DATA(aligned_blk));
182 }
183