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