From 97705c94e48016bf8ae0ce86024a75937a248aa2 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 22 May 2024 12:17:10 +0200 Subject: [PATCH] defrag: simplify lookup/create loops Turn into a simpler do { } while loop like in the flow code. --- src/defrag-hash.c | 129 ++++++++++++++-------------------------------- 1 file changed, 40 insertions(+), 89 deletions(-) diff --git a/src/defrag-hash.c b/src/defrag-hash.c index afd1aeeea0..5c1de9482d 100644 --- a/src/defrag-hash.c +++ b/src/defrag-hash.c @@ -576,65 +576,39 @@ DefragTracker *DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, P /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */ dt = hb->head; - /* see if this is the tracker we are looking for */ - if (dt->remove || DefragTrackerCompare(dt, p) == 0) { - DefragTracker *pdt = NULL; /* previous tracker */ - - while (dt) { - pdt = dt; - dt = dt->hnext; + do { + if (!dt->remove && DefragTrackerCompare(dt, p)) { + /* found our tracker, lock & return */ + SCMutexLock(&dt->lock); + (void)DefragTrackerIncrUsecnt(dt); + DRLOCK_UNLOCK(hb); + return dt; + } else if (dt->hnext == NULL) { + DefragTracker *prev = dt; + dt = DefragTrackerGetNew(tv, dtv, p); if (dt == NULL) { - dt = pdt->hnext = DefragTrackerGetNew(tv, dtv, p); - if (dt == NULL) { - DRLOCK_UNLOCK(hb); - return NULL; - } - hb->tail = dt; - - /* tracker is locked */ - - dt->hprev = pdt; - - /* initialize and return */ - DefragTrackerInit(dt,p); - DRLOCK_UNLOCK(hb); - return dt; + return NULL; } + prev->hnext = hb->tail = dt; + dt->hprev = prev; - if (DefragTrackerCompare(dt, p) != 0) { - /* we found our tracker, lets put it on top of the - * hash list -- this rewards active trackers */ - if (dt->hnext) { - dt->hnext->hprev = dt->hprev; - } - if (dt->hprev) { - dt->hprev->hnext = dt->hnext; - } - if (dt == hb->tail) { - hb->tail = dt->hprev; - } + /* tracker is locked */ - dt->hnext = hb->head; - dt->hprev = NULL; - hb->head->hprev = dt; - hb->head = dt; + /* initialize and return */ + DefragTrackerInit(dt, p); - /* found our tracker, lock & return */ - SCMutexLock(&dt->lock); - (void) DefragTrackerIncrUsecnt(dt); - DRLOCK_UNLOCK(hb); - return dt; - } + DRLOCK_UNLOCK(hb); + return dt; } - } - /* lock & return */ - SCMutexLock(&dt->lock); - (void) DefragTrackerIncrUsecnt(dt); - DRLOCK_UNLOCK(hb); - return dt; + dt = dt->hnext; + } while (dt != NULL); + + /* should be unreachable */ + BUG_ON(1); + return NULL; } /** \brief look up a tracker in the hash @@ -662,48 +636,25 @@ DefragTracker *DefragLookupTrackerFromHash (Packet *p) /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */ dt = hb->head; - /* see if this is the tracker we are looking for */ - if (DefragTrackerCompare(dt, p) == 0) { - while (dt) { - dt = dt->hnext; - - if (dt == NULL) { - DRLOCK_UNLOCK(hb); - return dt; - } - - if (DefragTrackerCompare(dt, p) != 0) { - /* we found our tracker, lets put it on top of the - * hash list -- this rewards active tracker */ - if (dt->hnext) { - dt->hnext->hprev = dt->hprev; - } - if (dt->hprev) { - dt->hprev->hnext = dt->hnext; - } - if (dt == hb->tail) { - hb->tail = dt->hprev; - } - - dt->hnext = hb->head; - dt->hprev = NULL; - hb->head->hprev = dt; - hb->head = dt; + do { + if (!dt->remove && DefragTrackerCompare(dt, p)) { + /* found our tracker, lock & return */ + SCMutexLock(&dt->lock); + (void)DefragTrackerIncrUsecnt(dt); + DRLOCK_UNLOCK(hb); + return dt; - /* found our tracker, lock & return */ - SCMutexLock(&dt->lock); - (void) DefragTrackerIncrUsecnt(dt); - DRLOCK_UNLOCK(hb); - return dt; - } + } else if (dt->hnext == NULL) { + DRLOCK_UNLOCK(hb); + return NULL; } - } - /* lock & return */ - SCMutexLock(&dt->lock); - (void) DefragTrackerIncrUsecnt(dt); - DRLOCK_UNLOCK(hb); - return dt; + dt = dt->hnext; + } while (dt != NULL); + + /* should be unreachable */ + BUG_ON(1); + return NULL; } /** \internal -- 2.47.2