Skip to content

Commit

Permalink
BufferAllocation_2: respect 0 length buffer request (#265)
Browse files Browse the repository at this point in the history
* BufferAllocation_2: respect 0 length buffer request

The pxGetNetworkBufferWithDescriptor() documentation states:

	If xRequestedSizeBytes is zero then the returned network
	buffer descriptor will not reference an Ethernet buffer
	(the reference is set to NULL).

But this did not match the implementation. 2 bytes were
unconditionally being added to the requested size, and the
returned NetworkBufferDescriptor_t would have a nonzero
pucEthernetBuffer.

Move the code performing length alignment as is into the
branch checking whether size is nonzero. This lets the
caller request a bare NetworkBufferDescriptor_t to eg.
implement zero copy RX.

* BufferAllocation_2: initialize NetworkBuffer data length on free

Co-authored-by: Aniruddha Kanhere <[email protected]>
  • Loading branch information
twpedersen and AniruddhaKanhere authored Jun 10, 2021
1 parent fc36c06 commit c0c0a48
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions portable/BufferManagement/BufferAllocation_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,6 @@ NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedS

if( xNetworkBufferSemaphore != NULL )
{
if( ( xRequestedSizeBytes != 0U ) && ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
{
/* ARP packets can replace application packets, so the storage must be
* at least large enough to hold an ARP. */
xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;
}

/* Add 2 bytes to xRequestedSizeBytes and round up xRequestedSizeBytes
* to the nearest multiple of N bytes, where N equals 'sizeof( size_t )'. */
xRequestedSizeBytes += 2U;

if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1U ) ) != 0U )
{
xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1U ) ) + 1U;
}

/* If there is a semaphore available, there is a network buffer available. */
if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
{
Expand All @@ -268,6 +252,22 @@ NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedS

if( xRequestedSizeBytes > 0U )
{
if( ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
{
/* ARP packets can replace application packets, so the storage must be
* at least large enough to hold an ARP. */
xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;
}

/* Add 2 bytes to xRequestedSizeBytes and round up xRequestedSizeBytes
* to the nearest multiple of N bytes, where N equals 'sizeof( size_t )'. */
xRequestedSizeBytes += 2U;

if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1U ) ) != 0U )
{
xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1U ) ) + 1U;
}

/* Extra space is obtained so a pointer to the network buffer can
* be stored at the beginning of the buffer. */
pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
Expand Down Expand Up @@ -334,6 +334,7 @@ void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNet
* MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
pxNetworkBuffer->pucEthernetBuffer = NULL;
pxNetworkBuffer->xDataLength = 0U;

taskENTER_CRITICAL();
{
Expand Down

0 comments on commit c0c0a48

Please sign in to comment.