]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/gloss/sys_sbrk.c
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / gloss / sys_sbrk.c
1 #include <sys/types.h>
2
3 /* brk is handled entirely within the C library.  This limits METAL programs that
4  * use the C library to be disallowed from dynamically allocating memory
5  * without talking to the C library, but that sounds like a sane way to go
6  * about it.  Note that there is no error checking anywhere in this file, users
7  * will simply get the relevant error when actually trying to use the memory
8  * that's been allocated. */
9 extern char metal_segment_heap_target_start;
10 extern char metal_segment_heap_target_end;
11 static char *brk = &metal_segment_heap_target_start;
12
13 int
14 _brk(void *addr)
15 {
16   brk = addr;
17   return 0;
18 }
19
20 char *
21 _sbrk(ptrdiff_t incr)
22 {
23   char *old = brk;
24
25   /* If __heap_size == 0, we can't allocate memory on the heap */
26   if(&metal_segment_heap_target_start == &metal_segment_heap_target_end) {
27     return (void *)-1;
28   }
29
30   /* Don't move the break past the end of the heap */
31   if ((brk + incr) < &metal_segment_heap_target_end) {
32     brk += incr;
33   } else {
34     brk = &metal_segment_heap_target_end;
35     return (void *)-1;
36   }
37
38   return old;
39 }