Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev bufalloc #15

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,9 @@ option(FREERTOS_PLUS_TCP_ENABLE_BUILD_CHECKS "Enable the build checks for FreeRT

# Configuration
# Override these at project level with:
# Optional: set(FREERTOS_PLUS_TCP_BUFFER_ALLOCATION "1" CACHE STRING "" FORCE)
# Optional: set(FREERTOS_PLUS_TCP_COMPILER "" CACHE STRING "" FORCE)
# Required: set(FREERTOS_PLUS_TCP_NETWORK_IF "POSIX" CACHE STRING "" FORCE)

# Select the appropriate buffer allocation method.
# See: https://freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html
if (NOT FREERTOS_PLUS_TCP_BUFFER_ALLOCATION)
message(STATUS "Using default FREERTOS_PLUS_TCP_BUFFER_ALLOCATION = 2")
set(FREERTOS_PLUS_TCP_BUFFER_ALLOCATION "2" CACHE STRING "FreeRTOS buffer allocation model number. 1 .. 2.")
endif()

# Select the Compiler - if left blank will detect using CMake
# Note relies on CMake to detect over any setting here.
# Valid options are:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Below is the breakdown of what each directory contains:
- This directory contains all the [.c] source files.

## Note
At this time it is recommended to use BufferAllocation_2.c in which case it is essential to use the heap_4.c memory allocation scheme. See [memory management](http://www.FreeRTOS.org/a00111.html).
At this time it is recommended to leave ipconfigBUFFER_ALLOC_STATIC disabled in which case it is essential to use the heap_4.c memory allocation scheme. See [memory management](http://www.FreeRTOS.org/a00111.html).

### Kernel sources
The FreeRTOS Kernel Source is in [FreeRTOS/FreeRTOS-Kernel repository](https://github.com/FreeRTOS/FreeRTOS-Kernel), and it is consumed by testing/PR checks as a submodule in this repository.
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ https://www.freertos.org/Documentation/03-Libraries/02-FreeRTOS-plus/02-FreeRTOS
License information is available on:
http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/FreeRTOS_Plus_TCP_License.html

At this time it is recommended to use BufferAllocation_2.c in which case it is
At this time it is recommended to leave ipconfigBUFFER_ALLOC_STATIC disabled in which case it is
essential to use the heap_4.c memory allocation scheme:
http://www.FreeRTOS.org/a00111.html

14 changes: 8 additions & 6 deletions source/FreeRTOS_DNS_Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@
uint8_t * pucNewBuffer = NULL;
size_t uxExtraLength;

if( xBufferAllocFixedSize == pdFALSE )
#if ( ipconfigBUFFER_ALLOC_STATIC == 0 )
{
size_t uxDataLength = uxBufferLength +
sizeof( UDPHeader_t ) +
Expand Down Expand Up @@ -537,10 +537,11 @@
pxNetworkBuffer = NULL;
}
}
else
#else /* if ( ipconfigBUFFER_ALLOC_STATIC == 0 ) */
{
pucNewBuffer = &( pxNetworkBuffer->pucEthernetBuffer[ uxUDPOffset ] );
}
#endif /* if ( ipconfigBUFFER_ALLOC_STATIC == 0 ) */

if( ( pxNetworkBuffer != NULL ) )
{
Expand Down Expand Up @@ -1188,9 +1189,9 @@
* that were already present. */
uxSizeNeeded = pxNetworkBuffer->xDataLength + sizeof( NBNSAnswer_t ) - 2 * sizeof( uint16_t );

if( xBufferAllocFixedSize == pdFALSE )
#if ( ipconfigBUFFER_ALLOC_STATIC == 0 )
{
/* We're linked with BufferAllocation_2.c
/* Dynamic buffers are used,
* pxResizeNetworkBufferWithDescriptor() will malloc a new bigger buffer,
* and memcpy the data. The old buffer will be free'd.
*/
Expand All @@ -1206,12 +1207,13 @@
pxNetworkBuffer->xDataLength = uxSizeNeeded;
pucUDPPayloadBuffer = &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );
}
else
#else /* if ( ipconfigBUFFER_ALLOC_STATIC == 0 ) */
{
/* BufferAllocation_1.c is used, the Network Buffers can contain at least
/* Fixed buffers are used, the Network Buffers can contain at least
* ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER. */
configASSERT( uxSizeNeeded < ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER );
}
#endif /* if ( ipconfigBUFFER_ALLOC_STATIC == 0 ) */

pxNetworkBuffer->xDataLength = uxSizeNeeded;

Expand Down
6 changes: 3 additions & 3 deletions source/FreeRTOS_ND.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( BUFFER_FROM_WHERE_CALL( 181 ) uxPacketLength, uxBlockTimeTicks );
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( uxPacketLength, uxBlockTimeTicks );

if( pxNetworkBuffer != NULL )
{
Expand Down Expand Up @@ -952,7 +952,7 @@
if( xSendEventStructToIPTask( &xEventMessage, xDontBlock ) != pdPASS )
{
/* Failed to send the message, so release the network buffer. */
vReleaseNetworkBufferAndDescriptor( BUFFER_FROM_WHERE_CALL( 140 ) pxNDWaitingNetworkBuffer );
vReleaseNetworkBufferAndDescriptor( pxARPWaitingNetworkBuffer );
}

/* Clear the buffer. */
Expand Down Expand Up @@ -1384,7 +1384,7 @@
size_t uxNeededSize;

uxNeededSize = sizeof( ICMPPacket_IPv6_t );
pxTempBuffer = pxGetNetworkBufferWithDescriptor( BUFFER_FROM_WHERE_CALL( 199 ) uxNeededSize, 0U );
pxTempBuffer = pxGetNetworkBufferWithDescriptor( uxNeededSize, 0U );

if( pxTempBuffer != NULL )
{
Expand Down
5 changes: 3 additions & 2 deletions source/FreeRTOS_TCP_Transmission.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@
size_t uxNeeded;
BaseType_t xResize;

if( xBufferAllocFixedSize != pdFALSE )
#if ( ipconfigBUFFER_ALLOC_STATIC != 0 )
{
/* Network buffers are created with a fixed size and can hold the largest
* MTU. */
Expand All @@ -651,7 +651,7 @@
xResize = pdFALSE;
}
}
else
#else /* if ( ipconfigBUFFER_ALLOC_STATIC != 0 ) */
{
/* Network buffers are created with a variable size. See if it must
* grow. */
Expand All @@ -674,6 +674,7 @@
xResize = pdFALSE;
}
}
#endif /* if ( ipconfigBUFFER_ALLOC_STATIC != 0 ) */

if( xResize != pdFALSE )
{
Expand Down
80 changes: 59 additions & 21 deletions source/include/FreeRTOSIPConfigDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,65 @@ STATIC_ASSERT( ipconfigMAX_IP_TASK_SLEEP_TIME <= portMAX_DELAY );

/*---------------------------------------------------------------------------*/

/*
* ipconfigBUFFER_ALLOC_STATIC
*
* https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html
*
* Type: BaseType_t ( ipconfigENABLE | ipconfigDISABLE )
*
* Sets the buffer allocation method to use fixed static memory of size
* ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * ipTOTAL_ETHERNET_FRAME_SIZE.
* If disabled then a memory allocator that will not fragment the heap
* should be used.
*/

#ifndef ipconfigBUFFER_ALLOC_STATIC
#define ipconfigBUFFER_ALLOC_STATIC ipconfigDISABLE
#endif

#if ( ( ipconfigBUFFER_ALLOC_STATIC != ipconfigDISABLE ) && ( ipconfigBUFFER_ALLOC_STATIC != ipconfigENABLE ) )
#error Invalid ipconfigBUFFER_ALLOC_STATIC configuration
#endif

#ifndef ipconfigBUFFER_ALLOC_INIT
#define ipconfigBUFFER_ALLOC_INIT() do {} while( ipFALSE_BOOL )
#endif

#ifndef ipconfigBUFFER_ALLOC_LOCK
#define ipconfigBUFFER_ALLOC_LOCK() taskENTER_CRITICAL()
#endif

#ifndef ipconfigBUFFER_ALLOC_UNLOCK
#define ipconfigBUFFER_ALLOC_UNLOCK() taskEXIT_CRITICAL()
#endif

#if ipconfigIS_ENABLED( ipconfigBUFFER_ALLOC_STATIC )

#ifndef ipconfigBUFFER_ALLOC_LOCK_FROM_ISR
#define ipconfigBUFFER_ALLOC_LOCK_FROM_ISR() UBaseType_t uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); {
#endif

#ifndef ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR
#define ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR() portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); }
#endif

#ifndef ipconfigBUFFER_ALLOC_STATIC_CUSTOM_LOCATION
#define ipconfigBUFFER_ALLOC_STATIC_CUSTOM_LOCATION ipconfigDISABLE
#endif

#ifndef ipconfigBUFFER_ALLOC_STATIC_CUSTOM_LOCATION_STRING
#define ipconfigBUFFER_ALLOC_STATIC_CUSTOM_LOCATION_STRING ".EthBuffersSection"
#endif

#endif /* if ipconfigIS_ENABLED( ipconfigBUFFER_ALLOC_STATIC ) */

#ifndef ipconfigBUFFER_ALLOC_STATIC_CUSTOM_SIZE
#define ipconfigBUFFER_ALLOC_STATIC_CUSTOM_SIZE ipconfigDISABLE
#endif

/*---------------------------------------------------------------------------*/

/*
* ipconfigUSE_LINKED_RX_MESSAGES
*
Expand Down Expand Up @@ -3213,27 +3272,6 @@ STATIC_ASSERT( ipconfigDNS_SEND_BLOCK_TIME_TICKS <= portMAX_DELAY );

/*---------------------------------------------------------------------------*/

/*
* ipconfigTCP_IP_SANITY
*
* https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_IP_Configuration.html#ipconfigTCP_IP_SANITY
*
* Type: BaseType_t ( ipconfigENABLE | ipconfigDISABLE )
*
* Enables warnings when irregularities are detected when using
* BufferAllocation_1.c.
*/

#ifndef ipconfigTCP_IP_SANITY
#define ipconfigTCP_IP_SANITY ipconfigDISABLE
#endif

#if ( ( ipconfigTCP_IP_SANITY != ipconfigDISABLE ) && ( ipconfigTCP_IP_SANITY != ipconfigENABLE ) )
#error Invalid ipconfigTCP_IP_SANITY configuration
#endif

/*---------------------------------------------------------------------------*/

/*
* ipconfigTCP_MAY_LOG_PORT
*
Expand Down
3 changes: 0 additions & 3 deletions source/include/FreeRTOS_IP_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,6 @@ extern uint16_t usPacketIdentifier;
*/
extern List_t xBoundUDPSocketsList;

/* True when BufferAllocation_1.c was included, false for BufferAllocation_2.c */
extern const BaseType_t xBufferAllocFixedSize;

/* As FreeRTOS_Routing is included later, use forward declarations
* of the two structs. */
struct xNetworkEndPoint;
Expand Down
44 changes: 17 additions & 27 deletions source/include/NetworkBufferManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,29 @@

#include "FreeRTOS_IP.h"

/* _HT_ Two macro's needed while debugging/testing, please ignore. */

#define BUFFER_FROM_WHERE_DECL
#define BUFFER_FROM_WHERE_CALL( aWhere )

/* NOTE PUBLIC API FUNCTIONS. */

BaseType_t xNetworkBuffersInitialise( void );
NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
TickType_t xBlockTimeTicks );

/* The definition of the below function is only available if BufferAllocation_2.c has been linked into the source. */
NetworkBufferDescriptor_t * pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes );
void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer );

/* The definition of the below function is only available if BufferAllocation_2.c has been linked into the source. */
BaseType_t vNetworkBufferReleaseFromISR( NetworkBufferDescriptor_t * const pxNetworkBuffer );
uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes );
void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer );
#if ( ipconfigBUFFER_ALLOC_STATIC != 0 )

NetworkBufferDescriptor_t * pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes );

BaseType_t vNetworkBufferReleaseFromISR( NetworkBufferDescriptor_t * const pxNetworkBuffer );

void vNetworkBufferAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] );

extern const UBaseType_t uxBufferAllocFixedSize;

#else

/* Increase the size of a Network Buffer. */
NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
size_t xNewSizeBytes );
#endif /* ipconfigIS_ENABLED( ipconfigBUFFER_ALLOC_STATIC ) */

/* Get the current number of free network buffers. */
UBaseType_t uxGetNumberOfFreeNetworkBuffers( void );
Expand All @@ -65,21 +70,6 @@ UBaseType_t uxGetMinimumFreeNetworkBuffers( void );
NetworkBufferDescriptor_t * pxDuplicateNetworkBufferWithDescriptor( const NetworkBufferDescriptor_t * const pxNetworkBuffer,
size_t uxNewLength );

/* Increase the size of a Network Buffer.
* In case BufferAllocation_2.c is used, the new space must be allocated. */
NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
size_t xNewSizeBytes );

#if ipconfigTCP_IP_SANITY

/*
* Check if an address is a valid pointer to a network descriptor
* by looking it up in the array of network descriptors
*/
UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc );
BaseType_t prvIsFreeBuffer( const NetworkBufferDescriptor_t * pxDescr );
#endif

/* *INDENT-OFF* */
#ifdef __cplusplus
} /* extern "C" */
Expand Down
3 changes: 0 additions & 3 deletions source/include/NetworkInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
* BaseType_t xGetPhyLinkStatus( struct xNetworkInterface *pxInterface );
*/

/* The following function is defined only when BufferAllocation_1.c is linked in the project. */
void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] );

BaseType_t xGetPhyLinkStatus( struct xNetworkInterface * pxInterface );

#define MAC_IS_MULTICAST( pucMACAddressBytes ) ( ( pucMACAddressBytes[ 0 ] & 1U ) != 0U )
Expand Down
Loading
Loading