heap_1.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  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. #include "FreeRTOS.h"
  37. #include "task.h"
  38. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  39. #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
  40. #endif
  41. /* A few bytes might be lost to byte aligning the heap start address. */
  42. #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
  43. /* Allocate the memory for the heap. */
  44. #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
  45. /* The application writer has already defined the array used for the RTOS
  46. * heap - probably so it can be placed in a special segment or address. */
  47. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  48. #else
  49. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  50. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  51. /* Index into the ucHeap array. */
  52. static size_t xNextFreeByte = ( size_t ) 0;
  53. /*-----------------------------------------------------------*/
  54. void * pvPortMalloc( size_t xWantedSize )
  55. {
  56. void * pvReturn = NULL;
  57. static uint8_t * pucAlignedHeap = NULL;
  58. /* Ensure that blocks are always aligned. */
  59. #if ( portBYTE_ALIGNMENT != 1 )
  60. {
  61. if( xWantedSize & portBYTE_ALIGNMENT_MASK )
  62. {
  63. /* Byte alignment required. Check for overflow. */
  64. if ( (xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) )) > xWantedSize )
  65. {
  66. xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
  67. }
  68. else
  69. {
  70. xWantedSize = 0;
  71. }
  72. }
  73. }
  74. #endif
  75. vTaskSuspendAll();
  76. {
  77. if( pucAlignedHeap == NULL )
  78. {
  79. /* Ensure the heap starts on a correctly aligned boundary. */
  80. pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT - 1 ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  81. }
  82. /* Check there is enough room left for the allocation and. */
  83. if( ( xWantedSize > 0 ) && /* valid size */
  84. ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
  85. ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
  86. {
  87. /* Return the next free byte then increment the index past this
  88. * block. */
  89. pvReturn = pucAlignedHeap + xNextFreeByte;
  90. xNextFreeByte += xWantedSize;
  91. }
  92. }
  93. ( void ) xTaskResumeAll();
  94. #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
  95. {
  96. if( pvReturn == NULL )
  97. {
  98. extern void vApplicationMallocFailedHook( void );
  99. vApplicationMallocFailedHook();
  100. }
  101. }
  102. #endif
  103. return pvReturn;
  104. }
  105. /*-----------------------------------------------------------*/
  106. void vPortFree( void * pv )
  107. {
  108. /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
  109. * heap_4.c for alternative implementations, and the memory management pages of
  110. * https://www.FreeRTOS.org for more information. */
  111. ( void ) pv;
  112. /* Force an assert as it is invalid to call this function. */
  113. configASSERT( pv == NULL );
  114. }
  115. /*-----------------------------------------------------------*/
  116. void vPortInitialiseBlocks( void )
  117. {
  118. /* Only required when static memory is not cleared. */
  119. xNextFreeByte = ( size_t ) 0;
  120. }
  121. /*-----------------------------------------------------------*/
  122. size_t xPortGetFreeHeapSize( void )
  123. {
  124. return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
  125. }