ack/plat/linux/libsys/sbrk.c
David Given 36ab90385f Change sbrk() to take an int rather than an intptr_t (following the OpenBSD way
rather than the Linux way; various non-C bits of the ACK assume it takes an
int, so it's cleaner).
2016-11-23 22:06:24 +01:00

50 lines
737 B
C

/* $Source$
* $State$
* $Revision$
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "libsys.h"
#define OUT_OF_MEMORY (void*)(-1) /* sbrk returns this on failure */
static char* current = NULL;
int brk(void* end)
{
int e = _syscall(__NR_brk, (quad) end, 0, 0);
if (e == -1)
errno = ENOMEM;
else
current = end;
return e;
}
void* sbrk(int increment)
{
char* old;
char* new;
char* actual;
if (!current)
current = (char*) _syscall(__NR_brk, 0, 0, 0);
if (increment == 0)
return current;
old = current;
new = old + increment;
actual = (char*) _syscall(__NR_brk, (quad) new, 0, 0);
if (actual < new)
{
errno = ENOMEM;
return OUT_OF_MEMORY;
}
current = actual;
return old;
}