Rohit Grover / FreeRTOS

Dependents:   Nucleo freertos_test FreeRTOS_test freertos_bluetooth ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers heap_1.c Source File

heap_1.c

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