26.12.2013 Views

lwIP - A Minimal TCP/IP implementation - Wikia

lwIP - A Minimal TCP/IP implementation - Wikia

lwIP - A Minimal TCP/IP implementation - Wikia

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15 API REFERENCE 15.2 Buffer functions<br />

buf = netbuf_new(); /* create a new netbuf */<br />

netbuf_alloc(buf, 100); /* allocate 100 bytes of buffer */<br />

/* do something with the netbuf */<br />

/* [...] */<br />

}<br />

netbuf_delete(buf); /* deallocate netbuf */<br />

15.2.3 netbuf alloc()<br />

Synopsis<br />

void * netbuf alloc(struct netbuf *buf, int size)<br />

Description<br />

Allocates buffer memory with size number of bytes for the netbuf buf. The function returns a<br />

pointer to the allocated memory. Any memory previously allocated to the netbuf buf is deallocated.<br />

The allocated memory can later be deallocated with the netbuf_free() function. Since<br />

protocol headers are expected to precede the data when it should be sent, the function allocates<br />

memory for protocol headers as well as for the actual data.<br />

15.2.4 netbuf free()<br />

Synopsis<br />

int netbuf free(struct netbuf *buf)<br />

Description<br />

Deallocates the buffer memory associated with the netbuf buf. If no buffer memory has been<br />

allocated for the netbuf, this function does nothing.<br />

15.2.5 netbuf ref()<br />

Synopsis<br />

int netbuf ref(struct netbuf *buf, void *data, int size)<br />

Description<br />

Associates the external memory pointer to by the data pointer with the netbuf buf. The size of the<br />

external memory is given by size. Any memory previously allocated to the netbuf is deallocated.<br />

The difference between allocating memory for the netbuf with netbuf_alloc() and allocating<br />

memory using, e.g., malloc() and referencing it with netbuf_ref() is that in the former case,<br />

space for protocol headers is allocated as well which makes processing and sending the buffer<br />

faster.<br />

Example<br />

This example shows a simple use of the netbuf_ref() function.<br />

int<br />

main()<br />

{<br />

struct netbuf *buf;<br />

char string[] = "A string";<br />

/* create a new netbuf */<br />

buf = netbuf_new();<br />

22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!