]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/BufferManagement/BufferAllocation_1.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / BufferManagement / BufferAllocation_1.c
1 /*\r
2  * FreeRTOS+UDP V1.0.4\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /******************************************************************************\r
30  *\r
31  * See the following web page for essential buffer allocation scheme usage and\r
32  * configuration details:\r
33  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml\r
34  *\r
35  ******************************************************************************/\r
36 \r
37 \r
38 /* Standard includes. */\r
39 #include <stdint.h>\r
40 \r
41 /* FreeRTOS includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 #include "semphr.h"\r
45 \r
46 /* FreeRTOS+UDP includes. */\r
47 #include "FreeRTOS_UDP_IP.h"\r
48 #include "FreeRTOS_IP_Private.h"\r
49 #include "NetworkInterface.h"\r
50 \r
51 /* For an Ethernet interrupt to be able to obtain a network buffer there must\r
52 be at least this number of buffers available. */\r
53 #define ipINTERRUPT_BUFFER_GET_THRESHOLD        ( 3 )\r
54 \r
55 /* A list of free (available) xNetworkBufferDescriptor_t structures. */\r
56 static xList xFreeBuffersList;\r
57 \r
58 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the\r
59 system.  All the network buffers referenced from xFreeBuffersList exist in this\r
60 array.  The array is not accessed directly except during initialisation, when\r
61 the xFreeBuffersList is filled (as all the buffers are free when the system is\r
62 booted). */\r
63 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];\r
64 \r
65 /* The semaphore used to obtain network buffers. */\r
66 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 BaseType_t xNetworkBuffersInitialise( void )\r
71 {\r
72 BaseType_t xReturn, x;\r
73 \r
74         /* Only initialise the buffers and their associated kernel objects if they\r
75         have not been initialised before. */\r
76         if( xNetworkBufferSemaphore == NULL )\r
77         {\r
78                 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );\r
79                 configASSERT( xNetworkBufferSemaphore );\r
80 \r
81                 if( xNetworkBufferSemaphore != NULL )\r
82                 {\r
83                         vListInitialise( &xFreeBuffersList );\r
84 \r
85                         /* Initialise all the network buffers.  The buffer storage comes\r
86                         from the network interface, and different hardware has different\r
87                         requirements. */\r
88                         vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );\r
89                         for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )\r
90                         {\r
91                                 /* Initialise and set the owner of the buffer list items. */\r
92                                 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );\r
93                                 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );\r
94 \r
95                                 /* Currently, all buffers are available for use. */\r
96                                 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );\r
97                         }\r
98                 }\r
99         }\r
100 \r
101         if( xNetworkBufferSemaphore == NULL )\r
102         {\r
103                 xReturn = pdFAIL;\r
104         }\r
105         else\r
106         {\r
107                 xReturn = pdPASS;\r
108         }\r
109 \r
110         return xReturn;\r
111 }\r
112 /*-----------------------------------------------------------*/\r
113 \r
114 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )\r
115 {\r
116 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
117 \r
118         /*_RB_ The current implementation only has a single size memory block, so\r
119         the requested size parameter is not used (yet). */\r
120         ( void ) xRequestedSizeBytes;\r
121 \r
122         /* If there is a semaphore available, there is a network buffer available. */\r
123         if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
124         {\r
125                 /* Protect the structure as it is accessed from tasks and interrupts. */\r
126                 taskENTER_CRITICAL();\r
127                 {\r
128                         pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
129                         uxListRemove( &( pxReturn->xBufferListItem ) );\r
130                 }\r
131                 taskEXIT_CRITICAL();\r
132                 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
133         }\r
134         else\r
135         {\r
136                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
137         }\r
138 \r
139         return pxReturn;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )\r
144 {\r
145 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
146 UBaseType_t uxSavedInterruptStatus;\r
147 \r
148         /*_RB_ The current implementation only has a single size memory block, so\r
149         the requested size parameter is not used (yet). */\r
150         ( void ) xRequestedSizeBytes;\r
151 \r
152         /* If there is a semaphore available then there is a buffer available, but,\r
153         as this is called from an interrupt, only take a buffer if there are at\r
154         least ipINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining.  This prevents,\r
155         to a certain degree at least, a rapidly executing interrupt exhausting\r
156         buffer and in so doing preventing tasks from continuing. */\r
157         if( uxQueueMessagesWaitingFromISR( ( xQueueHandle ) xNetworkBufferSemaphore ) > ipINTERRUPT_BUFFER_GET_THRESHOLD )\r
158         {\r
159                 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )\r
160                 {\r
161                         /* Protect the structure as it is accessed from tasks and interrupts. */\r
162                         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
163                         {\r
164                                 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
165                                 uxListRemove( &( pxReturn->xBufferListItem ) );\r
166                         }\r
167                         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
168 \r
169                         iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );\r
170                 }\r
171         }\r
172 \r
173         if( pxReturn == NULL )\r
174         {\r
175                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();\r
176         }\r
177 \r
178         return pxReturn;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 BaseType_t vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
183 {\r
184 UBaseType_t uxSavedInterruptStatus;\r
185 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
186 \r
187         /* Ensure the buffer is returned to the list of free buffers before the\r
188         counting semaphore is 'given' to say a buffer is available. */\r
189         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
190         {\r
191                 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
192         }\r
193         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
194 \r
195         xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );\r
196         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
197 \r
198         return xHigherPriorityTaskWoken;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
203 {\r
204 BaseType_t xListItemAlreadyInFreeList;\r
205 \r
206         /* Ensure the buffer is returned to the list of free buffers before the\r
207         counting semaphore is 'given' to say a buffer is available. */\r
208         taskENTER_CRITICAL();\r
209         {\r
210                 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
211 \r
212                 if( xListItemAlreadyInFreeList == pdFALSE )\r
213                 {\r
214                         vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
215                 }\r
216 \r
217                 configASSERT( xListItemAlreadyInFreeList == pdFALSE );\r
218         }\r
219         taskEXIT_CRITICAL();\r
220 \r
221         xSemaphoreGive( xNetworkBufferSemaphore );\r
222         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 #if( ipconfigINCLUDE_TEST_CODE == 1 )\r
227 \r
228 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )\r
229 {\r
230         return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
231 }\r
232 \r
233 #endif /* ipconfigINCLUDE_TEST_CODE */\r