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