heap_1.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*
  29. * The simplest possible implementation of pvPortMalloc(). Note that this
  30. * implementation does NOT allow allocated memory to be freed again.
  31. *
  32. * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
  33. * memory management pages of https://www.FreeRTOS.org for more information.
  34. */
  35. #include <stdlib.h>
  36. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  37. * all the API functions to use the MPU wrappers. That should only be done when
  38. * task.h is included from an application file. */
  39. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  40. #include "FreeRTOS.h"
  41. #include "task.h"
  42. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  43. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  44. #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
  45. #endif
  46. /* A few bytes might be lost to byte aligning the heap start address. */
  47. #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
  48. /* Max value that fits in a size_t type. */
  49. #define heapSIZE_MAX ( ~( ( size_t ) 0 ) )
  50. /* Check if adding a and b will result in overflow. */
  51. #define heapADD_WILL_OVERFLOW( a, b ) ( ( a ) > ( heapSIZE_MAX - ( b ) ) )
  52. /*-----------------------------------------------------------*/
  53. /* Allocate the memory for the heap. */
  54. #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
  55. /* The application writer has already defined the array used for the RTOS
  56. * heap - probably so it can be placed in a special segment or address. */
  57. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  58. #else
  59. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  60. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  61. /* Index into the ucHeap array. */
  62. static size_t xNextFreeByte = ( size_t ) 0U;
  63. /*-----------------------------------------------------------*/
  64. void * pvPortMalloc( size_t xWantedSize )
  65. {
  66. void * pvReturn = NULL;
  67. static uint8_t * pucAlignedHeap = NULL;
  68. /* Ensure that blocks are always aligned. */
  69. #if ( portBYTE_ALIGNMENT != 1 )
  70. {
  71. size_t xAdditionalRequiredSize;
  72. if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
  73. {
  74. /* Byte alignment required. */
  75. xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
  76. if( heapADD_WILL_OVERFLOW( xWantedSize, xAdditionalRequiredSize ) == 0 )
  77. {
  78. xWantedSize += xAdditionalRequiredSize;
  79. }
  80. else
  81. {
  82. xWantedSize = 0;
  83. }
  84. }
  85. }
  86. #endif /* if ( portBYTE_ALIGNMENT != 1 ) */
  87. vTaskSuspendAll();
  88. {
  89. if( pucAlignedHeap == NULL )
  90. {
  91. /* Ensure the heap starts on a correctly aligned boundary. */
  92. pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &( ucHeap[ portBYTE_ALIGNMENT - 1 ] ) ) &
  93. ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  94. }
  95. /* Check there is enough room left for the allocation. */
  96. if( ( xWantedSize > 0 ) &&
  97. ( heapADD_WILL_OVERFLOW( xNextFreeByte, xWantedSize ) == 0 ) &&
  98. ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) )
  99. {
  100. /* Return the next free byte then increment the index past this
  101. * block. */
  102. pvReturn = pucAlignedHeap + xNextFreeByte;
  103. xNextFreeByte += xWantedSize;
  104. }
  105. traceMALLOC( pvReturn, xWantedSize );
  106. }
  107. ( void ) xTaskResumeAll();
  108. #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
  109. {
  110. if( pvReturn == NULL )
  111. {
  112. vApplicationMallocFailedHook();
  113. }
  114. }
  115. #endif
  116. return pvReturn;
  117. }
  118. /*-----------------------------------------------------------*/
  119. void vPortFree( void * pv )
  120. {
  121. /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
  122. * heap_4.c for alternative implementations, and the memory management pages of
  123. * https://www.FreeRTOS.org for more information. */
  124. ( void ) pv;
  125. /* Force an assert as it is invalid to call this function. */
  126. configASSERT( pv == NULL );
  127. }
  128. /*-----------------------------------------------------------*/
  129. void vPortInitialiseBlocks( void )
  130. {
  131. /* Only required when static memory is not cleared. */
  132. xNextFreeByte = ( size_t ) 0;
  133. }
  134. /*-----------------------------------------------------------*/
  135. size_t xPortGetFreeHeapSize( void )
  136. {
  137. return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
  138. }
  139. /*-----------------------------------------------------------*/
  140. /*
  141. * Reset the state in this file. This state is normally initialized at start up.
  142. * This function must be called by the application before restarting the
  143. * scheduler.
  144. */
  145. void vPortHeapResetState( void )
  146. {
  147. xNextFreeByte = ( size_t ) 0U;
  148. }
  149. /*-----------------------------------------------------------*/