xref: /btstack/port/renesas-ek-ra6m4a-da14531/e2-project/ra/fsp/src/bsp/mcu/all/bsp_sbrk.c (revision c30869498fb8e98c1408c9db0e7624f02f483b73)
1 /***********************************************************************************************************************
2  * Copyright [2020-2022] Renesas Electronics Corporation and/or its affiliates.  All Rights Reserved.
3  *
4  * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
5  * of Renesas Electronics Corp. and its affiliates ("Renesas").  No other uses are authorized.  Renesas products are
6  * sold pursuant to Renesas terms and conditions of sale.  Purchasers are solely responsible for the selection and use
7  * of Renesas products and Renesas assumes no liability.  No license, express or implied, to any intellectual property
8  * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
9  * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
10  * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
11  * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
12  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
13  * DOCUMENTATION.  RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH.  TO THE MAXIMUM
14  * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
15  * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
16  * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
17  * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
19  **********************************************************************************************************************/
20 
21 /***********************************************************************************************************************
22  * Includes   <System Includes> , "Project Includes"
23  **********************************************************************************************************************/
24 #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
25 #include "bsp_api.h"
26 #include <sys/types.h>
27 #include <errno.h>
28 
29 /***********************************************************************************************************************
30  * Macro definitions
31  **********************************************************************************************************************/
32 
33 /***********************************************************************************************************************
34  * Typedef definitions
35  **********************************************************************************************************************/
36 
37 /***********************************************************************************************************************
38  * Private function prototypes
39  **********************************************************************************************************************/
40 
41 /***********************************************************************************************************************
42  * Exported global variables (to be accessed by other files)
43  **********************************************************************************************************************/
44 
45 caddr_t _sbrk(int incr);
46 
47 /***********************************************************************************************************************
48  * Private global variables and functions
49  **********************************************************************************************************************/
50 
51 /*******************************************************************************************************************//**
52  * @addtogroup BSP_MCU
53  * @{
54  **********************************************************************************************************************/
55 
56 /*******************************************************************************************************************//**
57  * FSP implementation of the standard library _sbrk() function.
58  * @param[in]   inc  The number of bytes being asked for by malloc().
59  *
60  * @note This function overrides the _sbrk version that exists in the newlib library that is linked with.
61  *       That version improperly relies on the SP as part of it's allocation strategy. This is bad in general and
62  *       worse in an RTOS environment. This version insures that we allocate the byte pool requested by malloc()
63  *       only from our allocated HEAP area. Also note that newlib is pre-built and forces the pagesize used by
64  *       malloc() to be 4096. That requires that we have a HEAP of at least 4096 if we are to support malloc().
65  * @retval        Address of allocated area if successful, -1 otherwise.
66  **********************************************************************************************************************/
67 
_sbrk(int incr)68 caddr_t _sbrk (int incr)
69 {
70     extern char _Heap_Begin __asm("__HeapBase");  ///< Defined by the linker.
71 
72     extern char _Heap_Limit __asm("__HeapLimit"); ///< Defined by the linker.
73 
74     uint32_t      bytes            = (uint32_t) incr;
75     static char * current_heap_end = 0;
76     char        * current_block_address;
77 
78     if (current_heap_end == 0)
79     {
80         current_heap_end = &_Heap_Begin;
81     }
82 
83     current_block_address = current_heap_end;
84 
85     /* The returned address must be aligned to a word boundary to prevent hard faults on cores that do not support
86      * unaligned access. We assume the heap starts on a word boundary and make sure all allocations are a multiple
87      * of 4. */
88     bytes = (bytes + 3U) & (~3U);
89     if (current_heap_end + bytes > &_Heap_Limit)
90     {
91         /** Heap has overflowed */
92         errno = ENOMEM;
93 
94         return (caddr_t) -1;
95     }
96 
97     current_heap_end += bytes;
98 
99     return (caddr_t) current_block_address;
100 }
101 
102 #endif
103 
104 /******************************************************************************************************************//**
105  * @} (end addtogroup BSP_MCU)
106  *********************************************************************************************************************/
107