From 3484e2abde58e7a70a570da64158a2c56729dfb2 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sun, 6 Jun 2010 15:14:05 +0200 Subject: [PATCH] Fix flow engine memory handling. --- src/flow-util.c | 15 +++++++++------ src/flow.c | 4 ++-- src/util-unittest-helper.c | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/flow-util.c b/src/flow-util.c index fd330916fc..d59f4425df 100644 --- a/src/flow-util.c +++ b/src/flow-util.c @@ -36,8 +36,6 @@ #include "detect.h" #include "detect-engine-state.h" -/* Counter of flows that reached memcap capability */ -uint32_t directflows = 0; /* Allocate a flow */ Flow *FlowAlloc(void) @@ -45,11 +43,18 @@ Flow *FlowAlloc(void) Flow *f; SCMutexLock(&flow_memuse_mutex); + if (flow_memuse + sizeof(Flow) > flow_config.memcap) { + SCMutexUnlock(&flow_memuse_mutex); + return NULL; + } + SCMutexUnlock(&flow_memuse_mutex); + f = SCMalloc(sizeof(Flow)); if (f == NULL) { - SCMutexUnlock(&flow_memuse_mutex); return NULL; } + + SCMutexLock(&flow_memuse_mutex); flow_memuse += sizeof(Flow); SCMutexUnlock(&flow_memuse_mutex); @@ -59,9 +64,6 @@ Flow *FlowAlloc(void) f->hnext = NULL; f->hprev = NULL; - f->flowvar = NULL; - f->de_state = NULL; - return f; } @@ -104,6 +106,7 @@ void FlowInit(Flow *f, Packet *p) SCEnter(); SCLogDebug("flow %p", f); + f->flowvar = NULL; f->de_state = NULL; CLEAR_FLOW(f); diff --git a/src/flow.c b/src/flow.c index 7091213f6f..e5d3f978f2 100644 --- a/src/flow.c +++ b/src/flow.c @@ -269,7 +269,7 @@ static uint32_t FlowPruneFlowQueue(FlowQueue *q, struct timeval *ts) { SCEnter(); uint32_t cnt = 0; - //while(FlowPrune(q, ts)) { cnt++; } + while(FlowPrune(q, ts)) { cnt++; } return cnt; } @@ -1312,9 +1312,9 @@ static int FlowClearMemory(Flow* f, uint8_t proto_map) { if (flow_proto[proto_map].Freefunc != NULL) { flow_proto[proto_map].Freefunc(f->protoctx); } - f->protoctx = NULL; CLEAR_FLOW(f); + SCReturnInt(1); } diff --git a/src/util-unittest-helper.c b/src/util-unittest-helper.c index aef38d0f23..32e2725c04 100644 --- a/src/util-unittest-helper.c +++ b/src/util-unittest-helper.c @@ -701,7 +701,8 @@ uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir) { p->dst.addr_data32[0] = i; } FlowHandlePacket(NULL, p); - p->flow->use_cnt = 0; + if (p->flow != NULL) + p->flow->use_cnt = 0; /* Now the queues shoul be updated */ UTHFreePacket(p);