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