Browse code

htable: use a function for finding the iterator

Daniel-Constantin Mierla authored on 08/10/2021 07:15:06
Showing 1 changed files
... ...
@@ -1700,19 +1700,16 @@ void ht_iterator_init(void)
1700 1700
 	memset(_ht_iterators, 0, HT_ITERATOR_SIZE*sizeof(ht_iterator_t));
1701 1701
 }
1702 1702
 
1703
-int ht_iterator_start(str *iname, str *hname)
1703
+static inline int ht_iterator_find(str *iname)
1704 1704
 {
1705 1705
 	int i;
1706 1706
 	int k;
1707 1707
 
1708 1708
 	k = -1;
1709
-	for(i=0; i<HT_ITERATOR_SIZE; i++)
1710
-	{
1711
-		if(_ht_iterators[i].name.len>0)
1712
-		{
1709
+	for(i=0; i<HT_ITERATOR_SIZE; i++) {
1710
+		if(_ht_iterators[i].name.len>0) {
1713 1711
 			if(_ht_iterators[i].name.len==iname->len
1714
-					&& strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
1715
-			{
1712
+					&& strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0) {
1716 1713
 				k = i;
1717 1714
 				break;
1718 1715
 			}
... ...
@@ -1720,6 +1717,14 @@ int ht_iterator_start(str *iname, str *hname)
1720 1717
 			if(k==-1) k = i;
1721 1718
 		}
1722 1719
 	}
1720
+	return k;
1721
+}
1722
+
1723
+int ht_iterator_start(str *iname, str *hname)
1724
+{
1725
+	int k;
1726
+
1727
+	k = ht_iterator_find(iname);
1723 1728
 	if(k==-1)
1724 1729
 	{
1725 1730
 		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)
1759 1764
 
1760 1765
 int ht_iterator_next(str *iname)
1761 1766
 {
1762
-	int i;
1763 1767
 	int k;
1764 1768
 
1765
-	k = -1;
1766
-	for(i=0; i<HT_ITERATOR_SIZE; i++)
1767
-	{
1768
-		if(_ht_iterators[i].name.len>0)
1769
-		{
1770
-			if(_ht_iterators[i].name.len==iname->len
1771
-					&& strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
1772
-			{
1773
-				k = i;
1774
-				break;
1775
-			}
1776
-		} else {
1777
-			if(k==-1) k = i;
1778
-		}
1779
-	}
1769
+	k = ht_iterator_find(iname);
1780 1770
 	if(k==-1)
1781 1771
 	{
1782 1772
 		LM_ERR("iterator not found [%.*s]\n", iname->len, iname->s);
... ...
@@ -1819,47 +1809,38 @@ int ht_iterator_next(str *iname)
1819 1809
 
1820 1810
 int ht_iterator_end(str *iname)
1821 1811
 {
1822
-	int i;
1812
+	int k;
1823 1813
 
1824
-	for(i=0; i<HT_ITERATOR_SIZE; i++)
1814
+	k = ht_iterator_find(iname);
1815
+	if(k==-1 || _ht_iterators[k].name.len<=0)
1825 1816
 	{
1826
-		if(_ht_iterators[i].name.len>0)
1817
+		LM_ERR("iterator not found [%.*s]\n", iname->len, iname->s);
1818
+		return -1;
1819
+	}
1820
+
1821
+	if(_ht_iterators[k].ht!=NULL && _ht_iterators[k].it!=NULL)
1822
+	{
1823
+		if(_ht_iterators[k].slot>=0 && _ht_iterators[k].slot<_ht_iterators[k].ht->htsize)
1827 1824
 		{
1828
-			if(_ht_iterators[i].name.len==iname->len
1829
-					&& strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
1830
-			{
1831
-				if(_ht_iterators[i].ht!=NULL && _ht_iterators[i].it!=NULL)
1832
-				{
1833
-					if(_ht_iterators[i].slot>=0 && _ht_iterators[i].slot<_ht_iterators[i].ht->htsize)
1834
-					{
1835
-						ht_slot_unlock(_ht_iterators[i].ht, _ht_iterators[i].slot);
1836
-					}
1837
-				}
1838
-				memset(&_ht_iterators[i], 0, sizeof(ht_iterator_t));
1839
-				return 0;
1840
-			}
1825
+			ht_slot_unlock(_ht_iterators[k].ht, _ht_iterators[k].slot);
1841 1826
 		}
1842 1827
 	}
1843
-
1844
-	return -1;
1828
+	memset(&_ht_iterators[k], 0, sizeof(ht_iterator_t));
1829
+	return 0;
1845 1830
 }
1846 1831
 
1847 1832
 ht_cell_t* ht_iterator_get_current(str *iname)
1848 1833
 {
1849
-	int i;
1834
+	int k;
1835
+
1850 1836
 	if(iname==NULL || iname->len<=0)
1851 1837
 		return NULL;
1852 1838
 
1853
-	for(i=0; i<HT_ITERATOR_SIZE; i++)
1854
-	{
1855
-		if(_ht_iterators[i].name.len>0)
1856
-		{
1857
-			if(_ht_iterators[i].name.len==iname->len
1858
-					&& strncmp(_ht_iterators[i].name.s, iname->s, iname->len)==0)
1859
-			{
1860
-				return _ht_iterators[i].it;
1861
-			}
1862
-		}
1839
+	k = ht_iterator_find(iname);
1840
+	if(k==-1 || _ht_iterators[k].name.len<=0) {
1841
+		LM_DBG("iterator not found [%.*s]\n", iname->len, iname->s);
1842
+		return NULL;
1863 1843
 	}
1864
-	return NULL;
1844
+
1845
+	return _ht_iterators[k].it;
1865 1846
 }