xref: /titanic_52/usr/src/lib/libc/port/gen/memalign.c (revision 448bf8594153765bb5fce82a8888e01e3f6c3bad)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "lint.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 	if (!primary_link_map) {
74 		errno = ENOTSUP;
75 		return (NULL);
76 	}
77 
78 	/*
79 	 * check for valid size and alignment parameters
80 	 * MAX_ALIGN check prevents overflow in later calculation.
81 	 */
82 	if (nbytes == 0 || _misaligned(align) || align == 0 ||
83 	    align > MAX_ALIGN) {
84 		errno = EINVAL;
85 		return (NULL);
86 	}
87 
88 	/*
89 	 * Malloc enough memory to guarantee that the result can be
90 	 * aligned correctly. The worst case is when malloc returns
91 	 * a block so close to the next alignment boundary that a
92 	 * fragment of minimum size cannot be created.  In order to
93 	 * make sure we can handle this, we need to force the
94 	 * alignment to be at least as large as the minimum frag size
95 	 * (MINSIZE + WORDSIZE).
96 	 */
97 
98 	/* check for size that could overflow calculations */
99 	if (nbytes > MAX_MALLOC) {
100 		errno = ENOMEM;
101 		return (NULL);
102 	}
103 	ROUND(nbytes);
104 	if (nbytes < MINSIZE)
105 		nbytes = MINSIZE;
106 	ROUND(align);
107 	while (align < MINSIZE + WORDSIZE)
108 		align <<= 1;
109 	reqsize = nbytes + align + (MINSIZE + WORDSIZE);
110 
111 	/* check for overflow */
112 	if (reqsize < nbytes) {
113 		errno = ENOMEM;
114 		return (NULL);
115 	}
116 
117 	p = (TREE *)malloc(reqsize);
118 	if (p == (TREE *)NULL) {
119 		/* malloc sets errno */
120 		return (NULL);
121 	}
122 	(void) mutex_lock(&libc_malloc_lock);
123 
124 	/*
125 	 * get size of the entire block (overhead and all)
126 	 */
127 	blk = BLOCK(p);			/* back up to get length word */
128 	blksize = SIZE(blk);
129 	CLRBITS01(blksize);
130 
131 	/*
132 	 * locate the proper alignment boundary within the block.
133 	 */
134 	x = (size_t)p;
135 	if (x % align != 0)
136 		x += align - (x % align);
137 	alignedp = (TREE *)x;
138 	aligned_blk = BLOCK(alignedp);
139 
140 	/*
141 	 * Check out the space to the left of the alignment
142 	 * boundary, and split off a fragment if necessary.
143 	 */
144 	frag_size = (size_t)aligned_blk - (size_t)blk;
145 	if (frag_size != 0) {
146 		/*
147 		 * Create a fragment to the left of the aligned block.
148 		 */
149 		if (frag_size < MINSIZE + WORDSIZE) {
150 			/*
151 			 * Not enough space. So make the split
152 			 * at the other end of the alignment unit.
153 			 * We know this yields enough space, because
154 			 * we forced align >= MINSIZE + WORDSIZE above.
155 			 */
156 			frag_size += align;
157 			aligned_blk = _nextblk(aligned_blk, align);
158 		}
159 		blksize -= frag_size;
160 		SIZE(aligned_blk) = blksize | BIT0;
161 		frag_size -= WORDSIZE;
162 		SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk));
163 		_free_unlocked(DATA(blk));
164 	}
165 
166 	/*
167 	 * Is there a (sufficiently large) fragment to the
168 	 * right of the aligned block?
169 	 */
170 	frag_size = blksize - nbytes;
171 	if (frag_size >= MINSIZE + WORDSIZE) {
172 		/*
173 		 * split and free a fragment on the right
174 		 */
175 		blksize = SIZE(aligned_blk);
176 		SIZE(aligned_blk) = nbytes;
177 		blk = NEXT(aligned_blk);
178 		SETOLD01(SIZE(aligned_blk), blksize);
179 		frag_size -= WORDSIZE;
180 		SIZE(blk) = frag_size | BIT0;
181 		_free_unlocked(DATA(blk));
182 	}
183 	(void) mutex_unlock(&libc_malloc_lock);
184 	return (DATA(aligned_blk));
185 }
186