浏览代码

pv: use a pool of buffers for transformations

- currently 4 slots - safer for chained string transformations that need
  the local buffer
Daniel-Constantin Mierla 13 年之前
父节点
当前提交
3775eb7730
共有 3 个文件被更改,包括 43 次插入1 次删除
  1. 5 0
      modules_k/pv/pv.c
  2. 36 1
      modules_k/pv/pv_trans.c
  3. 2 0
      modules_k/pv/pv_trans.h

+ 5 - 0
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);
 }
 

+ 36 - 1
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:

+ 2 - 0
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