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