uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sbrk.c
Go to the documentation of this file.
1 #include <reent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 
5 #include <FreeRTOS.h>
6 #include <task.h>
7 #include <semphr.h>
8 #include <mpu_wrappers.h>
9 
10 #include "osdebug.h"
11 
12 extern uintptr_t __heap_start;
13 extern uintptr_t __stack_start;
14 
15 static void * heap_end = (void *) &__heap_start;
16 
17 void * sbrk(ptrdiff_t incr) {
18  void *prev_heap_end, *next_heap_end, *ret;
19  void *stack_min = (void *) &__stack_start;
20 
21  prev_heap_end = heap_end;
22 
23  /* Align to always be on 8-byte boundaries */
24  next_heap_end = (void *)((((uintptr_t)heap_end + incr) + 7) & ~7);
25 
26  /* Check if this allocation would exceed the end of the ram - would probably get into the stack first however */
27  if (next_heap_end > stack_min) {
28  set_errno(ENOMEM);
29  ret = NULL;
30  } else {
31  heap_end = next_heap_end;
32  ret = (void *)prev_heap_end;
33  }
34 
35  return ret;
36 }