RetroArch
|
#include <string.h>
#include "lwip/opt.h"
#include "lwip/stats.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include "arch/perf.h"
Macros | |
#define | DEC_PBUF_STATS |
#define | PBUF_POOL_FAST_FREE(p) |
#define | PBUF_POOL_FREE(p) |
Functions | |
void | pbuf_init (void) |
static struct pbuf * | pbuf_pool_alloc (void) |
struct pbuf * | pbuf_alloc (pbuf_layer l, u16_t length, pbuf_flag flag) |
void | pbuf_realloc (struct pbuf *p, u16_t new_len) |
u8_t | pbuf_header (struct pbuf *p, s16_t header_size_increment) |
u8_t | pbuf_free (struct pbuf *p) |
u8_t | pbuf_clen (struct pbuf *p) |
void | pbuf_ref (struct pbuf *p) |
void | pbuf_cat (struct pbuf *h, struct pbuf *t) |
void | pbuf_chain (struct pbuf *h, struct pbuf *t) |
struct pbuf * | pbuf_take (struct pbuf *p) |
struct pbuf * | pbuf_dechain (struct pbuf *p) |
Variables | |
static u8_t | pbuf_pool_memory [MEM_ALIGNMENT - 1+PBUF_POOL_SIZE *MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE+sizeof(struct pbuf))] |
static volatile u8_t | pbuf_pool_free_lock |
static volatile u8_t | pbuf_pool_alloc_lock |
static sys_sem | pbuf_pool_free_sem |
static struct pbuf * | pbuf_pool = NULL |
Packet buffer management
Packets are built from the pbuf data structure. It supports dynamic memory allocation for packet contents or can reference externally managed packet contents both in RAM and ROM. Quick allocation for incoming packets is provided through pools with fixed sized pbufs.
A packet may span over multiple pbufs, chained as a singly linked list. This is called a "pbuf chain".
Multiple packets may be queued, also using this singly linked list. This is called a "packet queue".
So, a packet queue consists of one or more pbuf chains, each of which consist of one or more pbufs. Currently, queues are only supported in a limited section of lwIP, this is the etharp queueing code. Outside of this section no packet queues are supported yet.
The differences between a pbuf chain and a packet queue are very precise but subtle.
The last pbuf of a packet has a ->tot_len field that equals the ->len field. It can be found by traversing the list. If the last pbuf of a packet has a ->next field other than NULL, more packets are on the queue.
Therefore, looping through a pbuf of a single packet, has an loop end condition (tot_len == p->len), NOT (next == NULL).
#define DEC_PBUF_STATS |
#define PBUF_POOL_FAST_FREE | ( | p | ) |
#define PBUF_POOL_FREE | ( | p | ) |
struct pbuf* pbuf_alloc | ( | pbuf_layer | l, |
u16_t | length, | ||
pbuf_flag | flag | ||
) |
Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
The actual memory allocated for the pbuf is determined by the layer at which the pbuf is allocated and the requested size (from the size parameter).
flag | this parameter decides how and where the pbuf should be allocated as follows: |
Concatenate two pbufs (each may be a pbuf chain) and take over the caller's reference of the tail pbuf.
Chain two pbufs (or pbuf chains) together.
The caller MUST call pbuf_free(t) once it has stopped using it. Use pbuf_cat() instead if you no longer use t.
h | head pbuf (chain) |
t | tail pbuf (chain) |
The ->tot_len fields of all pbufs of the head chain are adjusted. The ->next field of the last pbuf of the head chain is adjusted. The ->ref field of the first pbuf of the tail chain is adjusted.
Count number of pbufs in a chain
p | first pbuf of chain |
Dechains the first pbuf from its succeeding pbufs in the chain.
Makes p->tot_len field equal to p->len.
p | pbuf to dechain |
Dereference a pbuf chain or queue and deallocate any no-longer-used pbufs at the head of this chain or queue.
Decrements the pbuf reference count. If it reaches zero, the pbuf is deallocated.
For a pbuf chain, this is repeated for each pbuf in the chain, up to the first pbuf which has a non-zero reference count after decrementing. So, when all reference counts are one, the whole chain is free'd.
pbuf | The pbuf (chain) to be dereferenced. |
Adjusts the payload pointer to hide or reveal headers in the payload.
Adjusts the ->payload pointer so that space for a header (dis)appears in the pbuf payload.
The ->payload, ->tot_len and ->len fields are adjusted.
hdr_size_inc | Number of bytes to increment header size which increases the size of the pbuf. New space is on the front. (Using a negative value decreases the header size.) If hdr_size_inc is 0, this function does nothing and returns succesful. |
PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so the call will fail. A check is made that the increase in header size does not move the payload pointer in front of the start of the buffer.
Initializes the pbuf module.
A large part of memory is allocated for holding the pool of pbufs. The size of the individual pbufs in the pool is given by the size parameter, and the number of pbufs in the pool by the num parameter.
After the memory has been allocated, the pbufs are set up. The ->next pointer in each pbuf is set up to point to the next pbuf in the pool.
Shrink a pbuf chain to a desired length.
p | pbuf to shrink. |
new_len | desired new length of pbuf chain |
Depending on the desired length, the first few pbufs in a chain might be skipped and left unchanged. The new last pbuf in the chain will be resized, and any remaining pbufs will be freed.
Increment the reference count of the pbuf.
p | pbuf to increase reference counter of |
Create PBUF_POOL (or PBUF_RAM) copies of PBUF_REF pbufs.
Used to queue packets on behalf of the lwIP stack, such as ARP based queueing.
Go through a pbuf chain and replace any PBUF_REF buffers with PBUF_POOL (or PBUF_RAM) pbufs, each taking a copy of the referenced data.
p | Head of pbuf chain to process |
|
static |
|
static |
|
static |
|
static |