[sr-dev] git:master: pv: use a pool of buffers for transformations

Daniel-Constantin Mierla miconda at gmail.com
Thu Dec 15 19:00:28 CET 2011


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

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Thu Dec 15 18:58:02 2011 +0100

pv: use a pool of buffers for transformations

- currently 4 slots - safer for chained string transformations that need
  the local buffer

---

 modules_k/pv/pv.c       |    5 +++++
 modules_k/pv/pv_trans.c |   37 ++++++++++++++++++++++++++++++++++++-
 modules_k/pv/pv_trans.h |    2 ++
 3 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/modules_k/pv/pv.c b/modules_k/pv/pv.c
index c195a06..f5784d9 100644
--- a/modules_k/pv/pv.c
+++ b/modules_k/pv/pv.c
@@ -473,6 +473,11 @@ static void mod_destroy(void)
 
 int mod_register(char *path, int *dlflags, void *p1, void *p2)
 {
+	if(tr_init_buffers()<0)
+	{
+		LM_ERR("failed to initialize transformations buffers\n");
+		return -1;
+	}
 	return register_trans_mod(path, mod_trans);
 }
 
diff --git a/modules_k/pv/pv_trans.c b/modules_k/pv/pv_trans.c
index a411351..f1dcce3 100644
--- a/modules_k/pv/pv_trans.c
+++ b/modules_k/pv/pv_trans.c
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <time.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -51,10 +52,42 @@
 
 /*! transformation buffer size */
 #define TR_BUFFER_SIZE 65536
+#define TR_BUFFER_SLOTS	4
 
 /*! transformation buffer */
-static char _tr_buffer[TR_BUFFER_SIZE];
+static char **_tr_buffer_list = NULL;
 
+static char *_tr_buffer = NULL;
+
+static int _tr_buffer_idx = 0;
+
+/*!
+ *
+ */
+int tr_init_buffers(void)
+{
+	int i;
+
+	_tr_buffer_list = (char**)malloc(TR_BUFFER_SLOTS);
+	if(_tr_buffer_list==NULL)
+		return -1;
+	for(i=0; i<TR_BUFFER_SLOTS; i++) {
+		_tr_buffer_list[i] = (char*)malloc(TR_BUFFER_SIZE);
+		if(_tr_buffer_list[i]==NULL)
+			return -1;
+	}
+	return 0;
+}
+
+/*!
+ *
+ */
+char *tr_set_crt_buffer(void)
+{
+	_tr_buffer = _tr_buffer_list[_tr_buffer_idx];
+	_tr_buffer_idx = (_tr_buffer_idx + 1) % TR_BUFFER_SLOTS;
+	return _tr_buffer;
+}
 
 /*!
  * \brief Evaluate string transformations
@@ -76,6 +109,8 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
 	if(val==NULL || val->flags&PV_VAL_NULL)
 		return -1;
 
+	tr_set_crt_buffer();
+
 	switch(subtype)
 	{
 		case TR_S_LEN:
diff --git a/modules_k/pv/pv_trans.h b/modules_k/pv/pv_trans.h
index 391084c..4d77e1d 100644
--- a/modules_k/pv/pv_trans.h
+++ b/modules_k/pv/pv_trans.h
@@ -66,4 +66,6 @@ char* tr_parse_paramlist(str *in, trans_t *tr);
 char* tr_parse_nameaddr(str *in, trans_t *tr);
 char* tr_parse_tobody(str* in, trans_t *t);
 
+int tr_init_buffers(void);
+
 #endif




More information about the sr-dev mailing list