[sr-dev] git:master: lib/srdb1: eleminate string copy in db_val2pv_spec()

Alex Hermann alex at speakup.nl
Mon Sep 12 13:23:55 CEST 2011


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

Author: Alex Hermann <alex at speakup.nl>
Committer: Alex Hermann <alex at speakup.nl>
Date:   Tue Sep  6 10:18:10 2011 +0200

lib/srdb1: eleminate string copy in db_val2pv_spec()

After fixing a memory leak, i now realize pv_set_spec_value() always makes
a string copy too, so this one can be removed safely.

---

 lib/srdb1/db_ut.c |   52 +++++++++-------------------------------------------
 1 files changed, 9 insertions(+), 43 deletions(-)

diff --git a/lib/srdb1/db_ut.c b/lib/srdb1/db_ut.c
index babe0e7..840c861 100644
--- a/lib/srdb1/db_ut.c
+++ b/lib/srdb1/db_ut.c
@@ -407,8 +407,7 @@ int db_print_set(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k
 int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs)
 {
 	pv_value_t pv;
-	str sv = {NULL, 0};
-	static str _str_empty = { "", 0 };
+	char ll_buf[21];  /* sign, 19 digits and \0 */
 
 	if(dbval->nul)
 	{
@@ -419,18 +418,18 @@ int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs)
 		{
 			case DB1_STRING:
 				pv.flags = PV_VAL_STR;
-				sv.s = (char*)dbval->val.string_val;
-				sv.len = strlen(sv.s);
+				pv.rs.s = (char*)dbval->val.string_val;
+				pv.rs.len = strlen(pv.rs.s);
 			break;
 			case DB1_STR:
 				pv.flags = PV_VAL_STR;
-				sv.s = (char*)dbval->val.str_val.s;
-				sv.len = dbval->val.str_val.len;
+				pv.rs.s = (char*)dbval->val.str_val.s;
+				pv.rs.len = dbval->val.str_val.len;
 			break;
 			case DB1_BLOB:
 				pv.flags = PV_VAL_STR;
-				sv.s = (char*)dbval->val.blob_val.s;
-				sv.len = dbval->val.blob_val.len;
+				pv.rs.s = (char*)dbval->val.blob_val.s;
+				pv.rs.len = dbval->val.blob_val.len;
 			break;
 			case DB1_INT:
 				pv.flags = PV_VAL_INT | PV_TYPE_INT;
@@ -447,14 +446,8 @@ int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs)
 			case DB1_BIGINT:
 				/* BIGINT is stored as string */
 				pv.flags = PV_VAL_STR;
-				pv.rs.len = 21*sizeof(char);
-				pv.rs.s = (char*)pkg_malloc(pv.rs.len);
-				if (pv.rs.s==NULL)
-				{
-					LM_ERR("no more memory\n");
-					return -1;
-				}
-				db_longlong2str(dbval->val.ll_val, pv.rs.s, &pv.rs.len);
+				db_longlong2str(dbval->val.ll_val, ll_buf, &pv.rs.len);
+				pv.rs.s = ll_buf;
 			break;
 			default:
 				LM_NOTICE("unknown field type: %d, setting value to null\n",
@@ -467,39 +460,12 @@ int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs)
 	if (pv.flags == PV_VAL_NULL && pvs->type == PVT_AVP)
 		return 0;
 
-	/* handle string values */
-	if(pv.flags == PV_VAL_STR && sv.s)
-	{
-		if(sv.len==0)
-		{
-			pv.rs = _str_empty;
-		} else
-		{
-			/* create copy of string value in pkg mem */
-			pv.rs.s = (char*)pkg_malloc(sv.len*sizeof(char));
-			if(pv.rs.s==NULL)
-			{
-				LM_ERR("no more memory\n");
-				return -1;
-			}
-			memcpy(pv.rs.s, sv.s, sv.len);
-			pv.rs.len = sv.len;
-		}
-	}
-
 	/* add value to result pv */
 	if (pv_set_spec_value(msg, pvs, 0, &pv) != 0)
 	{
 		LM_ERR("Failed to add value to spec\n");
-		if (pv.flags == PV_VAL_STR && pv.rs.len > 0)
-			pkg_free(pv.rs.s);
 		return -1;
 	}
 
-	/* free string memory */
-	if (pv.flags == PV_VAL_STR && pv.rs.len > 0) {
-		pkg_free(pv.rs.s);
-	}
-
 	return 0;
 }




More information about the sr-dev mailing list