]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/pic32mzef/BufferAllocation_2.c
Sync FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP with the version in GitHub at (23665258ca...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / pic32mzef / BufferAllocation_2.c
1 /*\r
2  * FreeRTOS+TCP Labs Build 160919 (C) 2016 Real Time Engineers ltd.\r
3  * Authors include Hein Tibosch and Richard Barry\r
4  *\r
5  *******************************************************************************\r
6  ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***\r
7  ***                                                                         ***\r
8  ***                                                                         ***\r
9  ***   FREERTOS+TCP IS STILL IN THE LAB (mainly because the FTP and HTTP     ***\r
10  ***   demos have a dependency on FreeRTOS+FAT, which is only in the Labs    ***\r
11  ***   download):                                                            ***\r
12  ***                                                                         ***\r
13  ***   FreeRTOS+TCP is functional and has been used in commercial products   ***\r
14  ***   for some time.  Be aware however that we are still refining its       ***\r
15  ***   design, the source code does not yet quite conform to the strict      ***\r
16  ***   coding and style standards mandated by Real Time Engineers ltd., and  ***\r
17  ***   the documentation and testing is not necessarily complete.            ***\r
18  ***                                                                         ***\r
19  ***   PLEASE REPORT EXPERIENCES USING THE SUPPORT RESOURCES FOUND ON THE    ***\r
20  ***   URL: http://www.FreeRTOS.org/contact  Active early adopters may, at   ***\r
21  ***   the sole discretion of Real Time Engineers Ltd., be offered versions  ***\r
22  ***   under a license other than that described below.                      ***\r
23  ***                                                                         ***\r
24  ***                                                                         ***\r
25  ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***\r
26  *******************************************************************************\r
27  *\r
28  * FreeRTOS+TCP can be used under two different free open source licenses.  The\r
29  * license that applies is dependent on the processor on which FreeRTOS+TCP is\r
30  * executed, as follows:\r
31  *\r
32  * If FreeRTOS+TCP is executed on one of the processors listed under the Special\r
33  * License Arrangements heading of the FreeRTOS+TCP license information web\r
34  * page, then it can be used under the terms of the FreeRTOS Open Source\r
35  * License.  If FreeRTOS+TCP is used on any other processor, then it can be used\r
36  * under the terms of the GNU General Public License V2.  Links to the relevant\r
37  * licenses follow:\r
38  *\r
39  * The FreeRTOS+TCP License Information Page: http://www.FreeRTOS.org/tcp_license\r
40  * The FreeRTOS Open Source License: http://www.FreeRTOS.org/license\r
41  * The GNU General Public License Version 2: http://www.FreeRTOS.org/gpl-2.0.txt\r
42  *\r
43  * FreeRTOS+TCP is distributed in the hope that it will be useful.  You cannot\r
44  * use FreeRTOS+TCP unless you agree that you use the software 'as is'.\r
45  * FreeRTOS+TCP is provided WITHOUT ANY WARRANTY; without even the implied\r
46  * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
47  * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
48  * implied, expressed, or statutory.\r
49  *\r
50  * 1 tab == 4 spaces!\r
51  *\r
52  * http://www.FreeRTOS.org\r
53  * http://www.FreeRTOS.org/plus\r
54  * http://www.FreeRTOS.org/labs\r
55  *\r
56  */\r
57 \r
58 /******************************************************************************\r
59 *\r
60 * See the following web page for essential buffer allocation scheme usage and\r
61 * configuration details:\r
62 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html\r
63 *\r
64 ******************************************************************************/\r
65 \r
66 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR\r
67  * THAT WILL FRAGMENT THE HEAP MEMORY.  For example, heap_2 must not be used,\r
68  * heap_4 can be used. */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdint.h>\r
72 \r
73 /* FreeRTOS includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "semphr.h"\r
77 \r
78 /* FreeRTOS+TCP includes. */\r
79 #include "FreeRTOS_IP.h"\r
80 #include "FreeRTOS_UDP_IP.h"\r
81 #include "FreeRTOS_IP_Private.h"\r
82 #include "NetworkInterface.h"\r
83 #include "NetworkBufferManagement.h"\r
84 \r
85 #include "tcpip/tcpip.h"\r
86 #include "tcpip/src/tcpip_private.h"\r
87 \r
88 #include "NetworkConfig.h"\r
89 \r
90 /* The obtained network buffer must be large enough to hold a packet that might\r
91  * replace the packet that was requested to be sent. */\r
92 #if ipconfigUSE_TCP == 1\r
93     #define baMINIMAL_BUFFER_SIZE    sizeof( TCPPacket_t )\r
94 #else\r
95     #define baMINIMAL_BUFFER_SIZE    sizeof( ARPPacket_t )\r
96 #endif /* ipconfigUSE_TCP == 1 */\r
97 \r
98 /*_RB_ This is too complex not to have an explanation. */\r
99 #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )\r
100     #define ASSERT_CONCAT_( a, b )    a ## b\r
101     #define ASSERT_CONCAT( a, b )     ASSERT_CONCAT_( a, b )\r
102     #define STATIC_ASSERT( e ) \\r
103     ; enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = 1 / ( !!( e ) ) }\r
104 \r
105     STATIC_ASSERT( ipconfigETHERNET_MINIMUM_PACKET_BYTES <= baMINIMAL_BUFFER_SIZE );\r
106 #endif\r
107 \r
108 /* A list of free (available) NetworkBufferDescriptor_t structures. */\r
109 static List_t xFreeBuffersList;\r
110 \r
111 /* Some statistics about the use of buffers. */\r
112 static size_t uxMinimumFreeNetworkBuffers;\r
113 \r
114 /* Declares the pool of NetworkBufferDescriptor_t structures that are available\r
115  * to the system.  All the network buffers referenced from xFreeBuffersList exist\r
116  * in this array.  The array is not accessed directly except during initialisation,\r
117  * when the xFreeBuffersList is filled (as all the buffers are free when the system\r
118  * is booted). */\r
119 static NetworkBufferDescriptor_t xNetworkBufferDescriptors[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];\r
120 \r
121 /* This constant is defined as false to let FreeRTOS_TCP_IP.c know that the\r
122  * network buffers have a variable size: resizing may be necessary */\r
123 const BaseType_t xBufferAllocFixedSize = pdFALSE;\r
124 \r
125 /* The semaphore used to obtain network buffers. */\r
126 static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 #ifdef PIC32_USE_ETHERNET\r
131 \r
132     /* PIC32 specific stuff */\r
133     /* */\r
134 \r
135     /* MAC packet acknowledgment, once MAC is done with it */\r
136         static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,\r
137                                                 const void * param );\r
138 \r
139     /* allocates a MAC packet that holds a data buffer that can be used by both: */\r
140     /*  - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */\r
141     /*  - the Harmony MAC driver: TCPIP_MAC_PACKET->pDSeg->segLoad */\r
142     /* from the beginning of the buffer: */\r
143     /*      - 4 bytes pointer to the network descriptor (FreeRTOS) */\r
144     /*      - 4 bytes pointer to the MAC packet (pic32_NetworkInterface.c) */\r
145     /*      - 2 bytes offset from the MAC packet (Harmony MAC driver: segLoadOffset) */\r
146     /* */\r
147     /* NOTE: segLoadLen should NOT include: */\r
148     /*          - the TCPIP_MAC_FRAME_OFFSET (== ipBUFFER_PADDING which should be == 10!) */\r
149     /*          - the sizeof(TCPIP_MAC_ETHERNET_HEADER) */\r
150     /*       These are added by the MAC packet allocation! */\r
151     /* */\r
152     static uint8_t * PIC32_PktAlloc( uint16_t pktLen,\r
153                                      uint16_t segLoadLen,\r
154                                      TCPIP_MAC_PACKET_ACK_FUNC ackF,\r
155                                      TCPIP_MAC_PACKET ** pPtrPkt )\r
156     {\r
157         uint8_t * pBuff = 0;\r
158 \r
159         /* allocate standard packet */\r
160         TCPIP_MAC_PACKET * pPkt = TCPIP_PKT_PacketAlloc( pktLen, segLoadLen, 0 );\r
161 \r
162         /* set the MAC packet pointer in the packet */\r
163         if( pPkt != 0 )\r
164         {\r
165             pBuff = pPkt->pDSeg->segLoad;\r
166             TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pBuff - PIC32_BUFFER_PKT_PTR_OSSET );\r
167             configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
168             *ppkt = pPkt; /* store the packet it comes from */\r
169             pPkt->ackFunc = ackF;\r
170             pPkt->ackParam = 0;\r
171         }\r
172 \r
173         if( pPtrPkt != 0 )\r
174         {\r
175             *pPtrPkt = pPkt;\r
176         }\r
177 \r
178         return pBuff;\r
179     }\r
180 \r
181 \r
182 \r
183     /* standard PIC32 MAC allocation function for a MAC packet */\r
184     /* this packet saves room for the FreeRTOS network descriptor */\r
185     /* at the beginning of the data buffer */\r
186     /* see NetworkBufferAllocate */\r
187     /* Note: flags parameter is ignored since that's used in the Harmony stack only */\r
188     TCPIP_MAC_PACKET * PIC32_MacPacketAllocate( uint16_t pktLen,\r
189                                                 uint16_t segLoadLen,\r
190                                                 TCPIP_MAC_PACKET_FLAGS flags )\r
191     {\r
192         TCPIP_MAC_PACKET * pPkt;\r
193 \r
194         PIC32_PktAlloc( pktLen, segLoadLen, 0, &pPkt );\r
195 \r
196         return pPkt;\r
197     }\r
198 \r
199     /* standard PIC32 MAC packet acknowledgment */\r
200     /* function called once MAC is done with it */\r
201     static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,\r
202                                             const void * param )\r
203     {\r
204         configASSERT( ( pPkt != 0 ) );\r
205 \r
206         TCPIP_PKT_PacketFree( pPkt );\r
207 \r
208         return false;\r
209     }\r
210 \r
211     /* associates the current MAC packet with a network descriptor */\r
212     /* mainly for RX packet */\r
213     void PIC32_MacAssociate( TCPIP_MAC_PACKET * pRxPkt,\r
214                              NetworkBufferDescriptor_t * pxBufferDescriptor,\r
215                              size_t pktLength )\r
216     {\r
217         uint8_t * pPktBuff = pRxPkt->pDSeg->segLoad;\r
218 \r
219         pxBufferDescriptor->pucEthernetBuffer = pPktBuff;\r
220         pxBufferDescriptor->xDataLength = pktLength;\r
221 \r
222         /* make sure this is a properly allocated packet */\r
223         TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pPktBuff - PIC32_BUFFER_PKT_PTR_OSSET );\r
224         configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
225 \r
226         if( *ppkt != pRxPkt )\r
227         {\r
228             configASSERT( false );\r
229         }\r
230 \r
231         /* set the proper descriptor info */\r
232         NetworkBufferDescriptor_t ** ppDcpt = ( NetworkBufferDescriptor_t ** ) ( pPktBuff - ipBUFFER_PADDING );\r
233         configASSERT( ( ( uint32_t ) ppDcpt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
234         *ppDcpt = pxBufferDescriptor;\r
235     }\r
236 \r
237     /* debug functionality */\r
238     void PIC32_MacPacketOrphan( TCPIP_MAC_PACKET * pPkt )\r
239     {\r
240         TCPIP_PKT_PacketFree( pPkt );\r
241         configASSERT( false );\r
242     }\r
243 \r
244     /* FreeRTOS allocation functions */\r
245 \r
246     /* allocates a buffer that can be used by both: */\r
247     /*  - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */\r
248     /*  - the Harmony MAC driver: TCPIP_MAC_PACKET */\r
249     /*  See PIC32_PktAlloc for details */\r
250     /* */\r
251     /* NOTE: reqLength should NOT include the ipBUFFER_PADDING (which should be == 10!) */\r
252     /*       or the sizeof(TCPIP_MAC_ETHERNET_HEADER) */\r
253     /*       These are added by the MAC packet allocation! */\r
254     /* */\r
255     uint8_t * NetworkBufferAllocate( size_t reqLength )\r
256     {\r
257         return PIC32_PktAlloc( sizeof( TCPIP_MAC_PACKET ), reqLength, PIC32_MacPacketAcknowledge, 0 );\r
258     }\r
259 \r
260     /* deallocates a network buffer previously allocated */\r
261     /* with NetworkBufferAllocate */\r
262     void NetworkBufferFree( uint8_t * pNetworkBuffer )\r
263     {\r
264         if( pNetworkBuffer != 0 )\r
265         {\r
266             TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pNetworkBuffer - PIC32_BUFFER_PKT_PTR_OSSET );\r
267             configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
268             TCPIP_MAC_PACKET * pPkt = *ppkt;\r
269             configASSERT( ( pPkt != 0 ) );\r
270 \r
271             if( pPkt->ackFunc != 0 )\r
272             {\r
273                 ( *pPkt->ackFunc )( pPkt, pPkt->ackParam );\r
274             }\r
275             else\r
276             { /* ??? */\r
277                 PIC32_MacPacketOrphan( pPkt );\r
278             }\r
279         }\r
280     }\r
281 \r
282 #endif /* #ifdef PIC32_USE_ETHERNET */\r
283 \r
284 /*-----------------------------------------------------------*/\r
285 \r
286 BaseType_t xNetworkBuffersInitialise( void )\r
287 {\r
288     BaseType_t xReturn, x;\r
289 \r
290     /* Only initialise the buffers and their associated kernel objects if they\r
291      * have not been initialised before. */\r
292     if( xNetworkBufferSemaphore == NULL )\r
293     {\r
294         xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );\r
295         configASSERT( xNetworkBufferSemaphore );\r
296 \r
297         if( xNetworkBufferSemaphore != NULL )\r
298         {\r
299             #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
300                 {\r
301                     vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );\r
302                 }\r
303             #endif /* configQUEUE_REGISTRY_SIZE */\r
304 \r
305             /* If the trace recorder code is included name the semaphore for viewing\r
306              * in FreeRTOS+Trace.  */\r
307             #if ( ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 )\r
308                 {\r
309                     extern QueueHandle_t xNetworkEventQueue;\r
310                     vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
311                     vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );\r
312                 }\r
313             #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
314 \r
315             vListInitialise( &xFreeBuffersList );\r
316 \r
317             /* Initialise all the network buffers.  No storage is allocated to\r
318              * the buffers yet. */\r
319             for( x = 0; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )\r
320             {\r
321                 /* Initialise and set the owner of the buffer list items. */\r
322                 xNetworkBufferDescriptors[ x ].pucEthernetBuffer = NULL;\r
323                 vListInitialiseItem( &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );\r
324                 listSET_LIST_ITEM_OWNER( &( xNetworkBufferDescriptors[ x ].xBufferListItem ), &xNetworkBufferDescriptors[ x ] );\r
325 \r
326                 /* Currently, all buffers are available for use. */\r
327                 vListInsert( &xFreeBuffersList, &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );\r
328             }\r
329 \r
330             uxMinimumFreeNetworkBuffers = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;\r
331         }\r
332     }\r
333 \r
334     if( xNetworkBufferSemaphore == NULL )\r
335     {\r
336         xReturn = pdFAIL;\r
337     }\r
338     else\r
339     {\r
340         xReturn = pdPASS;\r
341     }\r
342 \r
343     return xReturn;\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes )\r
348 {\r
349     uint8_t * pucEthernetBuffer;\r
350     size_t xSize = *pxRequestedSizeBytes;\r
351 \r
352     if( xSize < baMINIMAL_BUFFER_SIZE )\r
353     {\r
354         /* Buffers must be at least large enough to hold a TCP-packet with\r
355          * headers, or an ARP packet, in case TCP is not included. */\r
356         xSize = baMINIMAL_BUFFER_SIZE;\r
357     }\r
358 \r
359     /* Round up xSize to the nearest multiple of N bytes,\r
360      * where N equals 'sizeof( size_t )'. */\r
361     if( ( xSize & ( sizeof( size_t ) - 1u ) ) != 0u )\r
362     {\r
363         xSize = ( xSize | ( sizeof( size_t ) - 1u ) ) + 1u;\r
364     }\r
365 \r
366     *pxRequestedSizeBytes = xSize;\r
367 \r
368     /* Allocate a buffer large enough to store the requested Ethernet frame size\r
369      * and a pointer to a network buffer structure (hence the addition of\r
370      * ipBUFFER_PADDING bytes). */\r
371 \r
372     #ifdef PIC32_USE_ETHERNET\r
373         pucEthernetBuffer = NetworkBufferAllocate( xSize - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );\r
374     #else\r
375         pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xSize + ipBUFFER_PADDING );\r
376     #endif /* #ifdef PIC32_USE_ETHERNET */\r
377 \r
378     configASSERT( pucEthernetBuffer );\r
379 \r
380     if( pucEthernetBuffer != NULL )\r
381     {\r
382         /* Enough space is left at the start of the buffer to place a pointer to\r
383          * the network buffer structure that references this Ethernet buffer.\r
384          * Return a pointer to the start of the Ethernet buffer itself. */\r
385                 #ifndef PIC32_USE_ETHERNET\r
386                 pucEthernetBuffer += ipBUFFER_PADDING;\r
387                 #endif /* #ifndef PIC32_USE_ETHERNET */\r
388     }\r
389 \r
390     return pucEthernetBuffer;\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer )\r
395 {\r
396     /* There is space before the Ethernet buffer in which a pointer to the\r
397      * network buffer that references this Ethernet buffer is stored.  Remove the\r
398      * space before freeing the buffer. */\r
399     #ifdef PIC32_USE_ETHERNET\r
400         NetworkBufferFree( pucEthernetBuffer );\r
401     #else\r
402         if( pucEthernetBuffer != NULL )\r
403         {\r
404             pucEthernetBuffer -= ipBUFFER_PADDING;\r
405             vPortFree( ( void * ) pucEthernetBuffer );\r
406         }\r
407     #endif /* #ifdef PIC32_USE_ETHERNET */\r
408 }\r
409 /*-----------------------------------------------------------*/\r
410 \r
411 NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,\r
412                                                               TickType_t xBlockTimeTicks )\r
413 {\r
414     NetworkBufferDescriptor_t * pxReturn = NULL;\r
415     size_t uxCount;\r
416 \r
417     if( ( xRequestedSizeBytes != 0u ) && ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )\r
418     {\r
419         /* ARP packets can replace application packets, so the storage must be\r
420          * at least large enough to hold an ARP. */\r
421         xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;\r
422     }\r
423 \r
424         #ifdef PIC32_USE_ETHERNET\r
425         if( xRequestedSizeBytes != 0u )\r
426     {\r
427         #endif /* #ifdef PIC32_USE_ETHERNET */\r
428         xRequestedSizeBytes += 2u;\r
429 \r
430         if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1u ) ) != 0u )\r
431         {\r
432                 xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1u ) ) + 1u;\r
433         }\r
434         #ifdef PIC32_USE_ETHERNET\r
435     }\r
436         #endif /* #ifdef PIC32_USE_ETHERNET */\r
437 \r
438     /* If there is a semaphore available, there is a network buffer available. */\r
439     if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
440     {\r
441         /* Protect the structure as it is accessed from tasks and interrupts. */\r
442         taskENTER_CRITICAL();\r
443         {\r
444             pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
445             uxListRemove( &( pxReturn->xBufferListItem ) );\r
446         }\r
447         taskEXIT_CRITICAL();\r
448 \r
449         /* Reading UBaseType_t, no critical section needed. */\r
450         uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
451 \r
452         if( uxMinimumFreeNetworkBuffers > uxCount )\r
453         {\r
454             uxMinimumFreeNetworkBuffers = uxCount;\r
455         }\r
456 \r
457         /* Allocate storage of exactly the requested size to the buffer. */\r
458         configASSERT( pxReturn->pucEthernetBuffer == NULL );\r
459 \r
460         if( xRequestedSizeBytes > 0 )\r
461         {\r
462             /* Extra space is obtained so a pointer to the network buffer can\r
463              * be stored at the beginning of the buffer. */\r
464 \r
465             #ifdef PIC32_USE_ETHERNET\r
466                 pxReturn->pucEthernetBuffer = NetworkBufferAllocate( xRequestedSizeBytes - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );\r
467             #else\r
468                 pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );\r
469             #endif /* #ifdef PIC32_USE_ETHERNET */\r
470 \r
471             if( pxReturn->pucEthernetBuffer == NULL )\r
472             {\r
473                 /* The attempt to allocate storage for the buffer payload failed,\r
474                  * so the network buffer structure cannot be used and must be\r
475                  * released. */\r
476                 vReleaseNetworkBufferAndDescriptor( pxReturn );\r
477                 pxReturn = NULL;\r
478             }\r
479             else\r
480             {\r
481                 /* Store a pointer to the network buffer structure in the\r
482                  * buffer storage area, then move the buffer pointer on past the\r
483                  * stored pointer so the pointer value is not overwritten by the\r
484                  * application when the buffer is used. */\r
485                 #ifdef PIC32_USE_ETHERNET\r
486                     *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer - ipBUFFER_PADDING ) ) = pxReturn;\r
487                 #else\r
488                     *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;\r
489                     pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;\r
490                 #endif /* #ifdef PIC32_USE_ETHERNET */\r
491 \r
492                 /* Store the actual size of the allocated buffer, which may be\r
493                  * greater than the original requested size. */\r
494                 pxReturn->xDataLength = xRequestedSizeBytes;\r
495 \r
496                 #if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )\r
497                     {\r
498                         /* make sure the buffer is not linked */\r
499                         pxReturn->pxNextBuffer = NULL;\r
500                     }\r
501                 #endif /* ipconfigUSE_LINKED_RX_MESSAGES */\r
502             }\r
503         }\r
504         else\r
505         {\r
506             /* A descriptor is being returned without an associated buffer being\r
507              * allocated. */\r
508         }\r
509     }\r
510 \r
511     if( pxReturn == NULL )\r
512     {\r
513         iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
514     }\r
515     else\r
516     {\r
517         iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
518     }\r
519 \r
520     return pxReturn;\r
521 }\r
522 /*-----------------------------------------------------------*/\r
523 \r
524 void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )\r
525 {\r
526     BaseType_t xListItemAlreadyInFreeList;\r
527 \r
528     /* Ensure the buffer is returned to the list of free buffers before the\r
529     * counting semaphore is 'given' to say a buffer is available.  Release the\r
530     * storage allocated to the buffer payload.  THIS FILE SHOULD NOT BE USED\r
531     * IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP\r
532     * MEMORY.  For example, heap_2 must not be used, heap_4 can be used. */\r
533     vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );\r
534     pxNetworkBuffer->pucEthernetBuffer = NULL;\r
535 \r
536     taskENTER_CRITICAL();\r
537     {\r
538         xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
539 \r
540         if( xListItemAlreadyInFreeList == pdFALSE )\r
541         {\r
542             vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
543         }\r
544     }\r
545     taskEXIT_CRITICAL();\r
546 \r
547     /*\r
548      * Update the network state machine, unless the program fails to release its 'xNetworkBufferSemaphore'.\r
549      * The program should only try to release its semaphore if 'xListItemAlreadyInFreeList' is false.\r
550      */\r
551     if( xListItemAlreadyInFreeList == pdFALSE )\r
552     {\r
553         if ( xSemaphoreGive( xNetworkBufferSemaphore ) == pdTRUE )\r
554         {\r
555             iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
556         }\r
557     }\r
558     else\r
559     {\r
560         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
561     }\r
562 }\r
563 /*-----------------------------------------------------------*/\r
564 \r
565 /*\r
566  * Returns the number of free network buffers\r
567  */\r
568 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )\r
569 {\r
570     return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
571 }\r
572 /*-----------------------------------------------------------*/\r
573 \r
574 UBaseType_t uxGetMinimumFreeNetworkBuffers( void )\r
575 {\r
576     return uxMinimumFreeNetworkBuffers;\r
577 }\r
578 /*-----------------------------------------------------------*/\r
579 \r
580 NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,\r
581                                                                  size_t xNewSizeBytes )\r
582 {\r
583     size_t xOriginalLength;\r
584     uint8_t * pucBuffer;\r
585 \r
586     #ifdef PIC32_USE_ETHERNET\r
587         xOriginalLength = pxNetworkBuffer->xDataLength;\r
588     #else\r
589         xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;\r
590                 xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;\r
591     #endif /* #ifdef PIC32_USE_ETHERNET */\r
592         \r
593     pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );\r
594 \r
595     if( pucBuffer == NULL )\r
596     {\r
597         /* In case the allocation fails, return NULL. */\r
598         pxNetworkBuffer = NULL;\r
599     }\r
600     else\r
601     {\r
602         pxNetworkBuffer->xDataLength = xNewSizeBytes;\r
603         if( xNewSizeBytes > xOriginalLength )\r
604         {\r
605             xNewSizeBytes = xOriginalLength;\r
606         }\r
607 \r
608         #ifdef PIC32_USE_ETHERNET\r
609             memcpy( pucBuffer, pxNetworkBuffer->pucEthernetBuffer, xNewSizeBytes );\r
610             *( ( NetworkBufferDescriptor_t ** ) ( pucBuffer - ipBUFFER_PADDING ) ) = pxNetworkBuffer;\r
611         #else\r
612             memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );\r
613         #endif /* #ifdef PIC32_USE_ETHERNET */\r
614 \r
615         vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );\r
616         pxNetworkBuffer->pucEthernetBuffer = pucBuffer;\r
617     }\r
618 \r
619     return pxNetworkBuffer;\r
620 }\r