sbrk.c revision 60481
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 * 2650476Speter * $FreeBSD: head/lib/libstand/sbrk.c 60481 2000-05-12 22:43:20Z peter $ 2739665Smsmith */ 2839665Smsmith 2939665Smsmith/* 3039665Smsmith * Minimal sbrk() emulation required for malloc support. 3139665Smsmith */ 3239665Smsmith 3339665Smsmith#include <string.h> 3439665Smsmith#include "stand.h" 3539665Smsmith 3639665Smsmithstatic size_t maxheap, heapsize = 0; 3739665Smsmithstatic void *heapbase; 3839665Smsmith 3939665Smsmithvoid 4039665Smsmithsetheap(void *base, void *top) 4139665Smsmith{ 4260481Speter /* Align start address to 16 bytes for the malloc code. Sigh. */ 4360481Speter heapbase = (void *)(((uintptr_t)base + 15) & ~15); 4460481Speter maxheap = top - heapbase; 4539665Smsmith} 4639665Smsmith 4739665Smsmithchar * 4839665Smsmithsbrk(int incr) 4939665Smsmith{ 5039665Smsmith char *ret; 5139665Smsmith 5239665Smsmith if ((heapsize + incr) <= maxheap) { 5339665Smsmith ret = heapbase + heapsize; 5439665Smsmith bzero(ret, incr); 5539665Smsmith heapsize += incr; 5639665Smsmith return(ret); 5739665Smsmith } 5839665Smsmith errno = ENOMEM; 5939665Smsmith return((char *)-1); 6039665Smsmith} 6139665Smsmith 62