4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1987 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
35 * memalign(align,nbytes)
38 * Returns a block of specified size on a specified alignment boundary.
41 * Malloc enough to ensure that a block can be aligned correctly.
42 * Find the alignment point and return the fragments
43 * before and after the block.
46 * Returns NULL and sets errno as follows:
49 * or if alignment is misaligned,
50 * or if the heap has been detectably corrupted.
52 * if the requested memory could not be allocated.
56 memalign(align
, nbytes
)
60 uint reqsize
; /* Num of bytes to get from malloc() */
61 register char *p
; /* Ptr returned from malloc() */
62 register Dblk blk
; /* For addressing fragment blocks */
63 register uint blksize
; /* Current (shrinking) block size */
64 register char *alignedp
; /* Ptr to properly aligned boundary */
65 register Dblk aligned_blk
; /* The block to be returned */
66 register uint frag_size
; /* size of fragments fore and aft */
67 uint x
; /* ccom can't do (char*)(uint/uint) */
70 * check for valid size and alignment parameters
72 if (nbytes
== 0 || misaligned(align
)) {
78 * Malloc enough memory to guarantee that the result can be
79 * aligned correctly. The worst case is when malloc returns
80 * a block so close to the next alignment boundary that a
81 * fragment of minimum size cannot be created.
83 nbytes
= roundup(nbytes
, ALIGNSIZ
);
84 reqsize
= nbytes
+ align
+ SMALLEST_BLK
;
91 * get size of the entire block (overhead and all)
93 blk
= (Dblk
)(p
- ALIGNSIZ
); /* back up to get length word */
97 * locate the proper alignment boundary within the block.
99 x
= roundup((uint
)p
, align
); /* ccom work-around */
100 alignedp
= (char *)x
;
101 aligned_blk
= (Dblk
)(alignedp
- ALIGNSIZ
);
104 * Check out the space to the left of the alignment
105 * boundary, and split off a fragment if necessary.
107 frag_size
= (uint
)aligned_blk
- (uint
)blk
;
108 if (frag_size
!= 0) {
110 * Create a fragment to the left of the aligned block.
112 if ( frag_size
< SMALLEST_BLK
) {
114 * Not enough space. So make the split
115 * at the other end of the alignment unit.
118 aligned_blk
= nextblk(aligned_blk
,align
);
120 blk
->size
= frag_size
;
121 blksize
-= frag_size
;
122 aligned_blk
->size
= blksize
;
127 * Is there a (sufficiently large) fragment to the
128 * right of the aligned block?
131 frag_size
= blksize
- nbytes
;
132 if (frag_size
> SMALLEST_BLK
) {
134 * split and free a fragment on the right
136 blk
= nextblk(aligned_blk
, nbytes
);
137 blk
->size
= frag_size
;
138 aligned_blk
->size
-= frag_size
;
141 return(aligned_blk
->data
);