sbrk.c revision 136093
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: head/lib/libstand/sbrk.c 136093 2004-10-03 15:58:20Z stefanf $");
2984221Sdillon
3039665Smsmith/*
3139665Smsmith * Minimal sbrk() emulation required for malloc support.
3239665Smsmith */
3339665Smsmith
3439665Smsmith#include <string.h>
3539665Smsmith#include "stand.h"
3639665Smsmith
3739665Smsmithstatic size_t	maxheap, heapsize = 0;
3839665Smsmithstatic void	*heapbase;
3939665Smsmith
4039665Smsmithvoid
4139665Smsmithsetheap(void *base, void *top)
4239665Smsmith{
4360481Speter    /* Align start address to 16 bytes for the malloc code. Sigh. */
4460481Speter    heapbase = (void *)(((uintptr_t)base + 15) & ~15);
45136093Sstefanf    maxheap = (char *)top - (char *)heapbase;
4639665Smsmith}
4739665Smsmith
4839665Smsmithchar *
4939665Smsmithsbrk(int incr)
5039665Smsmith{
5139665Smsmith    char	*ret;
5239665Smsmith
5339665Smsmith    if ((heapsize + incr) <= maxheap) {
54136093Sstefanf	ret = (char *)heapbase + heapsize;
5539665Smsmith	bzero(ret, incr);
5639665Smsmith	heapsize += incr;
5739665Smsmith	return(ret);
5839665Smsmith    }
5939665Smsmith    errno = ENOMEM;
6039665Smsmith    return((char *)-1);
6139665Smsmith}
6239665Smsmith
63