69,366
社区成员
发帖
与我相关
我的任务
分享
static struct buf *mk_new_buf(const unsigned char *buf, const unsigned long len)
{
struct buf *pb = NULL;
if (NULL == buf)
return NULL;
pb = (struct buf *)malloc(sizeof(struct buf) + len);
if ( NULL == pb )
{
return NULL;
}
/*
结点在分配的时候就赋值了
而且,如果添加速度不快的话,程序运行一切正常。不管一次加多少结点都没问题。
我放慢速度跑了三个多小时,也没见程序崩掉。速度一快就不行了,有时候加到700,释放到200就崩了,
有时候加到5000,能全释放完。
*/
pb->next = NULL; // pb->next 在这儿赋值
pb->len = len;
memcpy(pb->data, pkt, len);
return pb;
}
int buf_list_add(struct buf *pb)
{
EnterCriticalSection(&pbl->cs);
if (pbl->buf_cnt > MAX_CNT)
{
LeaveCriticalSection(&pbl->cs);
return -1;
}
/* BUG:pbl->in野指针!
* 换成 if (NULL == pbl->out)
* 因为释放最后一个结点时,pbl->in 变为野指针!
*/
//if (NULL == pbl->in && NULL == pbl->out)
if (NULL == pbl->out)
{
pbl->in = pb;
pbl->out = pb;
}
else
{
pbl->in->next = pb;
pbl->in = pb;
}
pbl->buf_cnt++;
#ifdef _DEBUG
fprintf(stderr, "add_says: There are %ld nodes.\n", pbl->buf_cnt);
#endif
LeaveCriticalSection(&pbl->cs);
return 0;
}
int buf_list_fecth(void)
{
struct buf *tmp = NULL;
EnterCriticalSection(&pbl->cs);
/* 最初没有加此判断,导致把缓冲区释放完时,pbl->in变为野指针。此时再加入数据时,程序漰溃!*/
if (pbl->out == NULL)
{
pbl->in = NULL;
pbl->buf_cnt = 0;
LeaveCriticalSection(&pbl->cs);
return -1;
}
#ifdef _DEBUG
fprintf(stderr, "fecth_says: There are %ld nodes.\n", pbl->buf_cnt);
#endif
tmp = pbl->out;
pbl->out = pbl->out->next;
free(tmp);
pbl->buf_cnt--;
LeaveCriticalSection(&pbl->cs);
return 0;
}
static struct buf *mk_new_buf(const unsigned char *buf, const unsigned long len)
{
struct buf *pb = NULL;
if (NULL == buf)
return NULL;
pb = (struct buf *)malloc(sizeof(struct buf) + len);
if ( NULL == pb )
{
return NULL;
}
pb->next = NULL;
pb->len = len;
memcpy(pb->data, pkt, len);
return pb;
}