Module: kamailio
Branch: master
Commit: 2e92a813cc0977e6f1e524bb8bafefb73efccba0
URL:
https://github.com/kamailio/kamailio/commit/2e92a813cc0977e6f1e524bb8bafefb…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2021-10-08T09:15:06+02:00
htable: use a function for finding the iterator
---
Modified: src/modules/htable/ht_api.c
---
Diff:
https://github.com/kamailio/kamailio/commit/2e92a813cc0977e6f1e524bb8bafefb…
Patch:
https://github.com/kamailio/kamailio/commit/2e92a813cc0977e6f1e524bb8bafefb…
---
diff --git a/src/modules/htable/ht_api.c b/src/modules/htable/ht_api.c
index 7bade571c3..7eb7bfa0d7 100644
--- a/src/modules/htable/ht_api.c
+++ b/src/modules/htable/ht_api.c
@@ -1700,19 +1700,16 @@ void ht_iterator_init(void)
memset(_ht_iterators, 0, HT_ITERATOR_SIZE*sizeof(ht_iterator_t));
}
-int ht_iterator_start(str *iname, str *hname)
+static inline int ht_iterator_find(str *iname)
{
int i;
int k;
k = -1;
- for(i=0; i<HT_ITERATOR_SIZE; i++)
- {
- if(_ht_iterators[i].name.len>0)
- {
+ for(i=0; i<HT_ITERATOR_SIZE; i++) {
+ if(_ht_iterators[i].name.len>0) {
if(_ht_iterators[i].name.len==iname->len
- && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
- {
+ && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0) {
k = i;
break;
}
@@ -1720,6 +1717,14 @@ int ht_iterator_start(str *iname, str *hname)
if(k==-1) k = i;
}
}
+ return k;
+}
+
+int ht_iterator_start(str *iname, str *hname)
+{
+ int k;
+
+ k = ht_iterator_find(iname);
if(k==-1)
{
LM_ERR("no iterator available - max number is %d\n", HT_ITERATOR_SIZE);
@@ -1759,24 +1764,9 @@ int ht_iterator_start(str *iname, str *hname)
int ht_iterator_next(str *iname)
{
- int i;
int k;
- k = -1;
- for(i=0; i<HT_ITERATOR_SIZE; i++)
- {
- if(_ht_iterators[i].name.len>0)
- {
- if(_ht_iterators[i].name.len==iname->len
- && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
- {
- k = i;
- break;
- }
- } else {
- if(k==-1) k = i;
- }
- }
+ k = ht_iterator_find(iname);
if(k==-1)
{
LM_ERR("iterator not found [%.*s]\n", iname->len, iname->s);
@@ -1819,47 +1809,38 @@ int ht_iterator_next(str *iname)
int ht_iterator_end(str *iname)
{
- int i;
+ int k;
- for(i=0; i<HT_ITERATOR_SIZE; i++)
+ k = ht_iterator_find(iname);
+ if(k==-1 || _ht_iterators[k].name.len<=0)
{
- if(_ht_iterators[i].name.len>0)
+ LM_ERR("iterator not found [%.*s]\n", iname->len, iname->s);
+ return -1;
+ }
+
+ if(_ht_iterators[k].ht!=NULL && _ht_iterators[k].it!=NULL)
+ {
+ if(_ht_iterators[k].slot>=0 &&
_ht_iterators[k].slot<_ht_iterators[k].ht->htsize)
{
- if(_ht_iterators[i].name.len==iname->len
- && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
- {
- if(_ht_iterators[i].ht!=NULL && _ht_iterators[i].it!=NULL)
- {
- if(_ht_iterators[i].slot>=0 &&
_ht_iterators[i].slot<_ht_iterators[i].ht->htsize)
- {
- ht_slot_unlock(_ht_iterators[i].ht, _ht_iterators[i].slot);
- }
- }
- memset(&_ht_iterators[i], 0, sizeof(ht_iterator_t));
- return 0;
- }
+ ht_slot_unlock(_ht_iterators[k].ht, _ht_iterators[k].slot);
}
}
-
- return -1;
+ memset(&_ht_iterators[k], 0, sizeof(ht_iterator_t));
+ return 0;
}
ht_cell_t* ht_iterator_get_current(str *iname)
{
- int i;
+ int k;
+
if(iname==NULL || iname->len<=0)
return NULL;
- for(i=0; i<HT_ITERATOR_SIZE; i++)
- {
- if(_ht_iterators[i].name.len>0)
- {
- if(_ht_iterators[i].name.len==iname->len
- && strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
- {
- return _ht_iterators[i].it;
- }
- }
+ k = ht_iterator_find(iname);
+ if(k==-1 || _ht_iterators[k].name.len<=0) {
+ LM_DBG("iterator not found [%.*s]\n", iname->len, iname->s);
+ return NULL;
}
- return NULL;
+
+ return _ht_iterators[k].it;
}