[sr-dev] git:master: core: exported internal static buffer through PV API

Daniel-Constantin Mierla miconda at gmail.com
Fri Jul 15 22:56:01 CEST 2011


Module: sip-router
Branch: master
Commit: 7e7d939980f147c2e473fd7f58dee7b15def37c9
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7e7d939980f147c2e473fd7f58dee7b15def37c9

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Fri Jul 15 16:35:15 2011 +0200

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

---

 main.c  |    4 +-
 pvapi.c |  139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 pvapi.h |   15 +++++-
 3 files changed, 143 insertions(+), 15 deletions(-)

diff --git a/main.c b/main.c
index f69b394..a35faf3 100644
--- a/main.c
+++ b/main.c
@@ -555,7 +555,7 @@ void cleanup(show_status)
 	destroy_sctp();
 #endif
 	destroy_timer();
-	destroy_pv_api();
+	pv_destroy_api();
 	destroy_script_cb();
 	destroy_nonsip_hooks();
 	destroy_routes();
@@ -1975,7 +1975,7 @@ int main(int argc, char** argv)
 	if (init_routes()<0) goto error;
 	if (init_nonsip_hooks()<0) goto error;
 	if (init_script_cb()<0) goto error;
-	if (init_pv_api()<0) goto error;
+	if (pv_init_api()<0) goto error;
 	if (pv_register_core_vars()!=0) goto error;
 	if (init_rpcs()<0) goto error;
 	if (register_core_rpcs()!=0) goto error;
diff --git a/pvapi.c b/pvapi.c
index 7149bfb..8f87d58 100644
--- a/pvapi.c
+++ b/pvapi.c
@@ -1195,16 +1195,10 @@ void pv_value_destroy(pv_value_t *val)
 	memset(val, 0, sizeof(pv_value_t));
 }
 
-#define PV_PRINT_BUF_SIZE  1024
-#define PV_PRINT_BUF_NO    3
 int pv_printf_s(struct sip_msg* msg, pv_elem_p list, str *s)
 {
-	static int buf_itr = 0;
-	static char buf[PV_PRINT_BUF_NO][PV_PRINT_BUF_SIZE];
-
-	s->s = buf[buf_itr];
-	s->len = PV_PRINT_BUF_SIZE;
-	buf_itr = (buf_itr+1)%PV_PRINT_BUF_NO;
+	s->s = pv_get_buffer();
+	s->len = pv_get_buffer_size();
 	return pv_printf( msg, list, s->s, &s->len);
 }
 
@@ -1583,10 +1577,12 @@ static pv_export_t _core_pvs[] = {
 /** init pv api (optional).
  * @return 0 on success, -1 on error
  */
-int init_pv_api(void)
+int pv_init_api(void)
 {
 	pv_init_table();
 	tr_init_table();
+	if(pv_init_buffer()<0)
+		return -1;
 	if(register_pvars_mod("core", _core_pvs)<0)
 		return -1;
 	return 0;
@@ -1594,10 +1590,133 @@ int init_pv_api(void)
 
 
 /** destroy pv api. */
-void destroy_pv_api(void)
+void pv_destroy_api(void)
 {
 	/* free PV and TR hash tables */
 	pv_table_free();
 	tr_table_free();
+	pv_destroy_buffer();
 	return;
 }
+
+/**
+ * - buffer to print PVs
+ */
+static char **_pv_print_buffer = NULL;
+#define PV_DEFAULT_PRINT_BUFFER_SIZE 1024
+static int _pv_print_buffer_size  = PV_DEFAULT_PRINT_BUFFER_SIZE;
+/* 6 mod params + 4 direct usage from mods */
+#define PV_DEFAULT_PRINT_BUFFER_SLOTS 10
+static int _pv_print_buffer_slots = PV_DEFAULT_PRINT_BUFFER_SLOTS;
+
+/**
+ *
+ */
+int pv_init_buffer(void)
+{
+	int i;
+
+	/* already initialized ?!? */
+	if(_pv_print_buffer!=NULL)
+		return 0;
+
+	_pv_print_buffer =
+		(char**)pkg_malloc(_pv_print_buffer_slots*sizeof(char*));
+	if(_pv_print_buffer==NULL)
+	{
+		LM_ERR("cannot init PV print buffer slots\n");
+		return -1;
+	}
+	memset(_pv_print_buffer, 0, _pv_print_buffer_slots);
+	for(i=0; i<_pv_print_buffer_slots; i++)
+	{
+		_pv_print_buffer[i] =
+			(char*)pkg_malloc(_pv_print_buffer_size*sizeof(char));
+		if(_pv_print_buffer[i]==NULL)
+		{
+			LM_ERR("cannot init PV print buffer slot[%d]\n", i);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+/**
+ *
+ */
+void pv_destroy_buffer(void)
+{
+	int i;
+
+	if(_pv_print_buffer==NULL)
+		return;
+	for(i=0; i<_pv_print_buffer_slots; i++)
+	{
+		if(_pv_print_buffer[i]!=NULL)
+			pkg_free(_pv_print_buffer[i]);
+	}
+	pkg_free(_pv_print_buffer);
+	_pv_print_buffer = NULL;
+}
+
+/**
+ *
+ */
+int pv_reinit_buffer(void)
+{
+	if(_pv_print_buffer_size==PV_DEFAULT_PRINT_BUFFER_SIZE
+			&& _pv_print_buffer_slots==PV_DEFAULT_PRINT_BUFFER_SLOTS)
+		return 0;
+	pv_destroy_buffer();
+	return pv_init_buffer();
+}
+
+/**
+ *
+ */
+char* pv_get_buffer(void)
+{
+	char *p;
+	static int _pv_print_buffer_itr = 0;
+
+	p = _pv_print_buffer[_pv_print_buffer_itr];
+	_pv_print_buffer_itr = (_pv_print_buffer_itr+1)%_pv_print_buffer_slots;
+
+	return p;
+}
+
+/**
+ *
+ */
+int pv_get_buffer_size(void)
+{
+	return _pv_print_buffer_size;
+}
+
+/**
+ *
+ */
+int pv_get_buffer_slots(void)
+{
+	return _pv_print_buffer_slots;
+}
+
+/**
+ *
+ */
+void pv_set_buffer_size(int n)
+{
+	_pv_print_buffer_size = n;
+	if(_pv_print_buffer_size<=0)
+		_pv_print_buffer_size = PV_DEFAULT_PRINT_BUFFER_SIZE;
+}
+
+/**
+ *
+ */
+void pv_set_buffer_slots(int n)
+{
+	_pv_print_buffer_slots = n;
+	if(_pv_print_buffer_slots<=0)
+		_pv_print_buffer_slots = PV_DEFAULT_PRINT_BUFFER_SLOTS;
+}
diff --git a/pvapi.h b/pvapi.h
index 26de06b..f3fd261 100644
--- a/pvapi.h
+++ b/pvapi.h
@@ -22,9 +22,18 @@
 #ifndef __pvapi_h__
 #define __pvapi_h__
 
-int init_pv_api(void);
-void destroy_pv_api(void);
+int  pv_init_api(void);
+void pv_destroy_api(void);
 
-#endif /*__pvapi_h_*/
+int   pv_init_buffer(void);
+int   pv_resize_buffer(void);
+void  pv_destroy_buffer(void);
+char* pv_get_buffer(void);
+int   pv_get_buffer_size(void);
+int   pv_get_buffer_slots(void);
+void  pv_set_buffer_size(int n);
+void  pv_set_buffer_slots(int n);
+
+#endif /*__pvapi_h__*/
 
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */




More information about the sr-dev mailing list