typedef struct {
  CTL_NET_MEM_FREE_FN_t free_fn;
  CTL_NET_MEM_ALLOC_FN_t alloc_xmit_fn;
  CTL_NET_MEM_ALLOC_FN_t alloc_data_fn;
  CTL_NET_MEM_TRIM_FN_t trim_fn;

In order to get the most flexibility out of a limited resource, the network library dynamically allocates RAM where and when it needs it. Systems that have dedicated Ethernet memory may use the network stack's built-in ‘net memory manager’ to manage the pool of Ethernet memory that is used for outgoing frames and TCP and UDP buffers.

Targets that don't have dedicated Ethernet memory may still benefit from using the net memory manager. Because the stack memory allocations are extremely transitory, more often than not there is no net memory allocated and the net memory heap is thus not fragmented. Using a private sub-heap is much more efficient than using the general heap in this particular case.

If you must squeeze every last bit of flexibility from dynamic RAM, then there is a stack version of the net memory manager that uses the general heap. You gain access to "all" of the heap, but you will be sharing it with the rest of the application and you will take a performance hit because of fragmentation issues.

The MAC layer is responsible for freeing net memory. After transmit, it should call ctl_net_mem_free on transmit frames (and their data) that it gets from the higher stack layers. The hdrData pointer of the CTL_ETH_TX_FRAME_t should always be ctl_net_mem_free'd, as well as the payload_free pointer (if it is non-null) and the CTL_ETH_TX_FRAME_t itself.

ctl_net_mem_alloc_xmit and ctl_net_mem_alloc_data both allocate memory for the network stack. The difference is that ctl_net_mem_alloc_xmit will attempt to take every last byte in the heap if that is what is required, while ctl_net_mem_alloc_data will attempt to leave a few bytes for future transmit allocations.

The reason for this duality is to prevent a potential fatal embrace whereby there is data available to be sent, but a transmit frame cannot be allocated to send it. Application code should always use ctl_net_mem_alloc_data when allocating memory from the network heap.

Method to free previously-allocated memory.
Method to allocate data for a transmit frame.
Method to allocate data for payload.
See Also