summaryrefslogtreecommitdiff
path: root/fw/fe310/bsp/gloss/sys_sbrk.c
blob: e789ec147839cba1de1d333120e8aac986051485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <sys/types.h>

/* brk is handled entirely within the C library.  This limits METAL programs that
 * use the C library to be disallowed from dynamically allocating memory
 * without talking to the C library, but that sounds like a sane way to go
 * about it.  Note that there is no error checking anywhere in this file, users
 * will simply get the relevant error when actually trying to use the memory
 * that's been allocated. */
extern char __heap_start;
extern char __heap_end;
static char *brk = &__heap_start;

int
_brk(void *addr)
{
  brk = addr;
  return 0;
}

char *
_sbrk(ptrdiff_t incr)
{
  char *old = brk;

  /* If __heap_size == 0, we can't allocate memory on the heap */
  if(&__heap_start == &__heap_end) {
    return (void *)-1;
  }

  /* Don't move the break past the end of the heap */
  if ((brk + incr) <= &__heap_end) {
    brk += incr;
  } else {
    return (void *)-1;
  }

  return old;
}