Browse code

core: exported internal static buffer through PV API

- access to the static buffers used to print dynamic strings with PVs
can be accessed via PV api

Daniel-Constantin Mierla authored on 15/07/2011 14:35:15
Showing 3 changed files
... ...
@@ -555,7 +555,7 @@ void cleanup(show_status)
555 555
 	destroy_sctp();
556 556
 #endif
557 557
 	destroy_timer();
558
-	destroy_pv_api();
558
+	pv_destroy_api();
559 559
 	destroy_script_cb();
560 560
 	destroy_nonsip_hooks();
561 561
 	destroy_routes();
... ...
@@ -1975,7 +1975,7 @@ int main(int argc, char** argv)
1975 1975
 	if (init_routes()<0) goto error;
1976 1976
 	if (init_nonsip_hooks()<0) goto error;
1977 1977
 	if (init_script_cb()<0) goto error;
1978
-	if (init_pv_api()<0) goto error;
1978
+	if (pv_init_api()<0) goto error;
1979 1979
 	if (pv_register_core_vars()!=0) goto error;
1980 1980
 	if (init_rpcs()<0) goto error;
1981 1981
 	if (register_core_rpcs()!=0) goto error;
... ...
@@ -1195,16 +1195,10 @@ void pv_value_destroy(pv_value_t *val)
1195 1195
 	memset(val, 0, sizeof(pv_value_t));
1196 1196
 }
1197 1197
 
1198
-#define PV_PRINT_BUF_SIZE  1024
1199
-#define PV_PRINT_BUF_NO    3
1200 1198
 int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s)
1201 1199
 {
1202
-	static int buf_itr = 0;
1203
-	static char buf[PV_PRINT_BUF_NO][PV_PRINT_BUF_SIZE];
1204
-
1205
-	s->s = buf[buf_itr];
1206
-	s->len = PV_PRINT_BUF_SIZE;
1207
-	buf_itr = (buf_itr+1)%PV_PRINT_BUF_NO;
1200
+	s->s = pv_get_buffer();
1201
+	s->len = pv_get_buffer_size();
1208 1202
 	return pv_printf( msg, list, s->s, &s->len);
1209 1203
 }
1210 1204
 
... ...
@@ -1583,10 +1577,12 @@ static pv_export_t _core_pvs[] = {
1583 1583
 /** init pv api (optional).
1584 1584
  * @return 0 on success, -1 on error
1585 1585
  */
1586
-int init_pv_api(void)
1586
+int pv_init_api(void)
1587 1587
 {
1588 1588
 	pv_init_table();
1589 1589
 	tr_init_table();
1590
+	if(pv_init_buffer()<0)
1591
+		return -1;
1590 1592
 	if(register_pvars_mod("core", _core_pvs)<0)
1591 1593
 		return -1;
1592 1594
 	return 0;
... ...
@@ -1594,10 +1590,133 @@ int init_pv_api(void)
1594 1594
 
1595 1595
 
1596 1596
 /** destroy pv api. */
1597
-void destroy_pv_api(void)
1597
+void pv_destroy_api(void)
1598 1598
 {
1599 1599
 	/* free PV and TR hash tables */
1600 1600
 	pv_table_free();
1601 1601
 	tr_table_free();
1602
+	pv_destroy_buffer();
1602 1603
 	return;
1603 1604
 }
1605
+
1606
+/**
1607
+ * - buffer to print PVs
1608
+ */
1609
+static char **_pv_print_buffer = NULL;
1610
+#define PV_DEFAULT_PRINT_BUFFER_SIZE 1024
1611
+static int _pv_print_buffer_size  = PV_DEFAULT_PRINT_BUFFER_SIZE;
1612
+/* 6 mod params + 4 direct usage from mods */
1613
+#define PV_DEFAULT_PRINT_BUFFER_SLOTS 10
1614
+static int _pv_print_buffer_slots = PV_DEFAULT_PRINT_BUFFER_SLOTS;
1615
+
1616
+/**
1617
+ *
1618
+ */
1619
+int pv_init_buffer(void)
1620
+{
1621
+	int i;
1622
+
1623
+	/* already initialized ?!? */
1624
+	if(_pv_print_buffer!=NULL)
1625
+		return 0;
1626
+
1627
+	_pv_print_buffer =
1628
+		(char**)pkg_malloc(_pv_print_buffer_slots*sizeof(char*));
1629
+	if(_pv_print_buffer==NULL)
1630
+	{
1631
+		LM_ERR("cannot init PV print buffer slots\n");
1632
+		return -1;
1633
+	}
1634
+	memset(_pv_print_buffer, 0, _pv_print_buffer_slots);
1635
+	for(i=0; i<_pv_print_buffer_slots; i++)
1636
+	{
1637
+		_pv_print_buffer[i] =
1638
+			(char*)pkg_malloc(_pv_print_buffer_size*sizeof(char));
1639
+		if(_pv_print_buffer[i]==NULL)
1640
+		{
1641
+			LM_ERR("cannot init PV print buffer slot[%d]\n", i);
1642
+			return -1;
1643
+		}
1644
+	}
1645
+	return 0;
1646
+}
1647
+
1648
+/**
1649
+ *
1650
+ */
1651
+void pv_destroy_buffer(void)
1652
+{
1653
+	int i;
1654
+
1655
+	if(_pv_print_buffer==NULL)
1656
+		return;
1657
+	for(i=0; i<_pv_print_buffer_slots; i++)
1658
+	{
1659
+		if(_pv_print_buffer[i]!=NULL)
1660
+			pkg_free(_pv_print_buffer[i]);
1661
+	}
1662
+	pkg_free(_pv_print_buffer);
1663
+	_pv_print_buffer = NULL;
1664
+}
1665
+
1666
+/**
1667
+ *
1668
+ */
1669
+int pv_reinit_buffer(void)
1670
+{
1671
+	if(_pv_print_buffer_size==PV_DEFAULT_PRINT_BUFFER_SIZE
1672
+			&& _pv_print_buffer_slots==PV_DEFAULT_PRINT_BUFFER_SLOTS)
1673
+		return 0;
1674
+	pv_destroy_buffer();
1675
+	return pv_init_buffer();
1676
+}
1677
+
1678
+/**
1679
+ *
1680
+ */
1681
+char* pv_get_buffer(void)
1682
+{
1683
+	char *p;
1684
+	static int _pv_print_buffer_itr = 0;
1685
+
1686
+	p = _pv_print_buffer[_pv_print_buffer_itr];
1687
+	_pv_print_buffer_itr = (_pv_print_buffer_itr+1)%_pv_print_buffer_slots;
1688
+
1689
+	return p;
1690
+}
1691
+
1692
+/**
1693
+ *
1694
+ */
1695
+int pv_get_buffer_size(void)
1696
+{
1697
+	return _pv_print_buffer_size;
1698
+}
1699
+
1700
+/**
1701
+ *
1702
+ */
1703
+int pv_get_buffer_slots(void)
1704
+{
1705
+	return _pv_print_buffer_slots;
1706
+}
1707
+
1708
+/**
1709
+ *
1710
+ */
1711
+void pv_set_buffer_size(int n)
1712
+{
1713
+	_pv_print_buffer_size = n;
1714
+	if(_pv_print_buffer_size<=0)
1715
+		_pv_print_buffer_size = PV_DEFAULT_PRINT_BUFFER_SIZE;
1716
+}
1717
+
1718
+/**
1719
+ *
1720
+ */
1721
+void pv_set_buffer_slots(int n)
1722
+{
1723
+	_pv_print_buffer_slots = n;
1724
+	if(_pv_print_buffer_slots<=0)
1725
+		_pv_print_buffer_slots = PV_DEFAULT_PRINT_BUFFER_SLOTS;
1726
+}
... ...
@@ -22,9 +22,18 @@
22 22
 #ifndef __pvapi_h__
23 23
 #define __pvapi_h__
24 24
 
25
-int init_pv_api(void);
26
-void destroy_pv_api(void);
25
+int  pv_init_api(void);
26
+void pv_destroy_api(void);
27 27
 
28
-#endif /*__pvapi_h_*/
28
+int   pv_init_buffer(void);
29
+int   pv_resize_buffer(void);
30
+void  pv_destroy_buffer(void);
31
+char* pv_get_buffer(void);
32
+int   pv_get_buffer_size(void);
33
+int   pv_get_buffer_slots(void);
34
+void  pv_set_buffer_size(int n);
35
+void  pv_set_buffer_slots(int n);
36
+
37
+#endif /*__pvapi_h__*/
29 38
 
30 39
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */