defrag: simplify lookup/create loops

Turn into a simpler do { } while loop like in the flow code.
pull/11227/head
Victor Julien 2 years ago committed by Victor Julien
parent 76e05c72f6
commit 97705c94e4

@ -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 */ /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */
dt = hb->head; dt = hb->head;
/* see if this is the tracker we are looking for */ do {
if (dt->remove || DefragTrackerCompare(dt, p) == 0) { if (!dt->remove && DefragTrackerCompare(dt, p)) {
DefragTracker *pdt = NULL; /* previous tracker */ /* found our tracker, lock & return */
SCMutexLock(&dt->lock);
while (dt) { (void)DefragTrackerIncrUsecnt(dt);
pdt = dt; DRLOCK_UNLOCK(hb);
dt = dt->hnext; return dt;
} else if (dt->hnext == NULL) {
DefragTracker *prev = dt;
dt = DefragTrackerGetNew(tv, dtv, p);
if (dt == NULL) { 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); DRLOCK_UNLOCK(hb);
return dt; return NULL;
} }
prev->hnext = hb->tail = dt;
dt->hprev = prev;
if (DefragTrackerCompare(dt, p) != 0) { /* tracker is locked */
/* 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;
}
dt->hnext = hb->head; /* initialize and return */
dt->hprev = NULL; DefragTrackerInit(dt, p);
hb->head->hprev = dt;
hb->head = dt;
/* found our tracker, lock & return */ DRLOCK_UNLOCK(hb);
SCMutexLock(&dt->lock); return dt;
(void) DefragTrackerIncrUsecnt(dt);
DRLOCK_UNLOCK(hb);
return dt;
}
} }
}
/* lock & return */ dt = dt->hnext;
SCMutexLock(&dt->lock); } while (dt != NULL);
(void) DefragTrackerIncrUsecnt(dt);
DRLOCK_UNLOCK(hb); /* should be unreachable */
return dt; BUG_ON(1);
return NULL;
} }
/** \brief look up a tracker in the hash /** \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 */ /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */
dt = hb->head; dt = hb->head;
/* see if this is the tracker we are looking for */ do {
if (DefragTrackerCompare(dt, p) == 0) { if (!dt->remove && DefragTrackerCompare(dt, p)) {
while (dt) { /* found our tracker, lock & return */
dt = dt->hnext; SCMutexLock(&dt->lock);
(void)DefragTrackerIncrUsecnt(dt);
if (dt == NULL) { DRLOCK_UNLOCK(hb);
DRLOCK_UNLOCK(hb); return dt;
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;
/* found our tracker, lock & return */ } else if (dt->hnext == NULL) {
SCMutexLock(&dt->lock); DRLOCK_UNLOCK(hb);
(void) DefragTrackerIncrUsecnt(dt); return NULL;
DRLOCK_UNLOCK(hb);
return dt;
}
} }
}
/* lock & return */ dt = dt->hnext;
SCMutexLock(&dt->lock); } while (dt != NULL);
(void) DefragTrackerIncrUsecnt(dt);
DRLOCK_UNLOCK(hb); /* should be unreachable */
return dt; BUG_ON(1);
return NULL;
} }
/** \internal /** \internal

Loading…
Cancel
Save