ARMEBS4  revision-26.06.2015
sbrk.c
Go to the documentation of this file.
1 /************************************************************************//**
2  * \file sbrk.c
3  * \brief provides _sbrk
4  * \author marc dot pignat at hevs dot ch
5  *
6  * When not using FreeRTOS, this function is used by newlib for dynamic memory
7  * allocation (malloc, free, ...).
8  *
9  * The heap space is provided by the linker script
10  *
11  * \see _heap_start
12  * \see _heap_end
13  ***************************************************************************/
14 
15 #include "heivs/config.h"
16 #if !USE_FREERTOS
17 
18 #include <errno.h>
19 #include <stddef.h>
20 #include "heivs/bsp.h"
21 
22 
23 static char const *heap_used_end = NULL;
24 static const uint32_t failed_is_fatal = USE_FATAL_MALLOC_FAILED;
25 
26 /**
27  * \brief Implementation of the sbrk systemcall for standalone software
28  *
29  * This system call is used when there is no more space for malloc.
30  *
31  * \see http://linux.die.net/man/2/sbrk
32  * \ingroup unistd_h
33  */
34 void *_sbrk (int incr)
35 {
36  /**
37  * Definitions provided by the linker
38  */
39  extern char _heap_start;
40  extern char _heap_end;
41  const char *end = &_heap_start;
42  const char *heap_end = &_heap_end;
43 
44  const char *old_end;
45 
46  /**
47  * First time run? init
48  */
49  if (heap_used_end == NULL)
50  {
51  heap_used_end = end;
52  }
53 
54  /* Verify there is enough place */
55  if (heap_used_end + incr > heap_end)
56  {
57  errno = ENOMEM;
58  if (failed_is_fatal)
59  {
61  }
62 
63  return (void *) -1;
64  }
65 
66  /* Remember last heap_used_end */
67  old_end = heap_used_end;
68 
69  heap_used_end += incr;
70 
71  return (void *)old_end;
72 }
73 #endif
BSP - Board Support Package.
libheivs configuration file
void bsp_fatal(status_e status)
fatal error
Definition: bsp.c:123
void * _sbrk(int incr)
Implementation of the sbrk systemcall for standalone software.
Definition: sbrk.c:34
#define USE_FATAL_MALLOC_FAILED
Call #bsp_fatal(ERROR_OUT_OF_MEMORY) when a malloc fails.
Definition: config.h:216
(or friend) failed
Definition: error.h:69