123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /*
- * FreeRTOS Kernel V10.4.6
- * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * SPDX-License-Identifier: MIT
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * https://www.FreeRTOS.org
- * https://github.com/FreeRTOS
- *
- */
- /*
- * The simplest possible implementation of pvPortMalloc(). Note that this
- * implementation does NOT allow allocated memory to be freed again.
- *
- * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
- * memory management pages of https://www.FreeRTOS.org for more information.
- */
- #include <stdlib.h>
- /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
- * all the API functions to use the MPU wrappers. That should only be done when
- * task.h is included from an application file. */
- #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
- #include "FreeRTOS.h"
- #include "task.h"
- #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
- #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
- #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
- #endif
- /* A few bytes might be lost to byte aligning the heap start address. */
- #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
- /* Allocate the memory for the heap. */
- #if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
- /* The application writer has already defined the array used for the RTOS
- * heap - probably so it can be placed in a special segment or address. */
- extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
- #else
- static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
- #endif /* configAPPLICATION_ALLOCATED_HEAP */
- /* Index into the ucHeap array. */
- static size_t xNextFreeByte = ( size_t ) 0;
- /*-----------------------------------------------------------*/
- void * pvPortMalloc( size_t xWantedSize )
- {
- void * pvReturn = NULL;
- static uint8_t * pucAlignedHeap = NULL;
- /* Ensure that blocks are always aligned. */
- #if ( portBYTE_ALIGNMENT != 1 )
- {
- if( xWantedSize & portBYTE_ALIGNMENT_MASK )
- {
- /* Byte alignment required. Check for overflow. */
- if ( (xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) )) > xWantedSize )
- {
- xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
- }
- else
- {
- xWantedSize = 0;
- }
- }
- }
- #endif
- vTaskSuspendAll();
- {
- if( pucAlignedHeap == NULL )
- {
- /* Ensure the heap starts on a correctly aligned boundary. */
- pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) & ucHeap[ portBYTE_ALIGNMENT - 1 ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
- }
- /* Check there is enough room left for the allocation and. */
- if( ( xWantedSize > 0 ) && /* valid size */
- ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
- ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) ) /* Check for overflow. */
- {
- /* Return the next free byte then increment the index past this
- * block. */
- pvReturn = pucAlignedHeap + xNextFreeByte;
- xNextFreeByte += xWantedSize;
- }
- traceMALLOC( pvReturn, xWantedSize );
- }
- ( void ) xTaskResumeAll();
- #if ( configUSE_MALLOC_FAILED_HOOK == 1 )
- {
- if( pvReturn == NULL )
- {
- extern void vApplicationMallocFailedHook( void );
- vApplicationMallocFailedHook();
- }
- }
- #endif
- return pvReturn;
- }
- /*-----------------------------------------------------------*/
- void vPortFree( void * pv )
- {
- /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and
- * heap_4.c for alternative implementations, and the memory management pages of
- * https://www.FreeRTOS.org for more information. */
- ( void ) pv;
- /* Force an assert as it is invalid to call this function. */
- configASSERT( pv == NULL );
- }
- /*-----------------------------------------------------------*/
- void vPortInitialiseBlocks( void )
- {
- /* Only required when static memory is not cleared. */
- xNextFreeByte = ( size_t ) 0;
- }
- /*-----------------------------------------------------------*/
- size_t xPortGetFreeHeapSize( void )
- {
- return( configADJUSTED_HEAP_SIZE - xNextFreeByte );
- }
|