2N/A/*
2N/A * CDDL HEADER START
2N/A *
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 *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
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 *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "lint.h"
2N/A#include "mallint.h"
2N/A#include "mtlib.h"
2N/A
2N/A#define _misaligned(p) ((unsigned)(p) & 3)
2N/A /* 4-byte "word" alignment is considered ok in LP64 */
2N/A#define _nextblk(p, size) ((TREE *)((uintptr_t)(p) + (size)))
2N/A
2N/A/*
2N/A * memalign(align, nbytes)
2N/A *
2N/A * Description:
2N/A * Returns a block of specified size on a specified alignment boundary.
2N/A *
2N/A * Algorithm:
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 *
2N/A * Errors:
2N/A * Returns NULL and sets errno as follows:
2N/A * [EINVAL]
2N/A * if nbytes = 0,
2N/A * or if alignment is misaligned,
2N/A * or if the heap has been detectably corrupted.
2N/A * [ENOMEM]
2N/A * if the requested memory could not be allocated.
2N/A */
2N/A
2N/Avoid *
2N/Amemalign(size_t align, size_t nbytes)
2N/A{
2N/A size_t reqsize; /* Num of bytes to get from malloc() */
2N/A TREE *p; /* Ptr returned from malloc() */
2N/A TREE *blk; /* For addressing fragment blocks */
2N/A size_t blksize; /* Current (shrinking) block size */
2N/A TREE *alignedp; /* Ptr to properly aligned boundary */
2N/A TREE *aligned_blk; /* The block to be returned */
2N/A size_t frag_size; /* size of fragments fore and aft */
2N/A size_t x;
2N/A
2N/A if (!primary_link_map) {
2N/A errno = ENOTSUP;
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * check for valid size and alignment parameters
2N/A * MAX_ALIGN check prevents overflow in later calculation.
2N/A */
2N/A if (nbytes == 0 || _misaligned(align) || align == 0 ||
2N/A align > MAX_ALIGN) {
2N/A errno = EINVAL;
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
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 */
2N/A
2N/A /* check for size that could overflow calculations */
2N/A if (nbytes > MAX_MALLOC) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A ROUND(nbytes);
2N/A if (nbytes < MINSIZE)
2N/A nbytes = MINSIZE;
2N/A ROUND(align);
2N/A while (align < MINSIZE + WORDSIZE)
2N/A align <<= 1;
2N/A reqsize = nbytes + align + (MINSIZE + WORDSIZE);
2N/A
2N/A /* check for overflow */
2N/A if (reqsize < nbytes) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A
2N/A p = (TREE *)malloc(reqsize);
2N/A if (p == (TREE *)NULL) {
2N/A /* malloc sets errno */
2N/A return (NULL);
2N/A }
2N/A (void) mutex_lock(&libc_malloc_lock);
2N/A
2N/A /*
2N/A * get size of the entire block (overhead and all)
2N/A */
2N/A blk = BLOCK(p); /* back up to get length word */
2N/A blksize = SIZE(blk);
2N/A CLRBITS01(blksize);
2N/A
2N/A /*
2N/A * locate the proper alignment boundary within the block.
2N/A */
2N/A x = (size_t)p;
2N/A if (x % align != 0)
2N/A x += align - (x % align);
2N/A alignedp = (TREE *)x;
2N/A aligned_blk = BLOCK(alignedp);
2N/A
2N/A /*
2N/A * Check out the space to the left of the alignment
2N/A * boundary, and split off a fragment if necessary.
2N/A */
2N/A frag_size = (size_t)aligned_blk - (size_t)blk;
2N/A if (frag_size != 0) {
2N/A /*
2N/A * Create a fragment to the left of the aligned block.
2N/A */
2N/A if (frag_size < MINSIZE + WORDSIZE) {
2N/A /*
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 */
2N/A frag_size += align;
2N/A aligned_blk = _nextblk(aligned_blk, align);
2N/A }
2N/A blksize -= frag_size;
2N/A SIZE(aligned_blk) = blksize | BIT0;
2N/A frag_size -= WORDSIZE;
2N/A SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk));
2N/A _free_unlocked(DATA(blk));
2N/A }
2N/A
2N/A /*
2N/A * Is there a (sufficiently large) fragment to the
2N/A * right of the aligned block?
2N/A */
2N/A frag_size = blksize - nbytes;
2N/A if (frag_size >= MINSIZE + WORDSIZE) {
2N/A /*
2N/A * split and free a fragment on the right
2N/A */
2N/A blksize = SIZE(aligned_blk);
2N/A SIZE(aligned_blk) = nbytes;
2N/A blk = NEXT(aligned_blk);
2N/A SETOLD01(SIZE(aligned_blk), blksize);
2N/A frag_size -= WORDSIZE;
2N/A SIZE(blk) = frag_size | BIT0;
2N/A _free_unlocked(DATA(blk));
2N/A }
2N/A (void) mutex_unlock(&libc_malloc_lock);
2N/A return (DATA(aligned_blk));
2N/A}