From 274d92478e2564e8fe468b09c92974269a1735f4 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 22 Oct 2008 11:10:36 +0200 Subject: [PATCH] Alloc a new packet if the queue is empty. --- src/packet-queue.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/packet-queue.c b/src/packet-queue.c index 83f4216727..da5ad3c9d8 100644 --- a/src/packet-queue.c +++ b/src/packet-queue.c @@ -1,5 +1,6 @@ /* Copyright (c) 2008 Victor Julien */ +#include "vips.h" #include "decode.h" #include "packet-queue.h" @@ -21,21 +22,36 @@ void PacketEnqueue (PacketQueue *q, Packet *p) { #endif /* DBG_PERF */ } +static u_int32_t dbg_packetdequeue = 0; + Packet *PacketDequeue (PacketQueue *q) { Packet *p = q->bot; + if (p == NULL) { + /* queue empty, alloc a new packet */ + Packet *p = malloc(sizeof(Packet)); + if (p == NULL) { + printf("ERROR: malloc failed: %s\n", strerror(errno)); + return NULL; + } - /* more packets in queue */ - if (q->bot->prev != NULL) { - q->bot = q->bot->prev; - q->bot->next = NULL; - /* just the one we remove, so now empty */ - } else { - q->top = NULL; - q->bot = NULL; - } + CLEAR_TCP_PACKET(p); + CLEAR_PACKET(p); - q->len--; + dbg_packetdequeue++; + printf("PacketDequeue: alloced a new packet. MAX_PENDING %u, extra alloc: %u\n", MAX_PENDING, dbg_packetdequeue); + } else { + /* more packets in queue */ + if (q->bot->prev != NULL) { + q->bot = q->bot->prev; + q->bot->next = NULL; + /* just the one we remove, so now empty */ + } else { + q->top = NULL; + q->bot = NULL; + } + q->len--; + } p->next = NULL; p->prev = NULL; return p;