📄 tcpmem.c
字号:
#include "tcpmem.h"
#include "lwdef.h"
#include "tcp.h"
#include "pbuf.h"
struct memp
{
struct memp *next;
};
static struct memp *memp_tab[MEMP_MAX];
static const u16_t memp_sizes[MEMP_MAX] =
{
sizeof(struct pbuf),
sizeof(struct tcp_pcb),
sizeof(struct tcp_pcb_listen),
sizeof(struct tcp_seg),
};
static const u16_t memp_num[MEMP_MAX] =
{
MEMP_NUM_PBUF,
MEMP_NUM_TCP_PCB,
MEMP_NUM_TCP_PCB_LISTEN,
MEMP_NUM_TCP_SEG,
};
static u8_t memp_memory[
MEMP_NUM_PBUF *(sizeof(struct pbuf) +sizeof(struct memp))
+
MEMP_NUM_TCP_PCB *(sizeof(struct tcp_pcb) +sizeof(struct memp))
+
MEMP_NUM_TCP_PCB_LISTEN *(sizeof(struct tcp_pcb_listen) +sizeof(struct memp))
+
MEMP_NUM_TCP_SEG *(sizeof(struct tcp_seg) +sizeof(struct memp))
];
static int memp_sanity(void)
{
int i, c;
struct memp *m, *n;
for(i = 0; i < MEMP_MAX; i++)
{
for(m = memp_tab[i]; m != NULL; m = m->next)
{
c = 1;
for(n = memp_tab[i]; n != NULL; n = n->next)
{
if (n == m)
{
--c;
}
if (c < 0)
{
return 0; /* LW was: abort(); */
}
}
}
}
return 1;
}
void memp_init(void)
{
struct memp *m, *memp;
u16_t i, j;
u16_t size;
memp = (struct memp *)&memp_memory[0];
for(i = 0; i < MEMP_MAX; ++i)
{
size =memp_sizes[i] + sizeof(struct memp);
if (memp_num[i] > 0)
{
memp_tab[i] = memp;
m = memp;
for(j = 0; j < memp_num[i]; ++j)
{
m->next = (struct memp *)((u8_t *)m + size);
memp = m;
m = m->next;
}
memp->next = NULL;
memp = m;
}
else
{
memp_tab[i] = NULL;
}
}
}
void *memp_malloc(memp_t type)
{
struct memp *memp;
void *mem;
if(type < MEMP_MAX)
{
return NULL;
}
memp = memp_tab[type];
if (memp != NULL)
{
memp_tab[type] = memp->next;
memp->next = NULL;
mem = (u8_t *)memp + sizeof(struct memp);
return mem;
}
else
{
return NULL;
}
}
void memp_free(memp_t type, void *mem)
{
struct memp *memp;
if (mem == NULL)
{
return;
}
memp = (struct memp *)((u8_t *)mem - sizeof(struct memp));
memp->next = memp_tab[type];
memp_tab[type] = memp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -