Francisco Paez / freertos

Dependents:   frdm_k64f_freertos_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers heap_1.c Source File

heap_1.c

00001 /*
00002     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
00003     All rights reserved
00004 
00005     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
00006 
00007     This file is part of the FreeRTOS distribution.
00008 
00009     FreeRTOS is free software; you can redistribute it and/or modify it under
00010     the terms of the GNU General Public License (version 2) as published by the
00011     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
00012 
00013     ***************************************************************************
00014     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
00015     >>!   distribute a combined work that includes FreeRTOS without being   !<<
00016     >>!   obliged to provide the source code for proprietary components     !<<
00017     >>!   outside of the FreeRTOS kernel.                                   !<<
00018     ***************************************************************************
00019 
00020     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
00021     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00022     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
00023     link: http://www.freertos.org/a00114.html
00024 
00025     ***************************************************************************
00026      *                                                                       *
00027      *    FreeRTOS provides completely free yet professionally developed,    *
00028      *    robust, strictly quality controlled, supported, and cross          *
00029      *    platform software that is more than just the market leader, it     *
00030      *    is the industry's de facto standard.                               *
00031      *                                                                       *
00032      *    Help yourself get started quickly while simultaneously helping     *
00033      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
00034      *    tutorial book, reference manual, or both:                          *
00035      *    http://www.FreeRTOS.org/Documentation                              *
00036      *                                                                       *
00037     ***************************************************************************
00038 
00039     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
00040     the FAQ page "My application does not run, what could be wrong?".  Have you
00041     defined configASSERT()?
00042 
00043     http://www.FreeRTOS.org/support - In return for receiving this top quality
00044     embedded software for free we request you assist our global community by
00045     participating in the support forum.
00046 
00047     http://www.FreeRTOS.org/training - Investing in training allows your team to
00048     be as productive as possible as early as possible.  Now you can receive
00049     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
00050     Ltd, and the world's leading authority on the world's leading RTOS.
00051 
00052     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
00053     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
00054     compatible FAT file system, and our tiny thread aware UDP/IP stack.
00055 
00056     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
00057     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
00058 
00059     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
00060     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
00061     licenses offer ticketed support, indemnification and commercial middleware.
00062 
00063     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
00064     engineered and independently SIL3 certified version for use in safety and
00065     mission critical applications that require provable dependability.
00066 
00067     1 tab == 4 spaces!
00068 */
00069 
00070 
00071 /*
00072  * The simplest possible implementation of pvPortMalloc().  Note that this
00073  * implementation does NOT allow allocated memory to be freed again.
00074  *
00075  * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the
00076  * memory management pages of http://www.FreeRTOS.org for more information.
00077  */
00078 #include <stdlib.h>
00079 
00080 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
00081 all the API functions to use the MPU wrappers.  That should only be done when
00082 task.h is included from an application file. */
00083 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
00084 
00085 #include "FreeRTOS.h"
00086 #include "task.h"
00087 
00088 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
00089 
00090 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
00091     #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
00092 #endif
00093 
00094 /* A few bytes might be lost to byte aligning the heap start address. */
00095 #define configADJUSTED_HEAP_SIZE    ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
00096 
00097 /* Allocate the memory for the heap. */
00098 /* Allocate the memory for the heap. */
00099 #if( configAPPLICATION_ALLOCATED_HEAP == 1 )
00100     /* The application writer has already defined the array used for the RTOS
00101     heap - probably so it can be placed in a special segment or address. */
00102     extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
00103 #else
00104     static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
00105 #endif /* configAPPLICATION_ALLOCATED_HEAP */
00106 
00107 static size_t xNextFreeByte = ( size_t ) 0;
00108 
00109 /*-----------------------------------------------------------*/
00110 
00111 void *pvPortMalloc( size_t xWantedSize )
00112 {
00113 void *pvReturn = NULL;
00114 static uint8_t *pucAlignedHeap = NULL;
00115 
00116     /* Ensure that blocks are always aligned to the required number of bytes. */
00117     #if( portBYTE_ALIGNMENT != 1 )
00118     {
00119         if( xWantedSize & portBYTE_ALIGNMENT_MASK )
00120         {
00121             /* Byte alignment required. */
00122             xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
00123         }
00124     }
00125     #endif
00126 
00127     vTaskSuspendAll();
00128     {
00129         if( pucAlignedHeap == NULL )
00130         {
00131             /* Ensure the heap starts on a correctly aligned boundary. */
00132             pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
00133         }
00134 
00135         /* Check there is enough room left for the allocation. */
00136         if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
00137             ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) )/* Check for overflow. */
00138         {
00139             /* Return the next free byte then increment the index past this
00140             block. */
00141             pvReturn = pucAlignedHeap + xNextFreeByte;
00142             xNextFreeByte += xWantedSize;
00143         }
00144 
00145         traceMALLOC( pvReturn, xWantedSize );
00146     }
00147     ( void ) xTaskResumeAll();
00148 
00149     #if( configUSE_MALLOC_FAILED_HOOK == 1 )
00150     {
00151         if( pvReturn == NULL )
00152         {
00153             extern void vApplicationMallocFailedHook( void );
00154             vApplicationMallocFailedHook();
00155         }
00156     }
00157     #endif
00158 
00159     return pvReturn;
00160 }
00161 /*-----------------------------------------------------------*/
00162 
00163 void vPortFree( void *pv )
00164 {
00165     /* Memory cannot be freed using this scheme.  See heap_2.c, heap_3.c and
00166     heap_4.c for alternative implementations, and the memory management pages of
00167     http://www.FreeRTOS.org for more information. */
00168     ( void ) pv;
00169 
00170     /* Force an assert as it is invalid to call this function. */
00171     configASSERT( pv == NULL );
00172 }
00173 /*-----------------------------------------------------------*/
00174 
00175 void vPortInitialiseBlocks( void )
00176 {
00177     /* Only required when static memory is not cleared. */
00178     xNextFreeByte = ( size_t ) 0;
00179 }
00180 /*-----------------------------------------------------------*/
00181 
00182 size_t xPortGetFreeHeapSize( void )
00183 {
00184     return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );
00185 }
00186 
00187 
00188 
00189