2013-05-16 23:03:38 +00:00
|
|
|
/*
|
|
|
|
* Raspberry Pi support library for the ACK
|
|
|
|
* © 2013 David Given
|
|
|
|
* This file is redistributable under the terms of the 3-clause BSD license.
|
|
|
|
* See the file 'Copying' in the root of the distribution for the full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2013-05-25 22:28:47 +00:00
|
|
|
#include <pi.h>
|
2013-05-16 23:03:38 +00:00
|
|
|
|
|
|
|
#define OUT_OF_MEMORY (void*)(-1) /* sbrk returns this on failure */
|
2013-05-25 22:28:47 +00:00
|
|
|
#define STACK_BUFFER 1024 /* number of bytes to leave for stack */
|
2013-05-16 23:03:38 +00:00
|
|
|
|
|
|
|
extern char _end[1];
|
|
|
|
static char* current = _end;
|
|
|
|
|
2013-05-25 22:28:47 +00:00
|
|
|
/* 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. */
|
2013-06-05 23:05:48 +00:00
|
|
|
static char* max = (char*) (128*1024);
|
2013-05-25 22:28:47 +00:00
|
|
|
|
2013-05-16 23:03:38 +00:00
|
|
|
int brk(void* newend)
|
|
|
|
{
|
2013-05-25 22:28:47 +00:00
|
|
|
if ((newend >= (void*)max) || (newend < (void*)_end))
|
2013-05-16 23:03:38 +00:00
|
|
|
return -1;
|
2013-05-25 22:28:47 +00:00
|
|
|
|
|
|
|
current = newend;
|
2013-05-16 23:03:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* sbrk(intptr_t increment)
|
|
|
|
{
|
|
|
|
char* old;
|
|
|
|
|
|
|
|
if (increment == 0)
|
|
|
|
return current;
|
|
|
|
|
|
|
|
old = current;
|
|
|
|
if (brk(old + increment) < 0)
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|