Newer
Older
pbuf / src / tlpbuf.c
@Razvan Turiac Razvan Turiac on 14 Oct 2022 1 KB ...
#include <tlpbuf.h>


void pbuf_pool_create(pbuf_pool_t* pool, uint32_t pbuf_count, void* pbuf_memory, uint32_t pbuf_payload_capacity, void* pbuf_payload_memory)
{
	pbuf_payload_capacity = (pbuf_payload_capacity + 3) & ~3;

	pool->pbuf_memory = (pbuf_t*)pbuf_memory;
	pool->pbuf_count = pbuf_count;

	pool->payload_capacity = pbuf_payload_capacity;
	pool->payload_memory = (uint8_t*)pbuf_payload_memory;
	
	pool->free_callback = NULL;

	pbuf_pool_reset(pool);
}



void pbuf_pool_reset(pbuf_pool_t* pool)
{
	//initialize pbuf structures
	uint8_t *payload = pool->payload_memory;

	pbuf_t *p = pool->free_list = pool->pbuf_memory;
	for(uint32_t i = 0; i < pool->pbuf_count - 1; i++)
	{
		p->payload_memory = payload;
		payload += pool->payload_capacity;
		
		p->pool = pool;
		p->next = p + 1;

		p++;
	}

	p->payload_memory = payload;
	p->pool = pool;
	p->next = 0;
	
	pool->pbufs_available = pool->pbuf_count;
}



#ifdef DEBUG

#include <debug.h>

void pbuf_pool_dump(pbuf_pool_t* pool)
{
	debug_printf("\r\nPool %x, %u pbufs available\r\n", pool, pool->pbufs_available);

	pbuf_t *p = pool->free_list;
	while(p)
	{
		debug_printf("\r\n%x", p);
		p = p->next;
	}

	debug_printf("\r\nEnd of dump\r\n");
}

#endif