[sr-dev] git:master: lib/srdb1: helper function to reallocate the index for result rows

Daniel-Constantin Mierla miconda at gmail.com
Sun Sep 14 15:08:30 CEST 2014


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

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Sun Sep 14 14:32:05 2014 +0200

lib/srdb1: helper function to reallocate the index for result rows

- useful for db connectors that don't know in advance the number of rows in result

---

 lib/srdb1/db_res.c |   33 +++++++++++++++++++++++++++++++++
 lib/srdb1/db_res.h |    8 ++++++++
 2 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/lib/srdb1/db_res.c b/lib/srdb1/db_res.c
index 95976a0..1a775ca 100644
--- a/lib/srdb1/db_res.c
+++ b/lib/srdb1/db_res.c
@@ -186,3 +186,36 @@ int db_allocate_rows(db1_res_t* _res)
 	
 	return 0;
 }
+
+/**
+ * Reallocate memory for rows.
+ * \param _res result set
+ * \param _nsize new number of rows in result set
+ * \return zero on success, negative on errors
+ */
+int db_reallocate_rows(db1_res_t* _res, int _nsize)
+{
+	int len;
+	int osize;
+	db_row_t *orows;
+
+	orows = RES_ROWS(_res);
+	osize = RES_ROW_N(_res);
+
+	RES_ROW_N(_res) = _nsize;
+	len = sizeof(db_row_t) * RES_ROW_N(_res);
+	RES_ROWS(_res) = (struct db_row*)pkg_malloc(len);
+	if (!RES_ROWS(_res)) {
+		LM_ERR("no private memory left\n");
+		return -1;
+	}
+	LM_DBG("allocate %d bytes for rows at %p\n", len, RES_ROWS(_res));
+	memset(RES_ROWS(_res), 0, len);
+
+	if(orows==NULL)
+		return 0;
+	memcpy(RES_ROWS(_res), orows,
+			((osize<_nsize)?osize:_nsize)*sizeof(db_row_t));
+	pkg_free(orows);
+	return 0;
+}
diff --git a/lib/srdb1/db_res.h b/lib/srdb1/db_res.h
index 9a26325..39f4e21 100644
--- a/lib/srdb1/db_res.h
+++ b/lib/srdb1/db_res.h
@@ -131,4 +131,12 @@ int db_allocate_columns(db1_res_t* _r, const unsigned int cols);
  */
 int db_allocate_rows(db1_res_t* _res);
 
+/**
+ * Reallocate memory for rows.
+ * \param _res result set
+ * \param _nsize new number of rows in result set
+ * \return zero on success, negative on errors
+ */
+int db_reallocate_rows(db1_res_t* _res, int _nsize);
+
 #endif /* DB1_RES_H */




More information about the sr-dev mailing list