]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
defrag: simplify lookup/create loops
authorVictor Julien <vjulien@oisf.net>
Wed, 22 May 2024 10:17:10 +0000 (12:17 +0200)
committerVictor Julien <victor@inliniac.net>
Tue, 4 Jun 2024 04:28:26 +0000 (06:28 +0200)
Turn into a simpler do { } while loop like in the flow code.

src/defrag-hash.c

index afd1aeeea03fec81e5dce6dfc20160330fabfa24..5c1de9482d8ab40237e2248373572809221ae30f 100644 (file)
@@ -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