ack/plat/qemuppc/libsys/brk.c
David Given 8a58614aef Rework the tests to run on pc86; lots of test fixes for the brk() test, which
was nearly useless; lots of fixes to qemuppc and pc86 sbrk(), which was broken;
change the pc86 console to echo output to the serial port (needed for running
tests on qemu).
2016-11-26 11:23:25 +01:00

60 lines
947 B
C

/* $Source$
* $State$
* $Revision$
*/
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define OUT_OF_MEMORY (void*)(-1) /* sbrk returns this on failure */
#define STACK_BUFFER 128 /* number of bytes to leave for stack */
extern char _end[1];
static char* current = _end;
int brk(void* newend)
{
/* This variable is used to figure out the current stack pointer,
* by taking its address. */
char dummy;
char* p = newend;
if ((p > (&dummy - STACK_BUFFER)) ||
(p < _end))
{
errno = ENOMEM;
return -1;
}
current = p;
return 0;
}
void* sbrk(int increment)
{
char* old;
char* new;
if (increment == 0)
return current;
old = current;
new = old + increment;
if ((increment > 0) && (new <= old))
goto out_of_memory;
else if ((increment < 0) && (new >= old))
goto out_of_memory;
if (brk(new) < 0)
goto out_of_memory;
return old;
out_of_memory:
errno = ENOMEM;
return OUT_OF_MEMORY;
}