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 (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#pragma weak _sbrk = sbrk
2N/A#pragma weak _brk = brk
2N/A
2N/A#include "lint.h"
2N/A#include <synch.h>
2N/A#include <errno.h>
2N/A#include <sys/isa_defs.h>
2N/A#include <sys/types.h>
2N/A#include <sys/sysmacros.h>
2N/A#include <inttypes.h>
2N/A#include <unistd.h>
2N/A#include "mtlib.h"
2N/A#include "libc.h"
2N/A
2N/Avoid *_nd = NULL;
2N/Amutex_t __sbrk_lock = DEFAULTMUTEX;
2N/A
2N/Aextern intptr_t _brk_unlocked(void *);
2N/Aextern void *_sbrk_unlocked(intptr_t);
2N/A
2N/A/*
2N/A * The break must always be at least 8-byte aligned
2N/A */
2N/A#if (_MAX_ALIGNMENT < 8)
2N/A#define ALIGNSZ 8
2N/A#else
2N/A#define ALIGNSZ _MAX_ALIGNMENT
2N/A#endif
2N/A
2N/A#define BRKALIGN(x) (caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
2N/A
2N/Avoid *
2N/Asbrk(intptr_t addend)
2N/A{
2N/A void *result;
2N/A
2N/A if (!primary_link_map) {
2N/A errno = ENOTSUP;
2N/A return ((void *)-1);
2N/A }
2N/A lmutex_lock(&__sbrk_lock);
2N/A result = _sbrk_unlocked(addend);
2N/A lmutex_unlock(&__sbrk_lock);
2N/A
2N/A return (result);
2N/A}
2N/A
2N/A/*
2N/A * _sbrk_unlocked() aligns the old break, adds the addend, aligns
2N/A * the new break, and calls _brk_unlocked() to set the new break.
2N/A * We must align the old break because _nd may begin life misaligned.
2N/A * The addend can be either positive or negative, so there are two
2N/A * overflow/underflow edge conditions to reject:
2N/A *
2N/A * - the addend is negative and brk + addend < 0.
2N/A * - the addend is positive and brk + addend > ULONG_MAX
2N/A */
2N/Avoid *
2N/A_sbrk_unlocked(intptr_t addend)
2N/A{
2N/A char *old_brk, *new_brk;
2N/A
2N/A if (_nd == NULL) {
2N/A /*
2N/A * Start of the data segment hasn't been calculated yet.
2N/A * Use brk(0) to gather it from the kernel.
2N/A */
2N/A _nd = (void *)_brk_unlocked(0);
2N/A }
2N/A
2N/A old_brk = BRKALIGN(_nd);
2N/A new_brk = BRKALIGN(old_brk + addend);
2N/A
2N/A if ((addend > 0 && new_brk < old_brk) ||
2N/A (addend < 0 && new_brk > old_brk)) {
2N/A errno = ENOMEM;
2N/A return ((void *)-1);
2N/A }
2N/A if (_brk_unlocked(new_brk) != 0)
2N/A return ((void *)-1);
2N/A _nd = new_brk;
2N/A return (old_brk);
2N/A}
2N/A
2N/A/*
2N/A * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
2N/A * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
2N/A * to set the new break. The low_aligned-aligned value is returned, and
2N/A * the actual space allocated is returned through actual_size.
2N/A *
2N/A * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
2N/A * not allow shrinking the heap.
2N/A */
2N/Avoid *
2N/A_sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
2N/A size_t *actual_size)
2N/A{
2N/A uintptr_t old_brk;
2N/A uintptr_t ret_brk;
2N/A uintptr_t high_brk;
2N/A uintptr_t new_brk;
2N/A int brk_result;
2N/A
2N/A if (!primary_link_map) {
2N/A errno = ENOTSUP;
2N/A return ((void *)-1);
2N/A }
2N/A if ((low_align & (low_align - 1)) != 0 ||
2N/A (high_align & (high_align - 1)) != 0) {
2N/A errno = EINVAL;
2N/A return ((void *)-1);
2N/A }
2N/A low_align = MAX(low_align, ALIGNSZ);
2N/A high_align = MAX(high_align, ALIGNSZ);
2N/A
2N/A lmutex_lock(&__sbrk_lock);
2N/A
2N/A if (_nd == NULL) {
2N/A /*
2N/A * Start of the data segment hasn't been calculated yet.
2N/A * Use brk(0) to gather it from the kernel.
2N/A */
2N/A _nd = (void *)_brk_unlocked(0);
2N/A }
2N/A
2N/A old_brk = (uintptr_t)BRKALIGN(_nd);
2N/A ret_brk = P2ROUNDUP(old_brk, low_align);
2N/A high_brk = ret_brk + min_size;
2N/A new_brk = P2ROUNDUP(high_brk, high_align);
2N/A
2N/A /*
2N/A * Check for overflow
2N/A */
2N/A if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
2N/A lmutex_unlock(&__sbrk_lock);
2N/A errno = ENOMEM;
2N/A return ((void *)-1);
2N/A }
2N/A
2N/A if ((brk_result = _brk_unlocked((void *)new_brk)) == 0)
2N/A _nd = (void *)new_brk;
2N/A lmutex_unlock(&__sbrk_lock);
2N/A
2N/A if (brk_result != 0)
2N/A return ((void *)-1);
2N/A
2N/A if (actual_size != NULL)
2N/A *actual_size = (new_brk - ret_brk);
2N/A return ((void *)ret_brk);
2N/A}
2N/A
2N/Aint
2N/Abrk(void *new_brk)
2N/A{
2N/A intptr_t result;
2N/A
2N/A if (!primary_link_map) {
2N/A errno = ENOTSUP;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Return what a call to brk(0) not going through the special path
2N/A * would return.
2N/A */
2N/A if ((uintptr_t)new_brk == 0) {
2N/A errno = ENOMEM;
2N/A return (-1);
2N/A }
2N/A /*
2N/A * Need to align this here; _brk_unlocked won't do it for us.
2N/A */
2N/A new_brk = BRKALIGN(new_brk);
2N/A
2N/A lmutex_lock(&__sbrk_lock);
2N/A if ((result = _brk_unlocked(new_brk)) == 0)
2N/A _nd = new_brk;
2N/A lmutex_unlock(&__sbrk_lock);
2N/A
2N/A return ((int)result);
2N/A}