139665Smsmith/*-
239665Smsmith * Copyright (c) 1998 Michael Smith
339665Smsmith * All rights reserved.
439665Smsmith *
539665Smsmith * Redistribution and use in source and binary forms, with or without
639665Smsmith * modification, are permitted provided that the following conditions
739665Smsmith * are met:
839665Smsmith * 1. Redistributions of source code must retain the above copyright
939665Smsmith *    notice, this list of conditions and the following disclaimer.
1039665Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1139665Smsmith *    notice, this list of conditions and the following disclaimer in the
1239665Smsmith *    documentation and/or other materials provided with the distribution.
1339665Smsmith *
1439665Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1539665Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1639665Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1739665Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1839665Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1939665Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2039665Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2139665Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2239665Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2339665Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2439665Smsmith * SUCH DAMAGE.
2539665Smsmith */
2639665Smsmith
2784221Sdillon#include <sys/cdefs.h>
2884221Sdillon__FBSDID("$FreeBSD$");
2984221Sdillon
3039665Smsmith/*
3139665Smsmith * Minimal sbrk() emulation required for malloc support.
3239665Smsmith */
3339665Smsmith
3439665Smsmith#include <string.h>
3539665Smsmith#include "stand.h"
36269101Sian#include "zalloc_defs.h"
3739665Smsmith
3839665Smsmithstatic size_t	maxheap, heapsize = 0;
3939665Smsmithstatic void	*heapbase;
4039665Smsmith
4139665Smsmithvoid
4239665Smsmithsetheap(void *base, void *top)
4339665Smsmith{
44269101Sian    /* Align start address for the malloc code.  Sigh. */
45269101Sian    heapbase = (void *)(((uintptr_t)base + MALLOCALIGN_MASK) &
46269101Sian        ~MALLOCALIGN_MASK);
47136093Sstefanf    maxheap = (char *)top - (char *)heapbase;
4839665Smsmith}
4939665Smsmith
5039665Smsmithchar *
5139665Smsmithsbrk(int incr)
5239665Smsmith{
5339665Smsmith    char	*ret;
5439665Smsmith
5539665Smsmith    if ((heapsize + incr) <= maxheap) {
56136093Sstefanf	ret = (char *)heapbase + heapsize;
5739665Smsmith	bzero(ret, incr);
5839665Smsmith	heapsize += incr;
5939665Smsmith	return(ret);
6039665Smsmith    }
6139665Smsmith    errno = ENOMEM;
6239665Smsmith    return((char *)-1);
6339665Smsmith}
6439665Smsmith
65