]> begriffs open source - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/BufferManagement/BufferAllocation_2.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_2.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  * 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 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR\r
38 THAT WILL FRAGMENT THE HEAP MEMORY.  For example, heap_2 must not be used,\r
39 heap_4 can be used. */\r
40 \r
41 \r
42 /* Standard includes. */\r
43 #include <stdint.h>\r
44 \r
45 /* FreeRTOS includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 #include "semphr.h"\r
49 \r
50 /* FreeRTOS+UDP includes. */\r
51 #include "FreeRTOS_UDP_IP.h"\r
52 #include "FreeRTOS_IP_Private.h"\r
53 #include "NetworkInterface.h"\r
54 \r
55 /* For an Ethernet interrupt to be able to obtain a network buffer there must\r
56 be at least this number of buffers available. */\r
57 #define ipINTERRUPT_BUFFER_GET_THRESHOLD        ( 3 )\r
58 \r
59 /* A list of free (available) xNetworkBufferDescriptor_t structures. */\r
60 static xList xFreeBuffersList;\r
61 \r
62 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the\r
63 system.  All the network buffers referenced from xFreeBuffersList exist in this\r
64 array.  The array is not accessed directly except during initialisation, when\r
65 the xFreeBuffersList is filled (as all the buffers are free when the system is\r
66 booted). */\r
67 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];\r
68 \r
69 /* The semaphore used to obtain network buffers. */\r
70 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 BaseType_t xNetworkBuffersInitialise( void )\r
75 {\r
76 BaseType_t xReturn, x;\r
77 \r
78         /* Only initialise the buffers and their associated kernel objects if they\r
79         have not been initialised before. */\r
80         if( xNetworkBufferSemaphore == NULL )\r
81         {\r
82                 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );\r
83                 configASSERT( xNetworkBufferSemaphore );\r
84                 vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );\r
85 \r
86                 /* If the trace recorder code is included name the semaphore for viewing\r
87                 in FreeRTOS+Trace.  */\r
88                 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
89                 {\r
90                         extern xQueueHandle xNetworkEventQueue;\r
91                         vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
92                         vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );\r
93                 }\r
94                 #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
95 \r
96                 if( xNetworkBufferSemaphore != NULL )\r
97                 {\r
98                         vListInitialise( &xFreeBuffersList );\r
99 \r
100                         /* Initialise all the network buffers.  No storage is allocated to\r
101                         the buffers yet. */\r
102                         for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )\r
103                         {\r
104                                 /* Initialise and set the owner of the buffer list items. */\r
105                                 xNetworkBuffers[ x ].pucEthernetBuffer = NULL;\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 uint8_t *pucEthernetBufferGet( size_t *pxRequestedSizeBytes )\r
129 {\r
130 uint8_t *pucEthernetBuffer;\r
131 \r
132         if( *pxRequestedSizeBytes < sizeof( xARPPacket_t ) )\r
133         {\r
134                 /* Buffers must be at least large enough to hold ARP packets, otherwise\r
135                 nothing can be done. */\r
136                 *pxRequestedSizeBytes = sizeof( xARPPacket_t );\r
137         }\r
138 \r
139         /* Allocate a buffer large enough to store the requested Ethernet frame size\r
140         and a pointer to a network buffer structure (hence the addition of\r
141         ipBUFFER_PADDING bytes). */\r
142         pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( *pxRequestedSizeBytes + ipBUFFER_PADDING );\r
143 \r
144         /* Enough space is left at the start of the buffer to place a pointer to\r
145         the network buffer structure that references this Ethernet buffer.  Return\r
146         a pointer to the start of the Ethernet buffer itself. */\r
147         pucEthernetBuffer += ipBUFFER_PADDING;\r
148 \r
149         return pucEthernetBuffer;\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 void vEthernetBufferRelease( uint8_t *pucEthernetBuffer )\r
154 {\r
155         /* There is space before the Ethernet buffer in which a pointer to the\r
156         network buffer that references this Ethernet buffer is stored.  Remove the\r
157         space before freeing the buffer. */\r
158         if( pucEthernetBuffer != NULL )\r
159         {\r
160                 pucEthernetBuffer -= ipBUFFER_PADDING;\r
161                 vPortFree( ( void * ) pucEthernetBuffer );\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )\r
167 {\r
168 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
169 \r
170         if( ( xRequestedSizeBytes != 0 ) && ( xRequestedSizeBytes < sizeof( xARPPacket_t ) ) )\r
171         {\r
172                 /* ARP packets can replace application packets, so the storage must be\r
173                 at least large enough to hold an ARP. */\r
174                 xRequestedSizeBytes = sizeof( xARPPacket_t );\r
175         }\r
176 \r
177         /* If there is a semaphore available, there is a network buffer available. */\r
178         if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
179         {\r
180                 /* Protect the structure as it is accessed from tasks and interrupts. */\r
181                 taskENTER_CRITICAL();\r
182                 {\r
183                         pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
184                         uxListRemove( &( pxReturn->xBufferListItem ) );\r
185                 }\r
186                 taskEXIT_CRITICAL();\r
187 \r
188                 /* Allocate storage of exactly the requested size to the buffer. */\r
189                 configASSERT( pxReturn->pucEthernetBuffer == NULL );\r
190                 if( xRequestedSizeBytes > 0 )\r
191                 {\r
192                         /* Extra space is obtained so a pointer to the network buffer can\r
193                         be stored at the beginning of the buffer. */\r
194                         pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );\r
195 \r
196                         if( pxReturn->pucEthernetBuffer == NULL )\r
197                         {\r
198                                 /* The attempt to allocate storage for the buffer payload failed,\r
199                                 so the network buffer structure cannot be used and must be\r
200                                 released. */\r
201                                 vNetworkBufferRelease( pxReturn );\r
202                                 pxReturn = NULL;\r
203                         }\r
204                         else\r
205                         {\r
206                                 /* Store a pointer to the network buffer structure in the\r
207                                 buffer storage area, then move the buffer pointer on past the\r
208                                 stored pointer so the pointer value is not overwritten by the\r
209                                 application when the buffer is used. */\r
210                                 *( ( xNetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;\r
211                                 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;\r
212                                 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
213 \r
214                                 /* Store the actual size of the allocated buffer, which may be\r
215                                 greater than the requested size. */\r
216                                 pxReturn->xDataLength = xRequestedSizeBytes;\r
217                         }\r
218                 }\r
219                 else\r
220                 {\r
221                         iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
222                 }\r
223         }\r
224 \r
225         if( pxReturn == NULL )\r
226         {\r
227                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
228         }\r
229 \r
230         return pxReturn;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
235 {\r
236 BaseType_t xListItemAlreadyInFreeList;\r
237 \r
238         /* Ensure the buffer is returned to the list of free buffers before the\r
239         counting semaphore is 'given' to say a buffer is available.  Release the\r
240         storage allocated to the buffer payload.  THIS FILE SHOULD NOT BE USED\r
241         IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP\r
242         MEMORY.  For example, heap_2 must not be used, heap_4 can be used. */\r
243         vEthernetBufferRelease( pxNetworkBuffer->pucEthernetBuffer );\r
244         pxNetworkBuffer->pucEthernetBuffer = NULL;\r
245 \r
246         taskENTER_CRITICAL();\r
247         {\r
248                 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
249 \r
250                 if( xListItemAlreadyInFreeList == pdFALSE )\r
251                 {\r
252                         vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
253                 }\r
254 \r
255                 configASSERT( xListItemAlreadyInFreeList == pdFALSE );\r
256         }\r
257         taskEXIT_CRITICAL();\r
258 \r
259         xSemaphoreGive( xNetworkBufferSemaphore );\r
260         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 #if( ipconfigINCLUDE_TEST_CODE == 1 )\r
265 \r
266 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )\r
267 {\r
268         return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
269 }\r
270 \r
271 #endif /* ipconfigINCLUDE_TEST_CODE */\r