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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2016 Joyent, Inc.
28 /* Copyright (c) 1988 AT&T */
29 /* All Rights Reserved */
35 #define _misaligned(p) ((unsigned)(p) & 3)
36 /* 4-byte "word" alignment is considered ok in LP64 */
37 #define _nextblk(p, size) ((TREE *)((uintptr_t)(p) + (size)))
40 * memalign(align, nbytes)
43 * Returns a block of specified size on a specified alignment boundary.
46 * Malloc enough to ensure that a block can be aligned correctly.
47 * Find the alignment point and return the fragments
48 * before and after the block.
51 * Returns NULL and sets errno as follows:
54 * or if alignment is misaligned,
55 * or if the heap has been detectably corrupted.
57 * if the requested memory could not be allocated.
61 memalign(size_t align
, size_t nbytes
)
63 size_t reqsize
; /* Num of bytes to get from malloc() */
64 TREE
*p
; /* Ptr returned from malloc() */
65 TREE
*blk
; /* For addressing fragment blocks */
66 size_t blksize
; /* Current (shrinking) block size */
67 TREE
*alignedp
; /* Ptr to properly aligned boundary */
68 TREE
*aligned_blk
; /* The block to be returned */
69 size_t frag_size
; /* size of fragments fore and aft */
72 if (!primary_link_map
) {
78 * check for valid size and alignment parameters
79 * MAX_ALIGN check prevents overflow in later calculation.
81 if (nbytes
== 0 || _misaligned(align
) || align
== 0 ||
88 * Malloc enough memory to guarantee that the result can be
89 * aligned correctly. The worst case is when malloc returns
90 * a block so close to the next alignment boundary that a
91 * fragment of minimum size cannot be created. In order to
92 * make sure we can handle this, we need to force the
93 * alignment to be at least as large as the minimum frag size
94 * (MINSIZE + WORDSIZE).
97 /* check for size that could overflow calculations */
98 if (nbytes
> MAX_MALLOC
) {
103 if (nbytes
< MINSIZE
)
106 while (align
< MINSIZE
+ WORDSIZE
)
108 reqsize
= nbytes
+ align
+ (MINSIZE
+ WORDSIZE
);
110 /* check for overflow */
111 if (reqsize
< nbytes
) {
116 p
= (TREE
*)malloc(reqsize
);
117 if (p
== (TREE
*)NULL
) {
118 /* malloc sets errno */
121 (void) mutex_lock(&libc_malloc_lock
);
124 * get size of the entire block (overhead and all)
126 blk
= BLOCK(p
); /* back up to get length word */
131 * locate the proper alignment boundary within the block.
135 x
+= align
- (x
% align
);
136 alignedp
= (TREE
*)x
;
137 aligned_blk
= BLOCK(alignedp
);
140 * Check out the space to the left of the alignment
141 * boundary, and split off a fragment if necessary.
143 frag_size
= (size_t)aligned_blk
- (size_t)blk
;
144 if (frag_size
!= 0) {
146 * Create a fragment to the left of the aligned block.
148 if (frag_size
< MINSIZE
+ WORDSIZE
) {
150 * Not enough space. So make the split
151 * at the other end of the alignment unit.
152 * We know this yields enough space, because
153 * we forced align >= MINSIZE + WORDSIZE above.
156 aligned_blk
= _nextblk(aligned_blk
, align
);
158 blksize
-= frag_size
;
159 SIZE(aligned_blk
) = blksize
| BIT0
;
160 frag_size
-= WORDSIZE
;
161 SIZE(blk
) = frag_size
| BIT0
| ISBIT1(SIZE(blk
));
162 _free_unlocked(DATA(blk
));
166 * Is there a (sufficiently large) fragment to the
167 * right of the aligned block?
169 frag_size
= blksize
- nbytes
;
170 if (frag_size
>= MINSIZE
+ WORDSIZE
) {
172 * split and free a fragment on the right
174 blksize
= SIZE(aligned_blk
);
175 SIZE(aligned_blk
) = nbytes
;
176 blk
= NEXT(aligned_blk
);
177 SETOLD01(SIZE(aligned_blk
), blksize
);
178 frag_size
-= WORDSIZE
;
179 SIZE(blk
) = frag_size
| BIT0
;
180 _free_unlocked(DATA(blk
));
182 (void) mutex_unlock(&libc_malloc_lock
);
183 return (DATA(aligned_blk
));
187 * This is the ISO/IEC C11 version of memalign. We have kept it as a separate
188 * function, but it is basically the same thing. Note that this is implemented
189 * this way to make life easier to libraries which already interpose on
193 aligned_alloc(size_t align
, size_t size
)
195 return (memalign(align
, size
));