]> git.sur5r.net Git - freertos/blobdiff - 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
diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/pic32mzef/BufferAllocation_2.c b/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/pic32mzef/BufferAllocation_2.c
new file mode 100644 (file)
index 0000000..531e878
--- /dev/null
@@ -0,0 +1,620 @@
+/*\r
+ * FreeRTOS+TCP Labs Build 160919 (C) 2016 Real Time Engineers ltd.\r
+ * Authors include Hein Tibosch and Richard Barry\r
+ *\r
+ *******************************************************************************\r
+ ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***\r
+ ***                                                                         ***\r
+ ***                                                                         ***\r
+ ***   FREERTOS+TCP IS STILL IN THE LAB (mainly because the FTP and HTTP     ***\r
+ ***   demos have a dependency on FreeRTOS+FAT, which is only in the Labs    ***\r
+ ***   download):                                                            ***\r
+ ***                                                                         ***\r
+ ***   FreeRTOS+TCP is functional and has been used in commercial products   ***\r
+ ***   for some time.  Be aware however that we are still refining its       ***\r
+ ***   design, the source code does not yet quite conform to the strict      ***\r
+ ***   coding and style standards mandated by Real Time Engineers ltd., and  ***\r
+ ***   the documentation and testing is not necessarily complete.            ***\r
+ ***                                                                         ***\r
+ ***   PLEASE REPORT EXPERIENCES USING THE SUPPORT RESOURCES FOUND ON THE    ***\r
+ ***   URL: http://www.FreeRTOS.org/contact  Active early adopters may, at   ***\r
+ ***   the sole discretion of Real Time Engineers Ltd., be offered versions  ***\r
+ ***   under a license other than that described below.                      ***\r
+ ***                                                                         ***\r
+ ***                                                                         ***\r
+ ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***\r
+ *******************************************************************************\r
+ *\r
+ * FreeRTOS+TCP can be used under two different free open source licenses.  The\r
+ * license that applies is dependent on the processor on which FreeRTOS+TCP is\r
+ * executed, as follows:\r
+ *\r
+ * If FreeRTOS+TCP is executed on one of the processors listed under the Special\r
+ * License Arrangements heading of the FreeRTOS+TCP license information web\r
+ * page, then it can be used under the terms of the FreeRTOS Open Source\r
+ * License.  If FreeRTOS+TCP is used on any other processor, then it can be used\r
+ * under the terms of the GNU General Public License V2.  Links to the relevant\r
+ * licenses follow:\r
+ *\r
+ * The FreeRTOS+TCP License Information Page: http://www.FreeRTOS.org/tcp_license\r
+ * The FreeRTOS Open Source License: http://www.FreeRTOS.org/license\r
+ * The GNU General Public License Version 2: http://www.FreeRTOS.org/gpl-2.0.txt\r
+ *\r
+ * FreeRTOS+TCP is distributed in the hope that it will be useful.  You cannot\r
+ * use FreeRTOS+TCP unless you agree that you use the software 'as is'.\r
+ * FreeRTOS+TCP is provided WITHOUT ANY WARRANTY; without even the implied\r
+ * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
+ * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
+ * implied, expressed, or statutory.\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://www.FreeRTOS.org/plus\r
+ * http://www.FreeRTOS.org/labs\r
+ *\r
+ */\r
+\r
+/******************************************************************************\r
+*\r
+* See the following web page for essential buffer allocation scheme usage and\r
+* configuration details:\r
+* http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html\r
+*\r
+******************************************************************************/\r
+\r
+/* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR\r
+ * THAT WILL FRAGMENT THE HEAP MEMORY.  For example, heap_2 must not be used,\r
+ * heap_4 can be used. */\r
+\r
+/* Standard includes. */\r
+#include <stdint.h>\r
+\r
+/* FreeRTOS includes. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+#include "semphr.h"\r
+\r
+/* FreeRTOS+TCP includes. */\r
+#include "FreeRTOS_IP.h"\r
+#include "FreeRTOS_UDP_IP.h"\r
+#include "FreeRTOS_IP_Private.h"\r
+#include "NetworkInterface.h"\r
+#include "NetworkBufferManagement.h"\r
+\r
+#include "tcpip/tcpip.h"\r
+#include "tcpip/src/tcpip_private.h"\r
+\r
+#include "NetworkConfig.h"\r
+\r
+/* The obtained network buffer must be large enough to hold a packet that might\r
+ * replace the packet that was requested to be sent. */\r
+#if ipconfigUSE_TCP == 1\r
+    #define baMINIMAL_BUFFER_SIZE    sizeof( TCPPacket_t )\r
+#else\r
+    #define baMINIMAL_BUFFER_SIZE    sizeof( ARPPacket_t )\r
+#endif /* ipconfigUSE_TCP == 1 */\r
+\r
+/*_RB_ This is too complex not to have an explanation. */\r
+#if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )\r
+    #define ASSERT_CONCAT_( a, b )    a ## b\r
+    #define ASSERT_CONCAT( a, b )     ASSERT_CONCAT_( a, b )\r
+    #define STATIC_ASSERT( e ) \\r
+    ; enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = 1 / ( !!( e ) ) }\r
+\r
+    STATIC_ASSERT( ipconfigETHERNET_MINIMUM_PACKET_BYTES <= baMINIMAL_BUFFER_SIZE );\r
+#endif\r
+\r
+/* A list of free (available) NetworkBufferDescriptor_t structures. */\r
+static List_t xFreeBuffersList;\r
+\r
+/* Some statistics about the use of buffers. */\r
+static size_t uxMinimumFreeNetworkBuffers;\r
+\r
+/* Declares the pool of NetworkBufferDescriptor_t structures that are available\r
+ * to the system.  All the network buffers referenced from xFreeBuffersList exist\r
+ * in this array.  The array is not accessed directly except during initialisation,\r
+ * when the xFreeBuffersList is filled (as all the buffers are free when the system\r
+ * is booted). */\r
+static NetworkBufferDescriptor_t xNetworkBufferDescriptors[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];\r
+\r
+/* This constant is defined as false to let FreeRTOS_TCP_IP.c know that the\r
+ * network buffers have a variable size: resizing may be necessary */\r
+const BaseType_t xBufferAllocFixedSize = pdFALSE;\r
+\r
+/* The semaphore used to obtain network buffers. */\r
+static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+#ifdef PIC32_USE_ETHERNET\r
+\r
+    /* PIC32 specific stuff */\r
+    /* */\r
+\r
+    /* MAC packet acknowledgment, once MAC is done with it */\r
+        static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,\r
+                                                const void * param );\r
+\r
+    /* allocates a MAC packet that holds a data buffer that can be used by both: */\r
+    /*  - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */\r
+    /*  - the Harmony MAC driver: TCPIP_MAC_PACKET->pDSeg->segLoad */\r
+    /* from the beginning of the buffer: */\r
+    /*      - 4 bytes pointer to the network descriptor (FreeRTOS) */\r
+    /*      - 4 bytes pointer to the MAC packet (pic32_NetworkInterface.c) */\r
+    /*      - 2 bytes offset from the MAC packet (Harmony MAC driver: segLoadOffset) */\r
+    /* */\r
+    /* NOTE: segLoadLen should NOT include: */\r
+    /*          - the TCPIP_MAC_FRAME_OFFSET (== ipBUFFER_PADDING which should be == 10!) */\r
+    /*          - the sizeof(TCPIP_MAC_ETHERNET_HEADER) */\r
+    /*       These are added by the MAC packet allocation! */\r
+    /* */\r
+    static uint8_t * PIC32_PktAlloc( uint16_t pktLen,\r
+                                     uint16_t segLoadLen,\r
+                                     TCPIP_MAC_PACKET_ACK_FUNC ackF,\r
+                                     TCPIP_MAC_PACKET ** pPtrPkt )\r
+    {\r
+        uint8_t * pBuff = 0;\r
+\r
+        /* allocate standard packet */\r
+        TCPIP_MAC_PACKET * pPkt = TCPIP_PKT_PacketAlloc( pktLen, segLoadLen, 0 );\r
+\r
+        /* set the MAC packet pointer in the packet */\r
+        if( pPkt != 0 )\r
+        {\r
+            pBuff = pPkt->pDSeg->segLoad;\r
+            TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pBuff - PIC32_BUFFER_PKT_PTR_OSSET );\r
+            configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
+            *ppkt = pPkt; /* store the packet it comes from */\r
+            pPkt->ackFunc = ackF;\r
+            pPkt->ackParam = 0;\r
+        }\r
+\r
+        if( pPtrPkt != 0 )\r
+        {\r
+            *pPtrPkt = pPkt;\r
+        }\r
+\r
+        return pBuff;\r
+    }\r
+\r
+\r
+\r
+    /* standard PIC32 MAC allocation function for a MAC packet */\r
+    /* this packet saves room for the FreeRTOS network descriptor */\r
+    /* at the beginning of the data buffer */\r
+    /* see NetworkBufferAllocate */\r
+    /* Note: flags parameter is ignored since that's used in the Harmony stack only */\r
+    TCPIP_MAC_PACKET * PIC32_MacPacketAllocate( uint16_t pktLen,\r
+                                                uint16_t segLoadLen,\r
+                                                TCPIP_MAC_PACKET_FLAGS flags )\r
+    {\r
+        TCPIP_MAC_PACKET * pPkt;\r
+\r
+        PIC32_PktAlloc( pktLen, segLoadLen, 0, &pPkt );\r
+\r
+        return pPkt;\r
+    }\r
+\r
+    /* standard PIC32 MAC packet acknowledgment */\r
+    /* function called once MAC is done with it */\r
+    static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,\r
+                                            const void * param )\r
+    {\r
+        configASSERT( ( pPkt != 0 ) );\r
+\r
+        TCPIP_PKT_PacketFree( pPkt );\r
+\r
+        return false;\r
+    }\r
+\r
+    /* associates the current MAC packet with a network descriptor */\r
+    /* mainly for RX packet */\r
+    void PIC32_MacAssociate( TCPIP_MAC_PACKET * pRxPkt,\r
+                             NetworkBufferDescriptor_t * pxBufferDescriptor,\r
+                             size_t pktLength )\r
+    {\r
+        uint8_t * pPktBuff = pRxPkt->pDSeg->segLoad;\r
+\r
+        pxBufferDescriptor->pucEthernetBuffer = pPktBuff;\r
+        pxBufferDescriptor->xDataLength = pktLength;\r
+\r
+        /* make sure this is a properly allocated packet */\r
+        TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pPktBuff - PIC32_BUFFER_PKT_PTR_OSSET );\r
+        configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
+\r
+        if( *ppkt != pRxPkt )\r
+        {\r
+            configASSERT( false );\r
+        }\r
+\r
+        /* set the proper descriptor info */\r
+        NetworkBufferDescriptor_t ** ppDcpt = ( NetworkBufferDescriptor_t ** ) ( pPktBuff - ipBUFFER_PADDING );\r
+        configASSERT( ( ( uint32_t ) ppDcpt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
+        *ppDcpt = pxBufferDescriptor;\r
+    }\r
+\r
+    /* debug functionality */\r
+    void PIC32_MacPacketOrphan( TCPIP_MAC_PACKET * pPkt )\r
+    {\r
+        TCPIP_PKT_PacketFree( pPkt );\r
+        configASSERT( false );\r
+    }\r
+\r
+    /* FreeRTOS allocation functions */\r
+\r
+    /* allocates a buffer that can be used by both: */\r
+    /*  - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */\r
+    /*  - the Harmony MAC driver: TCPIP_MAC_PACKET */\r
+    /*  See PIC32_PktAlloc for details */\r
+    /* */\r
+    /* NOTE: reqLength should NOT include the ipBUFFER_PADDING (which should be == 10!) */\r
+    /*       or the sizeof(TCPIP_MAC_ETHERNET_HEADER) */\r
+    /*       These are added by the MAC packet allocation! */\r
+    /* */\r
+    uint8_t * NetworkBufferAllocate( size_t reqLength )\r
+    {\r
+        return PIC32_PktAlloc( sizeof( TCPIP_MAC_PACKET ), reqLength, PIC32_MacPacketAcknowledge, 0 );\r
+    }\r
+\r
+    /* deallocates a network buffer previously allocated */\r
+    /* with NetworkBufferAllocate */\r
+    void NetworkBufferFree( uint8_t * pNetworkBuffer )\r
+    {\r
+        if( pNetworkBuffer != 0 )\r
+        {\r
+            TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pNetworkBuffer - PIC32_BUFFER_PKT_PTR_OSSET );\r
+            configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );\r
+            TCPIP_MAC_PACKET * pPkt = *ppkt;\r
+            configASSERT( ( pPkt != 0 ) );\r
+\r
+            if( pPkt->ackFunc != 0 )\r
+            {\r
+                ( *pPkt->ackFunc )( pPkt, pPkt->ackParam );\r
+            }\r
+            else\r
+            { /* ??? */\r
+                PIC32_MacPacketOrphan( pPkt );\r
+            }\r
+        }\r
+    }\r
+\r
+#endif /* #ifdef PIC32_USE_ETHERNET */\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+BaseType_t xNetworkBuffersInitialise( void )\r
+{\r
+    BaseType_t xReturn, x;\r
+\r
+    /* Only initialise the buffers and their associated kernel objects if they\r
+     * have not been initialised before. */\r
+    if( xNetworkBufferSemaphore == NULL )\r
+    {\r
+        xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );\r
+        configASSERT( xNetworkBufferSemaphore );\r
+\r
+        if( xNetworkBufferSemaphore != NULL )\r
+        {\r
+            #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
+                {\r
+                    vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );\r
+                }\r
+            #endif /* configQUEUE_REGISTRY_SIZE */\r
+\r
+            /* If the trace recorder code is included name the semaphore for viewing\r
+             * in FreeRTOS+Trace.  */\r
+            #if ( ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 )\r
+                {\r
+                    extern QueueHandle_t xNetworkEventQueue;\r
+                    vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
+                    vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );\r
+                }\r
+            #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
+\r
+            vListInitialise( &xFreeBuffersList );\r
+\r
+            /* Initialise all the network buffers.  No storage is allocated to\r
+             * the buffers yet. */\r
+            for( x = 0; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )\r
+            {\r
+                /* Initialise and set the owner of the buffer list items. */\r
+                xNetworkBufferDescriptors[ x ].pucEthernetBuffer = NULL;\r
+                vListInitialiseItem( &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );\r
+                listSET_LIST_ITEM_OWNER( &( xNetworkBufferDescriptors[ x ].xBufferListItem ), &xNetworkBufferDescriptors[ x ] );\r
+\r
+                /* Currently, all buffers are available for use. */\r
+                vListInsert( &xFreeBuffersList, &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );\r
+            }\r
+\r
+            uxMinimumFreeNetworkBuffers = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;\r
+        }\r
+    }\r
+\r
+    if( xNetworkBufferSemaphore == NULL )\r
+    {\r
+        xReturn = pdFAIL;\r
+    }\r
+    else\r
+    {\r
+        xReturn = pdPASS;\r
+    }\r
+\r
+    return xReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes )\r
+{\r
+    uint8_t * pucEthernetBuffer;\r
+    size_t xSize = *pxRequestedSizeBytes;\r
+\r
+    if( xSize < baMINIMAL_BUFFER_SIZE )\r
+    {\r
+        /* Buffers must be at least large enough to hold a TCP-packet with\r
+         * headers, or an ARP packet, in case TCP is not included. */\r
+        xSize = baMINIMAL_BUFFER_SIZE;\r
+    }\r
+\r
+    /* Round up xSize to the nearest multiple of N bytes,\r
+     * where N equals 'sizeof( size_t )'. */\r
+    if( ( xSize & ( sizeof( size_t ) - 1u ) ) != 0u )\r
+    {\r
+        xSize = ( xSize | ( sizeof( size_t ) - 1u ) ) + 1u;\r
+    }\r
+\r
+    *pxRequestedSizeBytes = xSize;\r
+\r
+    /* Allocate a buffer large enough to store the requested Ethernet frame size\r
+     * and a pointer to a network buffer structure (hence the addition of\r
+     * ipBUFFER_PADDING bytes). */\r
+\r
+    #ifdef PIC32_USE_ETHERNET\r
+        pucEthernetBuffer = NetworkBufferAllocate( xSize - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );\r
+    #else\r
+        pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xSize + ipBUFFER_PADDING );\r
+    #endif /* #ifdef PIC32_USE_ETHERNET */\r
+\r
+    configASSERT( pucEthernetBuffer );\r
+\r
+    if( pucEthernetBuffer != NULL )\r
+    {\r
+        /* Enough space is left at the start of the buffer to place a pointer to\r
+         * the network buffer structure that references this Ethernet buffer.\r
+         * Return a pointer to the start of the Ethernet buffer itself. */\r
+               #ifndef PIC32_USE_ETHERNET\r
+               pucEthernetBuffer += ipBUFFER_PADDING;\r
+               #endif /* #ifndef PIC32_USE_ETHERNET */\r
+    }\r
+\r
+    return pucEthernetBuffer;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer )\r
+{\r
+    /* There is space before the Ethernet buffer in which a pointer to the\r
+     * network buffer that references this Ethernet buffer is stored.  Remove the\r
+     * space before freeing the buffer. */\r
+    #ifdef PIC32_USE_ETHERNET\r
+        NetworkBufferFree( pucEthernetBuffer );\r
+    #else\r
+        if( pucEthernetBuffer != NULL )\r
+        {\r
+            pucEthernetBuffer -= ipBUFFER_PADDING;\r
+            vPortFree( ( void * ) pucEthernetBuffer );\r
+        }\r
+    #endif /* #ifdef PIC32_USE_ETHERNET */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,\r
+                                                              TickType_t xBlockTimeTicks )\r
+{\r
+    NetworkBufferDescriptor_t * pxReturn = NULL;\r
+    size_t uxCount;\r
+\r
+    if( ( xRequestedSizeBytes != 0u ) && ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )\r
+    {\r
+        /* ARP packets can replace application packets, so the storage must be\r
+         * at least large enough to hold an ARP. */\r
+        xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;\r
+    }\r
+\r
+       #ifdef PIC32_USE_ETHERNET\r
+       if( xRequestedSizeBytes != 0u )\r
+    {\r
+       #endif /* #ifdef PIC32_USE_ETHERNET */\r
+       xRequestedSizeBytes += 2u;\r
+\r
+       if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1u ) ) != 0u )\r
+       {\r
+               xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1u ) ) + 1u;\r
+       }\r
+       #ifdef PIC32_USE_ETHERNET\r
+    }\r
+       #endif /* #ifdef PIC32_USE_ETHERNET */\r
+\r
+    /* If there is a semaphore available, there is a network buffer available. */\r
+    if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
+    {\r
+        /* Protect the structure as it is accessed from tasks and interrupts. */\r
+        taskENTER_CRITICAL();\r
+        {\r
+            pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
+            uxListRemove( &( pxReturn->xBufferListItem ) );\r
+        }\r
+        taskEXIT_CRITICAL();\r
+\r
+        /* Reading UBaseType_t, no critical section needed. */\r
+        uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
+\r
+        if( uxMinimumFreeNetworkBuffers > uxCount )\r
+        {\r
+            uxMinimumFreeNetworkBuffers = uxCount;\r
+        }\r
+\r
+        /* Allocate storage of exactly the requested size to the buffer. */\r
+        configASSERT( pxReturn->pucEthernetBuffer == NULL );\r
+\r
+        if( xRequestedSizeBytes > 0 )\r
+        {\r
+            /* Extra space is obtained so a pointer to the network buffer can\r
+             * be stored at the beginning of the buffer. */\r
+\r
+            #ifdef PIC32_USE_ETHERNET\r
+                pxReturn->pucEthernetBuffer = NetworkBufferAllocate( xRequestedSizeBytes - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );\r
+            #else\r
+                pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );\r
+            #endif /* #ifdef PIC32_USE_ETHERNET */\r
+\r
+            if( pxReturn->pucEthernetBuffer == NULL )\r
+            {\r
+                /* The attempt to allocate storage for the buffer payload failed,\r
+                 * so the network buffer structure cannot be used and must be\r
+                 * released. */\r
+                vReleaseNetworkBufferAndDescriptor( pxReturn );\r
+                pxReturn = NULL;\r
+            }\r
+            else\r
+            {\r
+                /* Store a pointer to the network buffer structure in the\r
+                 * buffer storage area, then move the buffer pointer on past the\r
+                 * stored pointer so the pointer value is not overwritten by the\r
+                 * application when the buffer is used. */\r
+                #ifdef PIC32_USE_ETHERNET\r
+                    *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer - ipBUFFER_PADDING ) ) = pxReturn;\r
+                #else\r
+                    *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;\r
+                    pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;\r
+                #endif /* #ifdef PIC32_USE_ETHERNET */\r
+\r
+                /* Store the actual size of the allocated buffer, which may be\r
+                 * greater than the original requested size. */\r
+                pxReturn->xDataLength = xRequestedSizeBytes;\r
+\r
+                #if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )\r
+                    {\r
+                        /* make sure the buffer is not linked */\r
+                        pxReturn->pxNextBuffer = NULL;\r
+                    }\r
+                #endif /* ipconfigUSE_LINKED_RX_MESSAGES */\r
+            }\r
+        }\r
+        else\r
+        {\r
+            /* A descriptor is being returned without an associated buffer being\r
+             * allocated. */\r
+        }\r
+    }\r
+\r
+    if( pxReturn == NULL )\r
+    {\r
+        iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
+    }\r
+    else\r
+    {\r
+        iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
+    }\r
+\r
+    return pxReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )\r
+{\r
+    BaseType_t xListItemAlreadyInFreeList;\r
+\r
+    /* Ensure the buffer is returned to the list of free buffers before the\r
+    * counting semaphore is 'given' to say a buffer is available.  Release the\r
+    * storage allocated to the buffer payload.  THIS FILE SHOULD NOT BE USED\r
+    * IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP\r
+    * MEMORY.  For example, heap_2 must not be used, heap_4 can be used. */\r
+    vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );\r
+    pxNetworkBuffer->pucEthernetBuffer = NULL;\r
+\r
+    taskENTER_CRITICAL();\r
+    {\r
+        xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
+\r
+        if( xListItemAlreadyInFreeList == pdFALSE )\r
+        {\r
+            vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
+        }\r
+    }\r
+    taskEXIT_CRITICAL();\r
+\r
+    /*\r
+     * Update the network state machine, unless the program fails to release its 'xNetworkBufferSemaphore'.\r
+     * The program should only try to release its semaphore if 'xListItemAlreadyInFreeList' is false.\r
+     */\r
+    if( xListItemAlreadyInFreeList == pdFALSE )\r
+    {\r
+        if ( xSemaphoreGive( xNetworkBufferSemaphore ) == pdTRUE )\r
+        {\r
+            iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
+        }\r
+    }\r
+    else\r
+    {\r
+        iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
+    }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * Returns the number of free network buffers\r
+ */\r
+UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )\r
+{\r
+    return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+UBaseType_t uxGetMinimumFreeNetworkBuffers( void )\r
+{\r
+    return uxMinimumFreeNetworkBuffers;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,\r
+                                                                 size_t xNewSizeBytes )\r
+{\r
+    size_t xOriginalLength;\r
+    uint8_t * pucBuffer;\r
+\r
+    #ifdef PIC32_USE_ETHERNET\r
+        xOriginalLength = pxNetworkBuffer->xDataLength;\r
+    #else\r
+        xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;\r
+               xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;\r
+    #endif /* #ifdef PIC32_USE_ETHERNET */\r
+       \r
+    pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );\r
+\r
+    if( pucBuffer == NULL )\r
+    {\r
+        /* In case the allocation fails, return NULL. */\r
+        pxNetworkBuffer = NULL;\r
+    }\r
+    else\r
+    {\r
+        pxNetworkBuffer->xDataLength = xNewSizeBytes;\r
+        if( xNewSizeBytes > xOriginalLength )\r
+        {\r
+            xNewSizeBytes = xOriginalLength;\r
+        }\r
+\r
+        #ifdef PIC32_USE_ETHERNET\r
+            memcpy( pucBuffer, pxNetworkBuffer->pucEthernetBuffer, xNewSizeBytes );\r
+            *( ( NetworkBufferDescriptor_t ** ) ( pucBuffer - ipBUFFER_PADDING ) ) = pxNetworkBuffer;\r
+        #else\r
+            memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );\r
+        #endif /* #ifdef PIC32_USE_ETHERNET */\r
+\r
+        vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );\r
+        pxNetworkBuffer->pucEthernetBuffer = pucBuffer;\r
+    }\r
+\r
+    return pxNetworkBuffer;\r
+}\r