]> granicus.if.org Git - esp-idf/blob - components/freertos/list.c
sleep: fix checking length of RTC data sections
[esp-idf] / components / freertos / list.c
1 /*
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
12
13         ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18         ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40         the FAQ page "My application does not run, what could be wrong?".  Have you
41         defined configASSERT()?
42
43         http://www.FreeRTOS.org/support - In return for receiving this top quality
44         embedded software for free we request you assist our global community by
45         participating in the support forum.
46
47         http://www.FreeRTOS.org/training - Investing in training allows your team to
48         be as productive as possible as early as possible.  Now you can receive
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50         Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 #include <stdlib.h>
72 #include "FreeRTOS.h"
73 #include "list.h"
74
75 /*-----------------------------------------------------------
76  * PUBLIC LIST API documented in list.h
77  *----------------------------------------------------------*/
78
79 void vListInitialise( List_t * const pxList )
80 {
81         /* The list structure contains a list item which is used to mark the
82         end of the list.  To initialise the list the list end is inserted
83         as the only list entry. */
84         pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );                       /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
85
86         /* The list end value is the highest possible value in the list to
87         ensure it remains at the end of the list. */
88         pxList->xListEnd.xItemValue = portMAX_DELAY;
89
90         /* The list end next and previous pointers point to itself so we know
91         when the list is empty. */
92         pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );       /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
93         pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );/*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
94
95         pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
96
97         /* Write known values into the list if
98         configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
99         listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
100         listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
101 }
102 /*-----------------------------------------------------------*/
103
104 void vListInitialiseItem( ListItem_t * const pxItem )
105 {
106         /* Make sure the list item is not recorded as being on a list. */
107         pxItem->pvContainer = NULL;
108
109         /* Write known values into the list item if
110         configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
111         listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
112         listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
113 }
114 /*-----------------------------------------------------------*/
115
116 void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
117 {
118 ListItem_t * const pxIndex = pxList->pxIndex;
119
120         /* Only effective when configASSERT() is also defined, these tests may catch
121         the list data structures being overwritten in memory.  They will not catch
122         data errors caused by incorrect configuration or use of FreeRTOS. */
123         listTEST_LIST_INTEGRITY( pxList );
124         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
125
126         /* Insert a new list item into pxList, but rather than sort the list,
127         makes the new list item the last item to be removed by a call to
128         listGET_OWNER_OF_NEXT_ENTRY(). */
129         pxNewListItem->pxNext = pxIndex;
130         pxNewListItem->pxPrevious = pxIndex->pxPrevious;
131         pxIndex->pxPrevious->pxNext = pxNewListItem;
132         pxIndex->pxPrevious = pxNewListItem;
133
134         /* Remember which list the item is in. */
135         pxNewListItem->pvContainer = ( void * ) pxList;
136
137         ( pxList->uxNumberOfItems )++;
138 }
139 /*-----------------------------------------------------------*/
140
141 void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )
142 {
143 ListItem_t *pxIterator;
144 const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
145
146         /* Only effective when configASSERT() is also defined, these tests may catch
147         the list data structures being overwritten in memory.  They will not catch
148         data errors caused by incorrect configuration or use of FreeRTOS. */
149         listTEST_LIST_INTEGRITY( pxList );
150         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
151
152         /* Insert the new list item into the list, sorted in xItemValue order.
153
154         If the list already contains a list item with the same item value then the
155         new list item should be placed after it.  This ensures that TCB's which are
156         stored in ready lists (all of which have the same xItemValue value) get a
157         share of the CPU.  However, if the xItemValue is the same as the back marker
158         the iteration loop below will not end.  Therefore the value is checked
159         first, and the algorithm slightly modified if necessary. */
160         if( xValueOfInsertion == portMAX_DELAY )
161         {
162                 pxIterator = pxList->xListEnd.pxPrevious;
163         }
164         else
165         {
166                 /* *** NOTE ***********************************************************
167                 If you find your application is crashing here then likely causes are
168                 listed below.  In addition see http://www.freertos.org/FAQHelp.html for
169                 more tips, and ensure configASSERT() is defined!
170                 http://www.freertos.org/a00110.html#configASSERT
171
172                         1) Stack overflow -
173                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html
174                         2) Incorrect interrupt priority assignment, especially on Cortex-M
175                            parts where numerically high priority values denote low actual
176                            interrupt priorities, which can seem counter intuitive.  See
177                            http://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition
178                            of configMAX_SYSCALL_INTERRUPT_PRIORITY on
179                            http://www.freertos.org/a00110.html
180                         3) Calling an API function from within a critical section or when
181                            the scheduler is suspended, or calling an API function that does
182                            not end in "FromISR" from an interrupt.
183                         4) Using a queue or semaphore before it has been initialised or
184                            before the scheduler has been started (are interrupts firing
185                            before vTaskStartScheduler() has been called?).
186                 **********************************************************************/
187
188                 for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
189                 {
190                         /* There is nothing to do here, just iterating to the wanted
191                         insertion position. */
192                 }
193         }
194
195         pxNewListItem->pxNext = pxIterator->pxNext;
196         pxNewListItem->pxNext->pxPrevious = pxNewListItem;
197         pxNewListItem->pxPrevious = pxIterator;
198         pxIterator->pxNext = pxNewListItem;
199
200         /* Remember which list the item is in.  This allows fast removal of the
201         item later. */
202         pxNewListItem->pvContainer = ( void * ) pxList;
203
204         ( pxList->uxNumberOfItems )++;
205 }
206 /*-----------------------------------------------------------*/
207
208 UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
209 {
210 /* The list item knows which list it is in.  Obtain the list from the list
211 item. */
212 List_t * const pxList = ( List_t * ) pxItemToRemove->pvContainer;
213
214         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
215         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
216
217         /* Make sure the index is left pointing to a valid item. */
218         if( pxList->pxIndex == pxItemToRemove )
219         {
220                 pxList->pxIndex = pxItemToRemove->pxPrevious;
221         }
222         else
223         {
224                 mtCOVERAGE_TEST_MARKER();
225         }
226
227         pxItemToRemove->pvContainer = NULL;
228         ( pxList->uxNumberOfItems )--;
229
230         return pxList->uxNumberOfItems;
231 }
232 /*-----------------------------------------------------------*/
233