]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/BufferManagement/BufferAllocation_2.c
Clarify license blurb at the top of the FreeRTOS+UDP and FreeRTOS+CLI source files.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / BufferManagement / BufferAllocation_2.c
1 /*\r
2  * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.\r
3  *\r
4  * This file is part of the FreeRTOS+UDP distribution.  The FreeRTOS+UDP license\r
5  * terms are different to the FreeRTOS license terms.\r
6  *\r
7  * FreeRTOS+UDP uses a dual license model that allows the software to be used\r
8  * under a pure GPL open source license (as opposed to the modified GPL license\r
9  * under which FreeRTOS is distributed) or a commercial license.  Details of\r
10  * both license options follow:\r
11  *\r
12  * - Open source licensing -\r
13  * FreeRTOS+UDP is a free download and may be used, modified, evaluated and\r
14  * distributed without charge provided the user adheres to version two of the\r
15  * GNU General Public License (GPL) and does not remove the copyright notice or\r
16  * this text.  The GPL V2 text is available on the gnu.org web site, and on the\r
17  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.\r
18  *\r
19  * - Commercial licensing -\r
20  * Businesses and individuals that for commercial or other reasons cannot comply\r
21  * with the terms of the GPL V2 license must obtain a commercial license before \r
22  * incorporating FreeRTOS+UDP into proprietary software for distribution in any \r
23  * form.  Commercial licenses can be purchased from http://shop.freertos.org/udp \r
24  * and do not require any source files to be changed.\r
25  *\r
26  * FreeRTOS+UDP is distributed in the hope that it will be useful.  You cannot\r
27  * use FreeRTOS+UDP unless you agree that you use the software 'as is'.\r
28  * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied\r
29  * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
30  * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
31  * implied, expressed, or statutory.\r
32  *\r
33  * 1 tab == 4 spaces!\r
34  *\r
35  * http://www.FreeRTOS.org\r
36  * http://www.FreeRTOS.org/udp\r
37  *\r
38  */\r
39 \r
40 /******************************************************************************\r
41  *\r
42  * See the following web page for essential buffer allocation scheme usage and \r
43  * configuration details:\r
44  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml\r
45  *\r
46  ******************************************************************************/\r
47 \r
48 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR\r
49 THAT WILL FRAGMENT THE HEAP MEMORY.  For example, heap_2 must not be used,\r
50 heap_4 can be used. */\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 "FreeRTOS_IP_Private.h"\r
64 #include "NetworkInterface.h"\r
65 \r
66 /* For an Ethernet interrupt to be able to obtain a network buffer there must\r
67 be at least this number of buffers available. */\r
68 #define ipINTERRUPT_BUFFER_GET_THRESHOLD        ( 3 )\r
69 \r
70 /* A list of free (available) xNetworkBufferDescriptor_t structures. */\r
71 static xList xFreeBuffersList;\r
72 \r
73 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the\r
74 system.  All the network buffers referenced from xFreeBuffersList exist in this\r
75 array.  The array is not accessed directly except during initialisation, when\r
76 the xFreeBuffersList is filled (as all the buffers are free when the system is\r
77 booted). */\r
78 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];\r
79 \r
80 /* The semaphore used to obtain network buffers. */\r
81 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 portBASE_TYPE xNetworkBuffersInitialise( void )\r
86 {\r
87 portBASE_TYPE xReturn, x;\r
88 \r
89         /* Only initialise the buffers and their associated kernel objects if they\r
90         have not been initialised before. */\r
91         if( xNetworkBufferSemaphore == NULL )\r
92         {\r
93                 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );\r
94                 configASSERT( xNetworkBufferSemaphore );\r
95                 vQueueAddToRegistry( xNetworkBufferSemaphore, ( signed char * ) "NetBufSem" );\r
96 \r
97                 /* If the trace recorder code is included name the semaphore for viewing\r
98                 in FreeRTOS+Trace.  */\r
99                 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
100                 {\r
101                         extern xQueueHandle xNetworkEventQueue;\r
102                         vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
103                         vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );\r
104                 }\r
105                 #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
106 \r
107                 if( xNetworkBufferSemaphore != NULL )\r
108                 {\r
109                         vListInitialise( &xFreeBuffersList );\r
110 \r
111                         /* Initialise all the network buffers.  No storage is allocated to\r
112                         the buffers yet. */\r
113                         for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )\r
114                         {\r
115                                 /* Initialise and set the owner of the buffer list items. */\r
116                                 xNetworkBuffers[ x ].pucEthernetBuffer = NULL;\r
117                                 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );\r
118                                 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );\r
119 \r
120                                 /* Currently, all buffers are available for use. */\r
121                                 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );\r
122                         }\r
123                 }\r
124         }\r
125 \r
126         if( xNetworkBufferSemaphore == NULL )\r
127         {\r
128                 xReturn = pdFAIL;\r
129         }\r
130         else\r
131         {\r
132                 xReturn = pdPASS;\r
133         }\r
134 \r
135         return xReturn;\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 uint8_t *pucEthernetBufferGet( size_t *pxRequestedSizeBytes )\r
140 {\r
141 uint8_t *pucEthernetBuffer;\r
142 \r
143         if( *pxRequestedSizeBytes < sizeof( xARPPacket_t ) )\r
144         {\r
145                 /* Buffers must be at least large enough to hold ARP packets, otherwise\r
146                 nothing can be done. */\r
147                 *pxRequestedSizeBytes = sizeof( xARPPacket_t );\r
148         }\r
149 \r
150         /* Allocate a buffer large enough to store the requested Ethernet frame size\r
151         and a pointer to a network buffer structure (hence the addition of\r
152         ipBUFFER_PADDING bytes). */\r
153         pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( *pxRequestedSizeBytes + ipBUFFER_PADDING );\r
154 \r
155         /* Enough space is left at the start of the buffer to place a pointer to\r
156         the network buffer structure that references this Ethernet buffer.  Return\r
157         a pointer to the start of the Ethernet buffer itself. */\r
158         pucEthernetBuffer += ipBUFFER_PADDING;\r
159 \r
160         return pucEthernetBuffer;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vEthernetBufferRelease( uint8_t *pucEthernetBuffer )\r
165 {\r
166         /* There is space before the Ethernet buffer in which a pointer to the\r
167         network buffer that references this Ethernet buffer is stored.  Remove the\r
168         space before freeing the buffer. */\r
169         if( pucEthernetBuffer != NULL )\r
170         {\r
171                 pucEthernetBuffer -= ipBUFFER_PADDING;\r
172                 vPortFree( ( void * ) pucEthernetBuffer );\r
173         }\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )\r
178 {\r
179 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
180 \r
181         if( ( xRequestedSizeBytes != 0 ) && ( xRequestedSizeBytes < sizeof( xARPPacket_t ) ) )\r
182         {\r
183                 /* ARP packets can replace application packets, so the storage must be\r
184                 at least large enough to hold an ARP. */\r
185                 xRequestedSizeBytes = sizeof( xARPPacket_t );\r
186         }\r
187 \r
188         /* If there is a semaphore available, there is a network buffer available. */\r
189         if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
190         {\r
191                 /* Protect the structure as it is accessed from tasks and interrupts. */\r
192                 taskENTER_CRITICAL();\r
193                 {\r
194                         pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
195                         uxListRemove( &( pxReturn->xBufferListItem ) );\r
196                 }\r
197                 taskEXIT_CRITICAL();\r
198 \r
199                 /* Allocate storage of exactly the requested size to the buffer. */\r
200                 configASSERT( pxReturn->pucEthernetBuffer == NULL );\r
201                 if( xRequestedSizeBytes > 0 )\r
202                 {\r
203                         /* Extra space is obtained so a pointer to the network buffer can\r
204                         be stored at the beginning of the buffer. */\r
205                         pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );\r
206 \r
207                         if( pxReturn->pucEthernetBuffer == NULL )\r
208                         {\r
209                                 /* The attempt to allocate storage for the buffer payload failed,\r
210                                 so the network buffer structure cannot be used and must be\r
211                                 released. */\r
212                                 vNetworkBufferRelease( pxReturn );\r
213                                 pxReturn = NULL;\r
214                         }\r
215                         else\r
216                         {\r
217                                 /* Store a pointer to the network buffer structure in the\r
218                                 buffer storage area, then move the buffer pointer on past the\r
219                                 stored pointer so the pointer value is not overwritten by the\r
220                                 application when the buffer is used. */\r
221                                 *( ( xNetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;\r
222                                 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;\r
223                                 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
224 \r
225                                 /* Store the actual size of the allocated buffer, which may be\r
226                                 greater than the requested size. */\r
227                                 pxReturn->xDataLength = xRequestedSizeBytes;\r
228                         }\r
229                 }\r
230                 else\r
231                 {\r
232                         iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
233                 }\r
234         }\r
235 \r
236         if( pxReturn == NULL )\r
237         {\r
238                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
239         }\r
240 \r
241         return pxReturn;\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
246 {\r
247 portBASE_TYPE xListItemAlreadyInFreeList;\r
248 \r
249         /* Ensure the buffer is returned to the list of free buffers before the\r
250         counting semaphore is 'given' to say a buffer is available.  Release the\r
251         storage allocated to the buffer payload.  THIS FILE SHOULD NOT BE USED\r
252         IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP\r
253         MEMORY.  For example, heap_2 must not be used, heap_4 can be used. */\r
254         vEthernetBufferRelease( pxNetworkBuffer->pucEthernetBuffer );\r
255         pxNetworkBuffer->pucEthernetBuffer = NULL;\r
256 \r
257         taskENTER_CRITICAL();\r
258         {\r
259                 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
260 \r
261                 if( xListItemAlreadyInFreeList == pdFALSE )\r
262                 {\r
263                         vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
264                 }\r
265 \r
266                 configASSERT( xListItemAlreadyInFreeList == pdFALSE );\r
267         }\r
268         taskEXIT_CRITICAL();\r
269 \r
270         xSemaphoreGive( xNetworkBufferSemaphore );\r
271         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 #if( ipconfigINCLUDE_TEST_CODE == 1 )\r
276 \r
277 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )\r
278 {\r
279         return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
280 }\r
281 \r
282 #endif /* ipconfigINCLUDE_TEST_CODE */\r