xref: /titanic_52/usr/src/lib/libbc/libc/gen/common/memalign.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1987 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 #include "mallint.h"
30 #include <errno.h>
31 
32 extern	int errno;
33 
34 /*
35  * memalign(align,nbytes)
36  *
37  * Description:
38  *	Returns a block of specified size on a specified alignment boundary.
39  *
40  * Algorithm:
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.
44  *
45  * Errors:
46  *	Returns NULL and sets errno as follows:
47  *	[EINVAL]
48  *		if nbytes = 0,
49  *		or if alignment is misaligned,
50  *	 	or if the heap has been detectably corrupted.
51  *	[ENOMEM]
52  *		if the requested memory could not be allocated.
53  */
54 
55 char *
56 memalign(align, nbytes)
57 	uint	align;
58 	uint	nbytes;
59 {
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) */
68 
69 	/*
70 	 * check for valid size and alignment parameters
71 	 */
72 	if (nbytes == 0 || misaligned(align)) {
73 		errno = EINVAL;
74 		return NULL;
75 	}
76 
77 	/*
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.
82 	 */
83 	nbytes = roundup(nbytes, ALIGNSIZ);
84 	reqsize = nbytes + align + SMALLEST_BLK;
85 	p = malloc(reqsize);
86 	if (p == NULL) {
87 		return NULL;
88 	}
89 
90 	/*
91 	 * get size of the entire block (overhead and all)
92 	 */
93 	blk = (Dblk)(p - ALIGNSIZ);	/* back up to get length word */
94 	blksize = blk->size;
95 
96 	/*
97 	 * locate the proper alignment boundary within the block.
98 	 */
99 	x = roundup((uint)p, align);		/* ccom work-around */
100 	alignedp = (char *)x;
101 	aligned_blk = (Dblk)(alignedp - ALIGNSIZ);
102 
103 	/*
104 	 * Check out the space to the left of the alignment
105 	 * boundary, and split off a fragment if necessary.
106 	 */
107 	frag_size = (uint)aligned_blk - (uint)blk;
108 	if (frag_size != 0) {
109 		/*
110 		 * Create a fragment to the left of the aligned block.
111 		 */
112 		if ( frag_size < SMALLEST_BLK ) {
113 			/*
114 			 * Not enough space. So make the split
115 			 * at the other end of the alignment unit.
116 			 */
117 			frag_size += align;
118 			aligned_blk = nextblk(aligned_blk,align);
119 		}
120 		blk->size = frag_size;
121 		blksize -= frag_size;
122 		aligned_blk->size = blksize;
123 		free(blk->data);
124 	}
125 
126 	/*
127 	 * Is there a (sufficiently large) fragment to the
128 	 * right of the aligned block?
129 	 */
130 	nbytes += ALIGNSIZ;
131 	frag_size = blksize - nbytes;
132 	if (frag_size > SMALLEST_BLK) {
133 		/*
134 		 * split and free a fragment on the right
135 		 */
136 		blk = nextblk(aligned_blk, nbytes);
137 		blk->size = frag_size;
138 		aligned_blk->size -= frag_size;
139 		free(blk->data);
140 	}
141 	return(aligned_blk->data);
142 }
143