2007-04-21 22:59:42 +00:00
|
|
|
/* $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))
|
2016-11-26 10:23:25 +00:00
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
2007-04-21 22:59:42 +00:00
|
|
|
return -1;
|
2016-11-26 10:23:25 +00:00
|
|
|
}
|
2007-04-21 22:59:42 +00:00
|
|
|
|
|
|
|
current = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:06:24 +00:00
|
|
|
void* sbrk(int increment)
|
2007-04-21 22:59:42 +00:00
|
|
|
{
|
|
|
|
char* old;
|
2016-11-26 10:23:25 +00:00
|
|
|
char* new;
|
|
|
|
|
2007-04-21 22:59:42 +00:00
|
|
|
if (increment == 0)
|
|
|
|
return current;
|
|
|
|
|
|
|
|
old = current;
|
2016-11-26 10:23:25 +00:00
|
|
|
|
|
|
|
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;
|
2007-04-21 22:59:42 +00:00
|
|
|
|
|
|
|
return old;
|
2016-11-26 10:23:25 +00:00
|
|
|
|
|
|
|
out_of_memory:
|
|
|
|
errno = ENOMEM;
|
|
|
|
return OUT_OF_MEMORY;
|
2007-04-21 22:59:42 +00:00
|
|
|
}
|