heap_1.c 4.9 KB

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