lwIP Wiki
Advertisement

netbuf_alloc[]

void * netbuf_alloc ( struct netbuf * aNetBuf, u16_t aSize );

Allocates memory of size "aSize" into the already existing netbuf object.

  • in aNetBuf : nebuf object obtained with e.g. netbuf_new()
  • in aSize  : the length of the memory to allocate
  • return  : NULL on failure, or pointer to the allocated memory

In case the netbuf object already references memory it is deallocated prior to the new allocation.


netbuf_ref[]

err_t netbuf_ref ( struct netbuf * aNetBuf, const void * aData, u16_t aSize );

Alternately to using netbuf_alloc you can create an external buffer and then reference it into the netbuf object. This is useful for example if you get data by a parameter of a function and you want to output the data to an UDP connection.

  • in aNetBuf : nebuf object obtained with e.g. netbuf_new()
  • in aData : pointer to the data you want to associate with the object
  • in aSize : the length of the data
  • return : ERR_MEM if there's no memory for the internal buffer management, ERR_OK else


Note that there is a subtle difference between the two function in respect to memory deallocation. If you use "netbuf_alloc()" and then "netbuf_delete()", all of the memory is freed, but if you use "netbuf_ref()" instead, the memory used by the referenced parameter is not freed by "netbuf_delete()". This makes sense though, because the reference could also be static memory.

Example:

struct netbuf * xNetBuf = netbuf_new ();
Pointer xBuf = mem_malloc ( 0x100 );
memset ( xBuf, 0xAA, 0x100 );

err_t xErr = netbuf_ref ( xNetBuf, xBuf, 0x100 );
if ( xErr != ERR_OK )
   netbuf_free ( xNetBuf );            // does NOT free xBuf

memset ( xBuf, 0x55 );
...
...

mem_free ( xBuf );


xBuf = netbuf_alloc ( xNetBuf, 0x100 );
if ( xBuf != NULL )
{

   memset ( xBuf, 0xAA );
   netbuf_delete ( xNetBuf );           // frees all memory
}





(last changed: Sept. 26, 2011)

Advertisement