sbrk.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***********************************************************************************************************************
  2. * DISCLAIMER
  3. * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
  4. * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
  5. * applicable laws, including copyright laws.
  6. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
  7. * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
  8. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
  9. * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
  10. * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
  11. * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  12. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
  13. * this software. By using this software, you agree to the additional terms and conditions found by accessing the
  14. * following link:
  15. * http://www.renesas.com/disclaimer
  16. *
  17. * Copyright (C) 2013 Renesas Electronics Corporation. All rights reserved.
  18. ***********************************************************************************************************************/
  19. /***********************************************************************************************************************
  20. * File Name : sbrk.c
  21. * Device(s) : RX
  22. * Description : Configures the MCU heap memory. The size of the heap is defined by the macro HEAPSIZE below.
  23. ***********************************************************************************************************************/
  24. /***********************************************************************************************************************
  25. * History : DD.MM.YYYY Version Description
  26. * : 26.10.2011 1.00 First Release
  27. * : 12.03.2012 1.10 Heap size is now defined in r_bsp_config.h, not sbrk.h.
  28. * : 19.11.2012 1.20 Updated code to use 'BSP_' and 'BSP_CFG_' prefix for macros.
  29. * : 26.11.2013 1.21 Replaced stddef.h, stdio.h, and stdint.h includes with include for platform.h.
  30. * : 15.05.2017 1.30 Deleted unnecessary comments.
  31. * :(15.05.2017 2.00 Deleted unnecessary comments.)
  32. * Added the bsp startup module disable function.
  33. * :(01.11.2017 2.00 Added the bsp startup module disable function.)
  34. * : xx.xx.xxxx 2.01 Added support for GNUC and ICCRX.
  35. * Deleted unnecessary stddef.h, stdio.h, and stdint.h includes.
  36. ***********************************************************************************************************************/
  37. /***********************************************************************************************************************
  38. Includes <System Includes> , "Project Includes"
  39. ***********************************************************************************************************************/
  40. #include "r_bsp_config.h"
  41. /* Only use this file if heap is enabled in r_bsp_config. */
  42. #if (BSP_CFG_HEAP_BYTES > 0)
  43. /* Used for getting BSP_CFG_HEAP_BYTES macro. */
  44. #include "platform.h"
  45. /* When using the user startup program, disable the following code. */
  46. #if (BSP_CFG_STARTUP_DISABLE == 0)
  47. #if defined(__CCRX__) || defined(__GNUC__)
  48. /***********************************************************************************************************************
  49. Macro definitions
  50. ***********************************************************************************************************************/
  51. /***********************************************************************************************************************
  52. Typedef definitions
  53. ***********************************************************************************************************************/
  54. /***********************************************************************************************************************
  55. Exported global variables (to be accessed by other files)
  56. ***********************************************************************************************************************/
  57. /***********************************************************************************************************************
  58. Private global variables and functions
  59. ***********************************************************************************************************************/
  60. /* Memory allocation function prototype declaration (CC-RX and GNURX+NEWLIB) */
  61. int8_t *sbrk(size_t size);
  62. #if defined(__GNUC__)
  63. /* Memory address function prototype declaration (GNURX+OPTLIB only) */
  64. int8_t *_top_of_heap(void);
  65. #endif /* defined(__GNUC__) */
  66. //const size_t _sbrk_size= /* Specifies the minimum unit of */
  67. /* the defined heap area */
  68. extern int8_t *_s1ptr;
  69. union HEAP_TYPE
  70. {
  71. int32_t dummy; /* Dummy for 4-byte boundary */
  72. int8_t heap[BSP_CFG_HEAP_BYTES]; /* Declaration of the area managed by sbrk*/
  73. };
  74. /* Declare memory heap area */
  75. static union HEAP_TYPE heap_area;
  76. /* End address allocated by sbrk (CC-RX and GNURX+NEWLIB) */
  77. static int8_t *brk=(int8_t *)&heap_area;
  78. #if defined(__GNUC__)
  79. /* Start address of allocated heap area (GNURX+OPTLIB only) */
  80. int8_t *_heap_of_memory=(int8_t *)&heap_area;
  81. /* End address of allocated heap area (GNURX+OPTLIB only) */
  82. int8_t *_last_heap_object=(int8_t *)&heap_area;
  83. #endif /* defined(__GNUC__) */
  84. /***********************************************************************************************************************
  85. * Function name: sbrk
  86. * Description : This function configures MCU memory area allocation. (CC-RX and GNURX+NEWLIB)
  87. * Arguments : size -
  88. * assigned area size
  89. * Return value : Start address of allocated area (pass)
  90. * -1 (failure)
  91. ***********************************************************************************************************************/
  92. int8_t *sbrk(size_t size)
  93. {
  94. int8_t *p;
  95. if ((brk + size) > (heap_area.heap + BSP_CFG_HEAP_BYTES))
  96. {
  97. /* Empty area size */
  98. p = (int8_t *)-1;
  99. }
  100. else
  101. {
  102. /* Area assignment */
  103. p = brk;
  104. /* End address update */
  105. brk += size;
  106. }
  107. /* Return result */
  108. return p;
  109. }
  110. #if defined(__GNUC__)
  111. /***********************************************************************************************************************
  112. * Function name: _top_of_heap
  113. * Description : This function returns end address of reserved heap area. (GNURX+OPTLIB only)
  114. * Arguments : none
  115. * Return value : End address of reserved heap area
  116. ***********************************************************************************************************************/
  117. int8_t *_top_of_heap(void)
  118. {
  119. return (int8_t *)(heap_area.heap + BSP_CFG_HEAP_BYTES);
  120. }
  121. #endif /* defined(__GNUC__) */
  122. #endif /* defined(__CCRX__), defined(__GNUC__) */
  123. #endif /* BSP_CFG_STARTUP_DISABLE == 0 */
  124. #endif /* BSP_CFG_HEAP_BYTES */