heap_1.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 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. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  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 http://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. /* Allocate the memory for the heap. */
  49. /* Allocate the memory for the heap. */
  50. #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
  51. /* The application writer has already defined the array used for the RTOS
  52. heap - probably so it can be placed in a special segment or address. */
  53. extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  54. #else
  55. static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
  56. #endif /* configAPPLICATION_ALLOCATED_HEAP */
  57. /* Index into the ucHeap array. */
  58. static size_t xNextFreeByte = ( size_t ) 0;
  59. /*-----------------------------------------------------------*/
  60. void *pvPortMalloc( size_t xWantedSize )
  61. {
  62. void *pvReturn = NULL;
  63. static uint8_t *pucAlignedHeap = NULL;
  64. /* Ensure that blocks are always aligned to the required number of bytes. */
  65. #if( portBYTE_ALIGNMENT != 1 )
  66. {
  67. if( xWantedSize & portBYTE_ALIGNMENT_MASK )
  68. {
  69. /* Byte alignment required. */
  70. xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
  71. }
  72. }
  73. #endif
  74. vTaskSuspendAll();
  75. {
  76. if( pucAlignedHeap == NULL )
  77. {
  78. /* Ensure the heap starts on a correctly aligned boundary. */
  79. pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  80. }
  81. /* Check there is enough room left for the allocation. */
  82. if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
  83. ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) )/* Check for overflow. */
  84. {
  85. /* Return the next free byte then increment the index past this
  86. block. */
  87. pvReturn = pucAlignedHeap + xNextFreeByte;
  88. xNextFreeByte += xWantedSize;
  89. }
  90. traceMALLOC( pvReturn, xWantedSize );
  91. }
  92. ( void ) xTaskResumeAll();
  93. #if( configUSE_MALLOC_FAILED_HOOK == 1 )
  94. {
  95. if( pvReturn == NULL )
  96. {
  97. extern void vApplicationMallocFailedHook( void );
  98. vApplicationMallocFailedHook();
  99. }
  100. }
  101. #endif
  102. return pvReturn;
  103. }
  104. /*-----------------------------------------------------------*/
  105. void vPortFree( void *pv )
  106. {
  107. /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
  108. heap_4.c for alternative implementations, and the memory management pages of
  109. http://www.FreeRTOS.org for more information. */
  110. ( void ) pv;
  111. /* Force an assert as it is invalid to call this function. */
  112. configASSERT( pv == NULL );
  113. }
  114. /*-----------------------------------------------------------*/
  115. void vPortInitialiseBlocks( void )
  116. {
  117. /* Only required when static memory is not cleared. */
  118. xNextFreeByte = ( size_t ) 0;
  119. }
  120. /*-----------------------------------------------------------*/
  121. size_t xPortGetFreeHeapSize( void )
  122. {
  123. return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );
  124. }