diff --git a/conev.c b/conev.c index b24346a..47f4d63 100644 --- a/conev.c +++ b/conev.c @@ -331,20 +331,22 @@ void loop_event(struct poolhd *pool) struct buffer *buff_pop(struct poolhd *pool, size_t size) { - struct buffer *root = pool->root_buff; - if (root) { - pool->root_buff = root->next; - return root; + struct buffer *buff = pool->root_buff; + if (buff) { + pool->root_buff = buff->next; + pool->buff_count--; } - struct buffer *buff = malloc(sizeof(struct buffer) + size); - if (!buff) { - uniperror("malloc"); - return 0; + else { + buff = malloc(sizeof(struct buffer) + size); + if (!buff) { + uniperror("malloc"); + return 0; + } + LOG(LOG_S, "alloc new buffer\n"); + + memset(buff, 0, sizeof(struct buffer)); + buff->size = size; } - LOG(LOG_S, "alloc new buffer\n"); - - memset(buff, 0, sizeof(struct buffer)); - buff->size = size; return buff; } @@ -354,10 +356,16 @@ void buff_push(struct poolhd *pool, struct buffer *buff) if (!buff) { return; } + if (pool->buff_count >= MAX_BUFF_INP) { + free(buff); + return; + } buff->lock = 0; buff->offset = 0; buff->next = pool->root_buff; + pool->root_buff = buff; + pool->buff_count++; } diff --git a/conev.h b/conev.h index 04d3220..81a3b1f 100644 --- a/conev.h +++ b/conev.h @@ -40,6 +40,8 @@ #endif #define POLLTIMEOUT 0 +#define MAX_BUFF_INP 8 + struct poolhd; struct eval; typedef int (*evcb_t)(struct poolhd *, struct eval *, int); @@ -101,6 +103,7 @@ struct poolhd { struct eval *tv_start, *tv_end; struct buffer *root_buff; + int buff_count; }; struct poolhd *init_pool(int count);