2013-05-18 12:00:37 +00:00
|
|
|
/* $Source$
|
|
|
|
* $State$
|
|
|
|
* $Revision$
|
2013-05-08 23:56:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2013-05-18 12:00:37 +00:00
|
|
|
#include <errno.h>
|
2013-05-08 23:56:10 +00:00
|
|
|
#include "libsys.h"
|
|
|
|
|
|
|
|
#define OUT_OF_MEMORY (void*)(-1) /* sbrk returns this on failure */
|
|
|
|
|
2013-05-18 12:00:37 +00:00
|
|
|
static char* current = NULL;
|
2013-05-08 23:56:10 +00:00
|
|
|
|
2016-11-23 21:04:21 +00:00
|
|
|
int brk(void* end)
|
|
|
|
{
|
|
|
|
int e = _syscall(__NR_brk, (quad) end, 0, 0);
|
|
|
|
if (e == -1)
|
|
|
|
errno = ENOMEM;
|
|
|
|
else
|
|
|
|
current = end;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:06:24 +00:00
|
|
|
void* sbrk(int increment)
|
2013-05-08 23:56:10 +00:00
|
|
|
{
|
|
|
|
char* old;
|
|
|
|
char* new;
|
2013-05-18 12:00:37 +00:00
|
|
|
|
|
|
|
if (!current)
|
|
|
|
current = (char*) _syscall(__NR_brk, 0, 0, 0);
|
2013-05-08 23:56:10 +00:00
|
|
|
|
|
|
|
if (increment == 0)
|
|
|
|
return current;
|
|
|
|
|
|
|
|
old = current;
|
2016-11-26 10:56:17 +00:00
|
|
|
|
2013-05-08 23:56:10 +00:00
|
|
|
new = old + increment;
|
2013-05-18 12:00:37 +00:00
|
|
|
|
2016-11-26 10:56:17 +00:00
|
|
|
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;
|
2013-05-08 23:56:10 +00:00
|
|
|
|
|
|
|
return old;
|
2016-11-26 10:56:17 +00:00
|
|
|
|
|
|
|
out_of_memory:
|
|
|
|
errno = ENOMEM;
|
|
|
|
return OUT_OF_MEMORY;
|
2013-05-08 23:56:10 +00:00
|
|
|
}
|
2016-11-26 10:56:17 +00:00
|
|
|
|