#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
+#include <pi.h>
#define OUT_OF_MEMORY (void*)(-1) /* sbrk returns this on failure */
-#define STACK_BUFFER 128 /* number of bytes to leave for stack */
+#define STACK_BUFFER 1024 /* number of bytes to leave for stack */
extern char _end[1];
static char* current = _end;
+/* Top of heap: we assume that the block of memory the binary is loaded in
+ * is 256kB long. Because user pointers are always relative to the beginning
+ * of the block, this makes the end address easy to calculate. */
+static char* max = (char*) (256*1024);
+
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))
+ if ((newend >= (void*)max) || (newend < (void*)_end))
return -1;
-
- current = p;
+
+ current = newend;
return 0;
}