xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/memalign.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "mallint.h"
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate extern	int errno;
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  * memalign(align,nbytes)
36*0Sstevel@tonic-gate  *
37*0Sstevel@tonic-gate  * Description:
38*0Sstevel@tonic-gate  *	Returns a block of specified size on a specified alignment boundary.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * Algorithm:
41*0Sstevel@tonic-gate  *	Malloc enough to ensure that a block can be aligned correctly.
42*0Sstevel@tonic-gate  *	Find the alignment point and return the fragments
43*0Sstevel@tonic-gate  *	before and after the block.
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * Errors:
46*0Sstevel@tonic-gate  *	Returns NULL and sets errno as follows:
47*0Sstevel@tonic-gate  *	[EINVAL]
48*0Sstevel@tonic-gate  *		if nbytes = 0,
49*0Sstevel@tonic-gate  *		or if alignment is misaligned,
50*0Sstevel@tonic-gate  *	 	or if the heap has been detectably corrupted.
51*0Sstevel@tonic-gate  *	[ENOMEM]
52*0Sstevel@tonic-gate  *		if the requested memory could not be allocated.
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate char *
memalign(align,nbytes)56*0Sstevel@tonic-gate memalign(align, nbytes)
57*0Sstevel@tonic-gate 	uint	align;
58*0Sstevel@tonic-gate 	uint	nbytes;
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	uint	reqsize;		/* Num of bytes to get from malloc() */
61*0Sstevel@tonic-gate 	register char	*p;		/* Ptr returned from malloc() */
62*0Sstevel@tonic-gate 	register Dblk	blk;		/* For addressing fragment blocks */
63*0Sstevel@tonic-gate 	register uint	blksize;	/* Current (shrinking) block size */
64*0Sstevel@tonic-gate 	register char	*alignedp;	/* Ptr to properly aligned boundary */
65*0Sstevel@tonic-gate 	register Dblk	aligned_blk;	/* The block to be returned */
66*0Sstevel@tonic-gate 	register uint	frag_size;	/* size of fragments fore and aft */
67*0Sstevel@tonic-gate 	uint	x;			/* ccom can't do (char*)(uint/uint) */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	/*
70*0Sstevel@tonic-gate 	 * check for valid size and alignment parameters
71*0Sstevel@tonic-gate 	 */
72*0Sstevel@tonic-gate 	if (nbytes == 0 || misaligned(align)) {
73*0Sstevel@tonic-gate 		errno = EINVAL;
74*0Sstevel@tonic-gate 		return NULL;
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	/*
78*0Sstevel@tonic-gate 	 * Malloc enough memory to guarantee that the result can be
79*0Sstevel@tonic-gate 	 * aligned correctly. The worst case is when malloc returns
80*0Sstevel@tonic-gate 	 * a block so close to the next alignment boundary that a
81*0Sstevel@tonic-gate 	 * fragment of minimum size cannot be created.
82*0Sstevel@tonic-gate 	 */
83*0Sstevel@tonic-gate 	nbytes = roundup(nbytes, ALIGNSIZ);
84*0Sstevel@tonic-gate 	reqsize = nbytes + align + SMALLEST_BLK;
85*0Sstevel@tonic-gate 	p = malloc(reqsize);
86*0Sstevel@tonic-gate 	if (p == NULL) {
87*0Sstevel@tonic-gate 		return NULL;
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/*
91*0Sstevel@tonic-gate 	 * get size of the entire block (overhead and all)
92*0Sstevel@tonic-gate 	 */
93*0Sstevel@tonic-gate 	blk = (Dblk)(p - ALIGNSIZ);	/* back up to get length word */
94*0Sstevel@tonic-gate 	blksize = blk->size;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/*
97*0Sstevel@tonic-gate 	 * locate the proper alignment boundary within the block.
98*0Sstevel@tonic-gate 	 */
99*0Sstevel@tonic-gate 	x = roundup((uint)p, align);		/* ccom work-around */
100*0Sstevel@tonic-gate 	alignedp = (char *)x;
101*0Sstevel@tonic-gate 	aligned_blk = (Dblk)(alignedp - ALIGNSIZ);
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/*
104*0Sstevel@tonic-gate 	 * Check out the space to the left of the alignment
105*0Sstevel@tonic-gate 	 * boundary, and split off a fragment if necessary.
106*0Sstevel@tonic-gate 	 */
107*0Sstevel@tonic-gate 	frag_size = (uint)aligned_blk - (uint)blk;
108*0Sstevel@tonic-gate 	if (frag_size != 0) {
109*0Sstevel@tonic-gate 		/*
110*0Sstevel@tonic-gate 		 * Create a fragment to the left of the aligned block.
111*0Sstevel@tonic-gate 		 */
112*0Sstevel@tonic-gate 		if ( frag_size < SMALLEST_BLK ) {
113*0Sstevel@tonic-gate 			/*
114*0Sstevel@tonic-gate 			 * Not enough space. So make the split
115*0Sstevel@tonic-gate 			 * at the other end of the alignment unit.
116*0Sstevel@tonic-gate 			 */
117*0Sstevel@tonic-gate 			frag_size += align;
118*0Sstevel@tonic-gate 			aligned_blk = nextblk(aligned_blk,align);
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 		blk->size = frag_size;
121*0Sstevel@tonic-gate 		blksize -= frag_size;
122*0Sstevel@tonic-gate 		aligned_blk->size = blksize;
123*0Sstevel@tonic-gate 		free(blk->data);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * Is there a (sufficiently large) fragment to the
128*0Sstevel@tonic-gate 	 * right of the aligned block?
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 	nbytes += ALIGNSIZ;
131*0Sstevel@tonic-gate 	frag_size = blksize - nbytes;
132*0Sstevel@tonic-gate 	if (frag_size > SMALLEST_BLK) {
133*0Sstevel@tonic-gate 		/*
134*0Sstevel@tonic-gate 		 * split and free a fragment on the right
135*0Sstevel@tonic-gate 		 */
136*0Sstevel@tonic-gate 		blk = nextblk(aligned_blk, nbytes);
137*0Sstevel@tonic-gate 		blk->size = frag_size;
138*0Sstevel@tonic-gate 		aligned_blk->size -= frag_size;
139*0Sstevel@tonic-gate 		free(blk->data);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	return(aligned_blk->data);
142*0Sstevel@tonic-gate }
143