]> begriffs open source - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/BufferManagement/BufferAllocation_1.c
Update FreeRTOS version number to V7.5.3
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / BufferManagement / BufferAllocation_1.c
1 /*\r
2  * FreeRTOS+UDP V1.0.1 (C) 2013 Real Time Engineers ltd.\r
3  * All rights reserved\r
4  *\r
5  * This file is part of the FreeRTOS+UDP distribution.  The FreeRTOS+UDP license\r
6  * terms are different to the FreeRTOS license terms.\r
7  *\r
8  * FreeRTOS+UDP uses a dual license model that allows the software to be used \r
9  * under a standard GPL open source license, or a commercial license.  The \r
10  * standard GPL license (unlike the modified GPL license under which FreeRTOS \r
11  * itself is distributed) requires that all software statically linked with \r
12  * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.  \r
13  * Details of both license options follow:\r
14  *\r
15  * - Open source licensing -\r
16  * FreeRTOS+UDP is a free download and may be used, modified, evaluated and\r
17  * distributed without charge provided the user adheres to version two of the\r
18  * GNU General Public License (GPL) and does not remove the copyright notice or\r
19  * this text.  The GPL V2 text is available on the gnu.org web site, and on the\r
20  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.\r
21  *\r
22  * - Commercial licensing -\r
23  * Businesses and individuals that for commercial or other reasons cannot comply\r
24  * with the terms of the GPL V2 license must obtain a commercial license before \r
25  * incorporating FreeRTOS+UDP into proprietary software for distribution in any \r
26  * form.  Commercial licenses can be purchased from http://shop.freertos.org/udp \r
27  * and do not require any source files to be changed.\r
28  *\r
29  * FreeRTOS+UDP is distributed in the hope that it will be useful.  You cannot\r
30  * use FreeRTOS+UDP unless you agree that you use the software 'as is'.\r
31  * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied\r
32  * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
33  * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
34  * implied, expressed, or statutory.\r
35  *\r
36  * 1 tab == 4 spaces!\r
37  *\r
38  * http://www.FreeRTOS.org\r
39  * http://www.FreeRTOS.org/udp\r
40  *\r
41  */\r
42 \r
43 \r
44 /******************************************************************************\r
45  *\r
46  * See the following web page for essential buffer allocation scheme usage and\r
47  * configuration details:\r
48  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml\r
49  *\r
50  ******************************************************************************/\r
51 \r
52 \r
53 /* Standard includes. */\r
54 #include <stdint.h>\r
55 \r
56 /* FreeRTOS includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "semphr.h"\r
60 \r
61 /* FreeRTOS+UDP includes. */\r
62 #include "FreeRTOS_UDP_IP.h"\r
63 #include "NetworkInterface.h"\r
64 \r
65 /* For an Ethernet interrupt to be able to obtain a network buffer there must\r
66 be at least this number of buffers available. */\r
67 #define ipINTERRUPT_BUFFER_GET_THRESHOLD        ( 3 )\r
68 \r
69 /* A list of free (available) xNetworkBufferDescriptor_t structures. */\r
70 static xList xFreeBuffersList;\r
71 \r
72 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the\r
73 system.  All the network buffers referenced from xFreeBuffersList exist in this\r
74 array.  The array is not accessed directly except during initialisation, when\r
75 the xFreeBuffersList is filled (as all the buffers are free when the system is\r
76 booted). */\r
77 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];\r
78 \r
79 /* The semaphore used to obtain network buffers. */\r
80 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 portBASE_TYPE xNetworkBuffersInitialise( void )\r
85 {\r
86 portBASE_TYPE xReturn, x;\r
87 \r
88         /* Only initialise the buffers and their associated kernel objects if they\r
89         have not been initialised before. */\r
90         if( xNetworkBufferSemaphore == NULL )\r
91         {\r
92                 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );\r
93                 configASSERT( xNetworkBufferSemaphore );\r
94 \r
95                 if( xNetworkBufferSemaphore != NULL )\r
96                 {\r
97                         vListInitialise( &xFreeBuffersList );\r
98 \r
99                         /* Initialise all the network buffers.  The buffer storage comes\r
100                         from the network interface, and different hardware has different\r
101                         requirements. */\r
102                         vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );\r
103                         for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )\r
104                         {\r
105                                 /* Initialise and set the owner of the buffer list items. */\r
106                                 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );\r
107                                 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );\r
108 \r
109                                 /* Currently, all buffers are available for use. */\r
110                                 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );\r
111                         }\r
112                 }\r
113         }\r
114 \r
115         if( xNetworkBufferSemaphore == NULL )\r
116         {\r
117                 xReturn = pdFAIL;\r
118         }\r
119         else\r
120         {\r
121                 xReturn = pdPASS;\r
122         }\r
123 \r
124         return xReturn;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )\r
129 {\r
130 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
131 \r
132         /*_RB_ The current implementation only has a single size memory block, so\r
133         the requested size parameter is not used (yet). */\r
134         ( void ) xRequestedSizeBytes;\r
135 \r
136         /* If there is a semaphore available, there is a network buffer available. */\r
137         if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
138         {\r
139                 /* Protect the structure as it is accessed from tasks and interrupts. */\r
140                 taskENTER_CRITICAL();\r
141                 {\r
142                         pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
143                         uxListRemove( &( pxReturn->xBufferListItem ) );\r
144                 }\r
145                 taskEXIT_CRITICAL();\r
146                 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
147         }\r
148         else\r
149         {\r
150                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
151         }\r
152 \r
153         return pxReturn;\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )\r
158 {\r
159 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
160 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
161 \r
162         /*_RB_ The current implementation only has a single size memory block, so\r
163         the requested size parameter is not used (yet). */\r
164         ( void ) xRequestedSizeBytes;\r
165 \r
166         /* If there is a semaphore available then there is a buffer available, but,\r
167         as this is called from an interrupt, only take a buffer if there are at\r
168         least ipINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining.  This prevents,\r
169         to a certain degree at least, a rapidly executing interrupt exhausting\r
170         buffer and in so doing preventing tasks from continuing. */\r
171         if( uxQueueMessagesWaitingFromISR( ( xQueueHandle ) xNetworkBufferSemaphore ) > ipINTERRUPT_BUFFER_GET_THRESHOLD )\r
172         {\r
173                 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )\r
174                 {\r
175                         /* Protect the structure as it is accessed from tasks and interrupts. */\r
176                         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
177                         {\r
178                                 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
179                                 uxListRemove( &( pxReturn->xBufferListItem ) );\r
180                         }\r
181                         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
182 \r
183                         iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );\r
184                 }\r
185         }\r
186 \r
187         if( pxReturn == NULL )\r
188         {\r
189                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();\r
190         }\r
191 \r
192         return pxReturn;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 portBASE_TYPE vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
197 {\r
198 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
199 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
200 \r
201         /* Ensure the buffer is returned to the list of free buffers before the\r
202         counting semaphore is 'given' to say a buffer is available. */\r
203         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
204         {\r
205                 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
206         }\r
207         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
208 \r
209         xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );\r
210         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
211 \r
212         return xHigherPriorityTaskWoken;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
217 {\r
218 portBASE_TYPE xListItemAlreadyInFreeList;\r
219 \r
220         /* Ensure the buffer is returned to the list of free buffers before the\r
221         counting semaphore is 'given' to say a buffer is available. */\r
222         taskENTER_CRITICAL();\r
223         {\r
224                 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
225 \r
226                 if( xListItemAlreadyInFreeList == pdFALSE )\r
227                 {\r
228                         vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
229                 }\r
230 \r
231                 configASSERT( xListItemAlreadyInFreeList == pdFALSE );\r
232         }\r
233         taskEXIT_CRITICAL();\r
234 \r
235         xSemaphoreGive( xNetworkBufferSemaphore );\r
236         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 #if( ipconfigINCLUDE_TEST_CODE == 1 )\r
241 \r
242 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )\r
243 {\r
244         return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
245 }\r
246 \r
247 #endif /* ipconfigINCLUDE_TEST_CODE */\r