2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A/* Copyright (c) 1988 AT&T */ 2N/A/* All Rights Reserved */ 2N/A#
pragma ident "%Z%%M% %I% %E% SMI" 2N/A /* 4-byte "word" alignment is considered ok in LP64 */ 2N/A * memalign(align, nbytes) 2N/A * Returns a block of specified size on a specified alignment boundary. 2N/A * Malloc enough to ensure that a block can be aligned correctly. 2N/A * Find the alignment point and return the fragments 2N/A * before and after the block. 2N/A * Returns NULL and sets errno as follows: 2N/A * or if alignment is misaligned, 2N/A * or if the heap has been detectably corrupted. 2N/A * if the requested memory could not be allocated. 2N/A TREE *p;
/* Ptr returned from malloc() */ 2N/A * check for valid size and alignment parameters 2N/A * MAX_ALIGN check prevents overflow in later calculation. 2N/A * Malloc enough memory to guarantee that the result can be 2N/A * aligned correctly. The worst case is when malloc returns 2N/A * a block so close to the next alignment boundary that a 2N/A * fragment of minimum size cannot be created. In order to 2N/A * make sure we can handle this, we need to force the 2N/A * alignment to be at least as large as the minimum frag size 2N/A * (MINSIZE + WORDSIZE). 2N/A /* check for size that could overflow calculations */ 2N/A /* check for overflow */ 2N/A /* malloc sets errno */ 2N/A * get size of the entire block (overhead and all) 2N/A * locate the proper alignment boundary within the block. 2N/A * Check out the space to the left of the alignment 2N/A * boundary, and split off a fragment if necessary. 2N/A * Create a fragment to the left of the aligned block. 2N/A * Not enough space. So make the split 2N/A * at the other end of the alignment unit. 2N/A * We know this yields enough space, because 2N/A * we forced align >= MINSIZE + WORDSIZE above. 2N/A * Is there a (sufficiently large) fragment to the 2N/A * right of the aligned block? 2N/A * split and free a fragment on the right